滚动时GridView会迷失方向

时间:2017-09-21 23:42:15

标签: java android android-studio gridview android-arrayadapter

gridView中的项目有时会重叠或向上或向下移动一点。但是当再次向上滚动时会得到修复。不幸的是我无法提供图像,因为我是新来的。我不确定我是否在做某事code.But使用listView时没有问题。

gridView的Xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.android.gametalks.MainActivity"
    android:background="@color/card_white">

    <GridView
android:columnWidth="10dp"
                android:numColumns="2"
                android:id="@+id/listView"
                android:dividerHeight="0dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:verticalSpacing="30dp"
                ></GridView>

    <ProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"/>

</RelativeLayout>

这是适配器:

[public class GameAdapter extends  ArrayAdapter<GameNews> {


    public GameAdapter(@NonNull Context context, List<GameNews> gameNews ){
        super(context, 0, gameNews);
        if(gameNews == null)
        {
            return;
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        GameNews currentNews = getItem(position);

        // Check if the existing view is being reused, otherwise inflate the view
        View listItemView = convertView;
        if (listItemView == null) {
            listItemView = LayoutInflater.from(getContext()).inflate(
                    R.layout.grid_item, parent, false);
            final ViewHolder v = new ViewHolder();

            // Find the TextView in the grid_item.xml layout with the ID version_name

            v.Heading = (TextView) listItemView.findViewById(R.id.title);
            v.description = (TextView) listItemView.findViewById(R.id.description);
            v.newsImage = (ImageView) listItemView.findViewById(R.id.image) ;
            v.source = (TextView)  listItemView.findViewById(R.id.source);
            v.time = (TextView) listItemView.findViewById(R.id.time);
            v.menuButton = (Button) listItemView.findViewById(R.id.popup);
            listItemView.setTag(v);


            v.menuButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    PopupMenu popup = new PopupMenu(getContext(),view);
                    MenuInflater inflater = popup.getMenuInflater();
                    inflater.inflate(R.menu.popup_menu, popup.getMenu());
                    popup.show();

                }
            });
        }

ViewHolder viewHolder = (ViewHolder) listItemView.getTag();

        //Setting title to current news title
        viewHolder.Heading.setText(currentNews.getTitle());
        viewHolder.description.setText(currentNews.getDescription());
        viewHolder.newsImage.setTag(currentNews.getphotoUrl());
viewHolder.source.setText(currentNews.getSource());
       viewHolder.time.setText(currentNews.getTime());
      //  Picasso.with(getContext()).load(currentNews.getphotoUrl()).into(viewHolder.newsImage);


       if (URLUtil.isValidUrl(currentNews.getphotoUrl())) {
            Picasso.with(getContext())
                    .load(currentNews.getphotoUrl())
                    .resize(350,300)
                    .tag(tag)
                    .error(R.drawable.placeholder)
                    .placeholder(R.drawable.placeholder)
                    .into(viewHolder.newsImage);

        } else {
            Picasso.with(getContext())
                    .load(R.drawable.right_line)
                    .resize(350,300)
                    .tag(tag)
                    .noPlaceholder()
                    .into(viewHolder.newsImage);
        }

        return listItemView;
    }




    public static class ViewHolder {
         TextView Heading;
         TextView description;
        ImageView newsImage;
        TextView source;
TextView time;
Button menuButton;


    }
}][1]

如果您需要任何其他详细信息,请询问。

1 个答案:

答案 0 :(得分:0)

这是因为我设置了gridView项目的大小来包装内容,因此不同的项目有不同的大小。这导致gridView中的不稳定。我通过给gridView的项目固定dp大小来修复它。现在它没有问题