RecyclerView 中的item来回滑动会越来越小

2023-02-21  本文已影响0人  因为我的心

一、前言:

我的页面是RecyclerView1 (ItemTypeProvider1)嵌套RecyclerView2。由于RecyclerView2中添加了item的间隙,导致每次复用的时候都会重复添加item的间隙。

图片.png

二、解决:

1、全部代码:

注意:一定要没有添加过间隙,才去添加,否则会重复添加,导致在RecyclerView中的子item,会越来越小。

//没有添加过再去添加
if (itemDecorationCount<=0){
      //设置上方和右方
     addItemDecoration(SpacesItemDecoration(15))
}
查看源码发现addItemDecoration维护的是一个ArrayList间隙列表:
 final ArrayList<ItemDecoration> mItemDecorations = new ArrayList<>();
 public void addItemDecoration(@NonNull ItemDecoration decor, int index) {
        if (mLayout != null) {
            mLayout.assertNotInLayoutOrScroll("Cannot add item decoration during a scroll  or"
                    + " layout");
        }
        if (mItemDecorations.isEmpty()) {
            setWillNotDraw(false);
        }
        if (index < 0) {
            mItemDecorations.add(decor);
        } else {
            mItemDecorations.add(index, decor);
        }
        markItemDecorInsetsDirty();
        requestLayout();
    }

2、全部代码:

/**
 * 男频女频-卡片类型1
 */
class ItemTypeProvider1 :
    BaseItemProvider<BaseProviderMultiBean>() {
    override val itemViewType: Int
        get() = HOME_TYPE1
    override val layoutId: Int
        get() = R.layout.item_home_type1

    override fun convert(helper: BaseViewHolder, item: BaseProviderMultiBean) {

        val tvTitle = helper.getView<TextView>(R.id.tv_title)
        val rvList = helper.getView<RecyclerView>(R.id.rv_list)
        if (item is NewBaseLabelBean){
            val label = item.label //推荐位名称
            val list = item.list?.take(3)  //下方数组
            tvTitle.text = label

            val madapter = BookItemType1Adapter()
            rvList.apply {
                val linearLayoutManager = GridLayoutManager(context,3)
                linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
                //没有添加过再去添加
                if (itemDecorationCount<=0){
                    //设置上方和右方
                    addItemDecoration(SpacesItemDecoration(15))
                }
                //设置左侧距离
                setPadding(15.dp, 0, 0, 0)
                layoutManager = linearLayoutManager
                adapter = madapter
                if (list != null) {
                    madapter.data = list as MutableList<BaseBookComic>
                }
            }

            //添加点击事件
            madapter.addChildClickViewIds(R.id.cl_item_type1)
            madapter.setOnItemChildClickListener { adapter,view,position ->
                val bean = list?.get(position)
                val bookId = bean?.book_id// 跳转类型对应的地址//推荐位列表
                when(view.id){
                    R.id.cl_item_type1 ->{
                       //打开书籍
                       HomeUtils.openBookDetail(context,bookId.toString(),1,0)
                    }
                }
            }
        }
    }
}

3、item添加间距

/**
 * GridLayoutManager 设置上右间距一样
 */
class SpacesItemDecoration(val space: Int) : RecyclerView.ItemDecoration() {
    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
//        if (parent.layoutManager is GridLayoutManager) {
//            outRect.top = space.dp
//            outRect.right = space.dp
//        }
        outRect.top = space.dp
        outRect.right = space.dp
    }
}
上一篇 下一篇

猜你喜欢

热点阅读