gpt4 book ai didi

java - RecyclerView 中的最后一项未显示

转载 作者:行者123 更新时间:2023-12-03 23:35:31 24 4
gpt4 key购买 nike

我有一个工作正常的回收 View ,但是当项目数量太大而无法在屏幕上显示时,我向下滚动,最后一个项目不会显示。但是,我可以通过日志看到已成功创建并绑定(bind)了一个 View 持有者。然后,如果我添加另一个项目,则最后一个项目将显示在末尾,依此类推。如果您能帮助我,我将不胜感激。


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>

最佳答案

添加:app:layout_constraintBottom_toBottomOf="parent"并将 recyclerview 的高度更改为 0dp

关于java - RecyclerView 中的最后一项未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58862657/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com