【发布时间】: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_itemXML。 -
@SandipSingh 谢谢。最后,至少有人评论了这个问题。非常感谢您的努力。我已经在里面添加了single_item.xml,请检查。
标签: android android-layout android-fragments gridview