【问题标题】:GridView Layout items get distracted when scrollingGridView 布局项目在滚动时分心
【发布时间】:2020-02-23 10:03:51
【问题描述】:

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

我之前尝试过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>

【问题讨论】:

  • 你能不能也分享一下single_item XML。
  • @SandipSingh 谢谢。最后,至少有人评论了这个问题。非常感谢您的努力。我已经在里面添加了single_item.xml,请检查。

标签: android android-layout android-fragments gridview


【解决方案1】:

终于解决了,这里的事实在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;
}

感谢所有试图解决它的人!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-10
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多