我有一个回收视图,在物品数量太多而无法适应屏幕并向下滚动时,最后一个物品不会显示。 但是,通过记录,我可以看到一个视图持有者已成功创建和绑定。然后,如果我添加另一个项目,则以前的最后一个项目将在最后显示,依此类推。如果您能帮助我,我将不胜感激。
public class ProdListAdapter extends RecyclerView.Adapter<ProdListItemViewHolder> implements Filterable {
public List<ProductListItem> prodListItems = null;
public List<ProductListItem> displayedProdListItems = null;
private int currListItemId;
private RecyclerView recyclerView;
private ProductsListFragment fragment;
private MainActivity activity;
public ProdListAdapter(ProductsListFragment fragment, MainActivity activity) {
System.out.println("LIST ADAPTER CONSTRUCTOR");
this.prodListItems = new ArrayList<>();
for (int i = 0; i < activity.products.size(); i++) {
Product currProd = activity.products.get(i);
System.out.println(currProd.getName() + " " + i);
//add category separator
if (i > 0 && !currProd.getCategory().equals(activity.products.get(i - 1).getCategory())) {
prodListItems.add(new ProductListCategory(currProd));
} else if (i == 0) {
prodListItems.add(new ProductListCategory(currProd));
}
prodListItems.add(new ProductListItem(currProd));
}
this.activity = activity;
this.displayedProdListItems = prodListItems;
this.currListItemId = 0;
this.fragment = fragment;
}
// Create new views (invoked by the layout manager)
@Override
public ProdListItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
int idToUse = currListItemId % prodListItems.size();
boolean isCatSeparator = false;
ProductListItem currProdListItem = prodListItems.get(idToUse);
ConstraintLayout listItemView = (ConstraintLayout) LayoutInflater.from(parent.getContext())
.inflate(R.layout.product_list_item, parent, false);
if (currProdListItem.isCategorySeparator()) {
isCatSeparator = true;
}
if (!isCatSeparator) {
Product currProd = currProdListItem.getProduct();
}
System.out.println("CREATING: " + currProdListItem.getItemText());
System.out.println("idToUse: " + idToUse + " " + currProdListItem.getItemText());
ProdListItemViewHolder viewHolder = new ProdListItemViewHolder(listItemView, isCatSeparator, fragment, currProdListItem);
currListItemId++;
return viewHolder;
}
// (invoked by the layout manager)
// Replace the contents of a view by the item at index: itemIndex
@Override
public void onBindViewHolder(ProdListItemViewHolder viewHolder, int itemIndex) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
//update product item
ProductListItem newListViewItem = displayedProdListItems.get(itemIndex);
boolean newItemIsCatSeparator = newListViewItem.isCategorySeparator();
boolean oldItemIsCatSeparator = viewHolder.getProductListItem().isCategorySeparator();
if (!newItemIsCatSeparator) {
Product currProd = newListViewItem.getProduct();
}
System.out.println();
System.out.println(viewHolder.getProductListItem().getItemText() + " -> " + newListViewItem.getItemText());
System.out.println(viewHolder.getProductListItem().isCategorySeparator() + " TO " + newListViewItem.isCategorySeparator());
System.out.println("checked " + viewHolder.getProductListItem().isChecked() + " to " + newListViewItem.isChecked());
System.out.println();
if (newItemIsCatSeparator && oldItemIsCatSeparator) {
//System.out.println("1");
viewHolder.changeCategory(newListViewItem.getProduct());
} else if (!newItemIsCatSeparator && !oldItemIsCatSeparator) {
//System.out.println("2");
viewHolder.changeProduct(newListViewItem);
} else if (newItemIsCatSeparator && !oldItemIsCatSeparator) {
//System.out.println("3");
viewHolder.toCatSeparator(newListViewItem.getProduct());
} else { // !newListViewItem.isCategorySeparator() && viewHolder.getProductListItem().isCategorySeparator()
//System.out.println("4");
viewHolder.toProdItem(newListViewItem);
}
}
@Override
public int getItemCount() {
int count = 0;
if(this.displayedProdListItems != null){
count = displayedProdListItems.size();
}
return count;
}
}
public class ProductsListFragment extends Fragment {
private int numCheckedItems = 0;
private BottomSheetBehavior botSheetBehavior;
private boolean botSheetOpen = false;
private ProdListAdapter prodAdapter;
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_products_list, container, false);
//recyclerView stuff below
RecyclerView recyclerView = (RecyclerView) root.findViewById(R.id.prod_list_recycler_view);
recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
LinearLayoutManager layoutManager = new LinearLayoutManager(this.getContext());
recyclerView.setLayoutManager(layoutManager);
prodAdapter = new ProdListAdapter(this, activity);
recyclerView.setAdapter(prodAdapter);
activity.setProdListAdapter(prodAdapter);
return root;
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:id="@+id/prodListLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<AutoCompleteTextView
android:id="@+id/searchTxtView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Search"
android:drawableLeft="@drawable/ic_search"
android:drawablePadding="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/prod_list_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dividerHeight="2dp"
android:scrollbars="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/searchTxtView" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:layout_marginEnd="28dp"
android:layout_marginRight="28dp"
android:layout_marginBottom="28dp"
android:src="@drawable/ic_fab_plus_36dp"
app:backgroundTint="#aa00ff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>