【问题标题】:Recyclerview index position mess data on scrolling of recyclerRecyclerview index position mess data on scrolling of recycler
【发布时间】:2017-03-06 08:02:00
【问题描述】:

首次正确填充数据。但是当我从下到上滚动recyclerview 时,所有数据及其位置都混乱了。我尝试添加 itemViewHolder.setIsRecyclable(false); setHasStableIds(false) 但没有任何效果 在垂直 recyclerview 内部尝试填充水平 recyclerview

public HomeRecyclerViewAdapter(Context context, Home homeModel) {
        this.context = context;
        this.homeModel = homeModel;
    }
 @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == TYPE_HEADER) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.homefragment_header, parent, false);
            return new HeaderViewHolder(v);
        } else if (viewType == TYPE_ITEM) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_homefragment, parent, false);
            return new ItemViewHolder(v);
        } else if (viewType == PRODUCT_ITEM) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_productlines, parent, false);
            return new ProductLinesItemHolder(v);
        } else if (viewType == SHOPS_ITEM) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_productlines, parent, false);
            return new ShopsOfWeekItemHolder(v);
        }
        return null;
    } 
@Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (position == 0) {
            if (holder instanceof HeaderViewHolder) {
              // load data in slider
            }
        } else if (holder instanceof ItemViewHolder) {
            ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
            if (homeModel.getCategories().get(0) != null) {
                if (position == 1) {
                     //load data in category recyclerview
                    holder.setIsRecyclable(false);
                }
            }
        } else if (holder instanceof ProductLinesItemHolder) {
            ProductLinesItemHolder itemViewHolder = (ProductLinesItemHolder) holder;
            if (homeModel.getProductLines() != null) {
                if (position > 1 && pos <= homeModel.getProductLines().size()-1) {
                    Log.d("HomeRecycler","pos:"+pos);
                    Log.d("HomeRecycler","position:"+ (position-2));
                        setRecyclerView(2, itemViewHolder);
                        Log.d("HomeRecycler","pos:"+homeModel.getProductLines().get(pos).getName());
                        itemViewHolder.txtProductName.setText(homeModel.getProductLines().get(pos).getName());
                        itemViewHolder.txtViewAllShops.setVisibility(View.GONE);
                        populateProductLinesItem(itemViewHolder, homeModel.getProductLines().get(pos));
                        pos = pos + 1;

                }
            }
        }
private void populateProductLinesItem(ProductLinesItemHolder itemViewHolder, ProductLines productLines) {
        ProductLinesItemAdapter productLinesItemAdapter = new ProductLinesItemAdapter(context, productLines.getItems(), clickEventOfProductsItemListener);
        itemViewHolder.prodLinesRecyclerView.setAdapter(productLinesItemAdapter);
        productLinesItemAdapter.notifyDataSetChanged();
    }

有人可以建议吗?如何避免这个问题? 更多代码:https://gist.github.com/anonymous/7785cdd97b5c27886c81f321c8965930

【问题讨论】:

    标签: android scroll android-recyclerview


    【解决方案1】:

    基于此:

    但是当我从下到上滚动 recyclerview 时,所有数据及其位置都混乱了。

    有一个类似的问题,这里是SO Post

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-23
      • 1970-01-01
      • 2021-03-25
      • 1970-01-01
      • 2021-02-06
      • 2022-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多