Я использую GridView для отображения товаров. И все было хорошо. Но когда я прокручиваю, элементы, которые находятся ниже размера экрана, становятся верхними, когда я прокручиваю вниз. Укажите, что элементы находятся во фрагменте tabview.
Я пробовал ViewHolder()
метод ранее, но это устарело.
Вот мой адаптер GridView * Код файла 1017 *
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>
Во фрагменте есть инфлятор
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;
}
Вот этот единственный_элемент. 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>