gpt4 book ai didi

android - Recyclerview 在滚动时导致应用关闭

转载 作者:搜寻专家 更新时间:2023-11-01 09:47:09 24 4
gpt4 key购买 nike

我有一个回收 View 。它在滚动时加载。但是当我连续从上到下滚动它时,它会在一段时间后崩溃(或关闭)。

错误:

Cannot scroll to position a LayoutManager set. Call setLayoutManager with a non-null argument.
E: No adapter attached; skipping layout
A: art/runtime/indirect_reference_table.cc:76] Check failed: table_mem_map_.get() != nullptr ashmem_create_region failed for 'indirect ref table': Too many open files
E: Cannot scroll to position a LayoutManager set. Call setLayoutManager with a non-null argument.
A: art/runtime/indirect_reference_table.cc:76] Check failed: table_mem_map_.get() != nullptr ashmem_create_region failed for 'indirect ref table': Too many open files

BookSearchResultListAdapter.java

public class BookSearchResultListAdapter extends RecyclerView.Adapter<BookSearchResultListAdapter.ViewHolder> implements View.OnClickListener{

private ArrayList<ProductModel> productList;
private Fragment fragment;
private RecyclerView recyclerView;
private int position = 0;

public BookSearchResultListAdapter(Fragment fragment, ArrayList<ProductModel> productList, RecyclerView recyclerView) {
this.productList = productList;
this.fragment = fragment;
this.recyclerView = recyclerView;
}

@Override
public void onClick(View v) {
int position = recyclerView.getChildLayoutPosition(v);
((BookSearchResultAdapterListener)fragment).onProductItemClicked(productList.get(position));
}


private void setOnScrollListener() {
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if(recyclerView.getLayoutManager() != null){
LinearLayoutManager lm= (LinearLayoutManager) recyclerView.getLayoutManager();
int Lastposition = lm.findLastCompletelyVisibleItemPosition();
if(Lastposition == productList.size()-1 && Lastposition != position){
position = lm.findLastCompletelyVisibleItemPosition();
((BookSearchResultAdapterListener)fragment).onListScrolled();
}
}

}

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
}
});
}

public void addCollectionToList(ArrayList<ProductModel> productList){
this.productList.addAll(productList);
notifyItemRangeChanged(0, productList.size() - 1);
}


public static class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView productName;
public TextView manufacturerName;
public TextView publisherName;
public ImageView image;
public ImageView isShelvedIcon;

public ViewHolder(View view) {
super(view);
}
}

@Override
public BookSearchResultListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.component_library_product, parent, false);

ViewHolder holder = new ViewHolder(v);
holder.productName = (TextView) v.findViewById(R.id.productName);
holder.manufacturerName = (TextView)v.findViewById(R.id.pageNumber);
holder.publisherName = (TextView)v.findViewById(R.id.publisherName);
holder.image = (ImageView)v.findViewById(R.id.productImage);
holder.isShelvedIcon = (ImageView)v.findViewById(R.id.isShelvedIcon);

holder.productName.setTypeface(App.MUSEO_300);
holder.manufacturerName.setTypeface(App.MUSEO_300);
holder.publisherName.setTypeface(App.MUSEO_300);

holder.isShelvedIcon.setVisibility(View.GONE);
v.setOnClickListener(this);
setOnScrollListener();
return holder;
}


@Override
public void onBindViewHolder(ViewHolder holder, int position) {
final ProductModel productModel = productList.get(position);
holder.productName.setText(productModel.getName());
holder.manufacturerName.setText(productModel.getManufacturer());
holder.publisherName.setText(productModel.getPublisher());

Picasso.Builder builder = new Picasso.Builder(fragment.getContext());
Picasso picasso = builder.build();
picasso.with(fragment.getContext()).load(productModel.getImage()).into(holder.image);
}

@Override
public int getItemCount() {
return productList.size();
}

public interface BookSearchResultAdapterListener {
void onProductItemClicked(ProductModel productModel);
void onListScrolled();
}
}

最佳答案

我找到了解决方案。它与 Picasso 库有关。只需更改以下代码:

Picasso.Builder builder = new Picasso.Builder(fragment.getContext());
Picasso picasso = builder.build();
picasso.with(fragment.getContext()).load(productModel.getImage()).into(holder.image);

到:

   Picasso.with(fragment.getContext()).load(productModel.getImage()).into(holder.image);

关于android - Recyclerview 在滚动时导致应用关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37593592/

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