滚动时 GridView 布局项会分散注意力

GridView Layout items get distracted when scrolling

我正在使用 GridView 来显示产品项目。一切都很好。但是当我滚动时,当我向下滚动时,屏幕尺寸以下的项目会到达顶部。需要注意的是项目在 tabview 的片段中。

我之前尝试过 ViewHolder() 方法,但该方法已被弃用。

这是我的 GridView 适配器 java 文件代码

public class GridAdapter extends BaseAdapter {

Context context;
private final String [] titles;
private final int [] images;
View view;
LayoutInflater layoutInflater;
ImageView imageTrending;

public GridAdapter(Context context, String[] titles, int[] images) {

    this.context = context;
    this.titles = titles;
    this.images = images;

}

@Override
public int getCount() {
    return titles.length;
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

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

    layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        view = new View(context);
        view = layoutInflater.inflate(R.layout.single_item, null);
        imageTrending = (ImageView) view.findViewById(R.id.trendingImage);
        TextView titleTrending = (TextView) view.findViewById(R.id.trendingTitle);
        titleTrending.setText(titles[position]);
        imageTrending.setImageResource(images[position]);

    }
    return view;
   }
}

这是 .xml 文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Trending">

<!-- TODO: Update blank fragment layout -->
<GridView
    android:id="@+id/trendingGridView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:numColumns="auto_fit"
    android:columnWidth="150dp">

</GridView>


</RelativeLayout>

在 Fragment 中,这里是 Inflater

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_trending, container, false);
    gridView = (GridView) rootView.findViewById(R.id.trendingGridView);

    GridAdapter gridAdapter = new GridAdapter(getContext(), titles, products);
    gridView.setAdapter(gridAdapter);
    return rootView;

}

这是single_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="150dp"
android:padding="8dp"
android:gravity="center">

<androidx.cardview.widget.CardView
    android:layout_width="wrap_content"
    android:layout_height="150dp">

    <ImageView
        android:id="@+id/trendingImage"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <TextView
        android:id="@+id/trendingTitle"
        android:gravity="bottom"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</androidx.cardview.widget.CardView>

</LinearLayout>

终于解决了,这里的事实在getView

  1. 参数View convertView需要使用来膨胀 single_item.xml 查看。
  2. 必须使用 convertView 来初始化 View()
  3. 并使用相同的 convertView 设置视图中的内容。
  4. 终于返回 convertView

这是新解决的代码...

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

    if (convertView == null) {
        layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = new View(context);
        convertView = layoutInflater.inflate(R.layout.single_item, null);
        imageTrending = (ImageView) convertView.findViewById(R.id.trendingImage);
        TextView titleTrending = (TextView) convertView.findViewById(R.id.trendingTitle);
        titleTrending.setText(titles[position]);
        imageTrending.setImageResource(images[position]);
    }
    return convertView;
}

这是之前的代码...

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

layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);

if (convertView == null) {
    view = new View(context);
    view = layoutInflater.inflate(R.layout.single_item, null);
    imageTrending = (ImageView) view.findViewById(R.id.trendingImage);
    TextView titleTrending = (TextView) view.findViewById(R.id.trendingTitle);
    titleTrending.setText(titles[position]);
    imageTrending.setImageResource(images[position]);

}
return view;
}

感谢所有尝试解决问题的人!