StaggeredGridLayoutManager减少RecyclerView套嵌RecyclerView

      在实际开发中,有时候遇到复杂的列表布局,往往会涉及到即有竖向列表,又有瀑布流一起滑动,往往会想到,用两个RecyclerView进行套嵌,这样不是不可以,但是性能不是很好,RecyclerView套嵌RecyclerView会滑动的时候会卡顿。其实用一个RecyclerView就可以,使用StaggeredGridLayoutManager作为管理器,如果某些Item需要水平撑满,则需要在该item的ViewHolder中的onBindViewHolder如下设置:

 ViewGroup.LayoutParams lp = viewHolder.itemView.getLayoutParams();
        if (lp instanceof StaggeredGridLayoutManager.LayoutParams) {
            ((StaggeredGridLayoutManager.LayoutParams) lp).setFullSpan(true);
        }
设置setFullSpan,其解释如下:
 /**
         * When set to true, the item will layout using all span area. That means, if orientation
         * is vertical, the view will have full width; if orientation is horizontal, the view will
         * have full height.
         *
         * @param fullSpan True if this item should traverse all spans.
         * @see #isFullSpan()
         */
        public void setFullSpan(boolean fullSpan) {
            mFullSpan = fullSpan;
        }

如果设置为true,item将撑满整个区域,这意味着,如果是垂直分部,则垂直方向会撑满,如果是水平分部,则水平方向会撑满

遇坑一:

     但是,经过以上设置后,水平方向还是不能撑满,显示一半,后检查发现,item的根布局,不能在代码中动态设置LayoutParam,否则设置fullSpan无效

遇坑二:

     tem的根布局必须设置为如果是水平撑满layout_width = "match_parent"

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值