CymChad / BaseRecyclerViewAdapterHelper

BRVAH:Powerful and flexible RecyclerAdapter
http://www.recyclerview.org/
MIT License
24.33k stars 5.16k forks source link

双层recyclerview嵌套,第1层竖向,第2层横向,添加ItemDecoration,滚动会造成间隔宽度累加 #1328

Closed lyq027 closed 7 years ago

lyq027 commented 7 years ago

双层recyclerview嵌套,第1层竖向,第2层横向,添加ItemDecoration,设置getItemOffsets的间隔,滚动会造成宽度累加。如果不使用框架,自已写adapter,将第2次的recyclerview的创建及ItemDecoration写在onCreateViewHolder,就不会宽度累加。

综上,如何操作,才能在使用框架的基础上,不造成ItemDecoration间隔宽度累加呢?

` recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() { @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { super.getItemOffsets(outRect, view, parent, state);

                    if(parent.getChildAdapterPosition(view)!=0){
                        outRect.left = 80;
                    }
                }
            });`
lyq027 commented 7 years ago

在官方demo的ItemClickAdapter中,在ClickEntity.NEST_CLICK_ITEM_CHILD_VIEW内加上上面代码,将recyclerview改成LinearLayoutManager.HORIZONTAL,然后多添加几个ClickEntity.NEST_CLICK_ITEM_CHILD_VIEW,上下滚动就会重现

FrankKwok commented 7 years ago

在convert里先removeItemDecoration再addItemDecoration

FrankKwok commented 7 years ago

需要使用同一个ItemDecoration实例

FrankKwok commented 7 years ago

在外层Adapter构造函数声明

DividerItemDecoration mDividerItemDecoration;

在convert里这么写就好了

if (mDividerItemDecoration == null) {
                    mDividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.HORIZONTAL);
                }
                recyclerView.removeItemDecoration(mDividerItemDecoration);
                recyclerView.addItemDecoration(mDividerItemDecoration);
lyq027 commented 7 years ago

谢谢,暂时这样处理可以实现。但是感觉不够优雅。希望作者能提供一个在adapter中初始化控件的方法或告知如何避免在convert里创建控件

CymChad commented 7 years ago

ItemDecoration和adapter无直接关系,它直接关联对象的是recyclerView。convert里创建控件完全由开发者控制。