android 在RecyclerView底部留一定空间的方法ItemDecoration

android 在RecyclerView底部留一定空间的方法ItemDecoration

1.StaggeredGridLayoutManager

 gridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
            float offset =100;//这里是你要在最后一个item底部留多少空间
            BottomOffsetDecoration bottomOffsetDecoration = new BottomOffsetDecoration((int) offset);
            recyclerView.addItemDecoration(bottomOffsetDecoration);
            recyclerView.setLayoutManager(gridLayoutManager);
            recyclerView.setAdapter(adapter);
            recyclerView.setItemAnimator(new DefaultItemAnimator());

BottomOffsetDecoration 继承自 RecyclerView.ItemDecoration,在里面判断了当前item是不是最后一个item,如果是就在它的下面加上一个offset,同理,可以在任何已知的item的周围加上offset从而实现特定的效果或需求,

static class BottomOffsetDecoration extends RecyclerView.ItemDecoration {
        private int mBottomOffset;

        public BottomOffsetDecoration(int bottomOffset) {
            mBottomOffset = bottomOffset;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            int dataSize = state.getItemCount();
            int position = parent.getChildAdapterPosition(view);
            StaggeredGridLayoutManager grid = (StaggeredGridLayoutManager) parent.getLayoutManager();
            if ((dataSize - position) <= grid.getSpanCount()) {
                outRect.set(0, 0, 0, mBottomOffset);
            } else {
                outRect.set(0, 0, 0, 0);
            }

        }
    }

2.如果是GridLayout就把StaggeredGridLayoutManager 相应替换就好了,LinearLayoutmanager判断方法有点不一样,其他都是一模一样

 int dataSize = state.getItemCount(); 
 int position = parent.getChildAdapterPosition(view); 
 if(dataSize> 0&& position == dataSize-1{
 outRect.set(000,mBottomOffset); 
} else {
 outRect.set(0000; 
} 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值