recyclerview嵌套的recyclerview notifyDataSetChanged错误

时间:2017-11-15 11:31:58

标签: android android-recyclerview notifydatasetchanged

enter image description here

enter image description here

像图片这样的问题,一些TextView在重用时不会刷新,日志显示没有调用onBindViewHolder方法。(问题如图,复用的时候有些TextView不会刷新,日志记录显示onBindViewHolder没有用) 虽然notifyItemRangeChanged可以解决问题,但我想知道为什么?(recyclelerview嵌套recyclelerview调用notifyItemRangeChanged的问题,虽然notifyItemRangeChanged可以解决,但是我不明白) 我来自中国,也是第一个帖子,希望能指点一下。英语是谷歌翻译,我希望说容易理解。

protected void initData() {
    mRv.setLayoutManager(new LinearLayoutManager(this));

    //init Data
    final ArrayList<ArrayList<Void>> mList = new ArrayList<>();
    for (int i = 0; i < 1000; i++) {
        ArrayList<Void> item = new ArrayList<>();
        //0
        //1  1
        //2  2  2
        //3
        //4  4
        //5  5  5
        //...
        switch (i % 3) {
            case 2:
                item.add(null);
            case 1:
                item.add(null);
            case 0:
                item.add(null);
                break;
            default:
                break;
        }
        mList.add(item);
    }

    //test
    mRv.setAdapter(new RecyclerView.Adapter<RecyclerView.ViewHolder>() {
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            RecyclerView itemView = new RecyclerView(RvTestActivity.this);
            itemView.setLayoutManager(new LinearLayoutManager(RvTestActivity.this, LinearLayoutManager.HORIZONTAL, false));
            //init ItemAdapter
            itemView.setAdapter(new ItemAdapter());
            return new RecyclerView.ViewHolder(itemView) {
            };
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            System.out.println("parent:" + position);
            //Reuse ItemAdapter
            final ItemAdapter adapter = (ItemAdapter) ((RecyclerView) holder.itemView).getAdapter();
            adapter.setListAndNotifyDataSetChanged(mList.get(position), position);

        }

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

private class ItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private int mParentPosition;
    private List<Void> mList;


    public void setListAndNotifyDataSetChanged(List<Void> list, int parentPosition) {
        mParentPosition = parentPosition;
        mList = list;
        notifyDataSetChanged();
        //Although notifyItemRangeChanged can solve the problem, but I wonder why?
        //notifyItemRangeChanged(0,list.size());
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        TextView tv = new TextView(RvTestActivity.this);
        tv.setTextSize(50);
        return new RecyclerView.ViewHolder(tv) {
        };
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((TextView) holder.itemView).setText(mParentPosition + "  ");

        //log
        if (mParentPosition % 3 == position) {
            System.out.println("P:" + mParentPosition + "  I:" + position + "  ");
        } else {
            System.out.print("P:" + mParentPosition + "  I:" + position + "  ");
        }
    }

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

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_rvtest_rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent/>
</RelativeLayout">

1 个答案:

答案 0 :(得分:0)

发现该错误已于今天修复,是recyclelerview版本的问题。 错误版本:com.android.support:recyclerview-v7:25.0.3 修正版:com.android.support:recyclerview-v7:27.0.1

虽然已经发布了很长时间,但它希望能够帮助其他人。