设置RecyclerView项目之间的间距

Simple ItemDecoration implementation for equal spaces between all items.

public class SpaceItemDecoration extends RecyclerView.ItemDecoration {
        int mSpace ;

        /** * @param space 传入的值,其单位视为dp */
        public SpaceItemDecoration(int space) {
            this.mSpace = UIUtil.dp2px(space);
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            int itemCount = mAdapter.getItemCount();
            int pos = parent.getChildAdapterPosition(view);
            Log.d(TAG, "itemCount>>" +itemCount + ";Position>>" + pos);

            outRect.left = 0;
            outRect.top = 0;
            outRect.bottom = 0;


            if (pos != (itemCount -1)) {
                outRect.right = mSpace;
            } else {
                outRect.right = 0;
            }
        }
    }

然后设置

recyclerView.addItemDecoration(new SpaceItemDecoration(5));

你可能感兴趣的:(android,divide,RecyclerVi)