美文网首页
recyclerview的item之间的间距

recyclerview的item之间的间距

作者: 名字_都被占了 | 来源:发表于2018-09-01 19:17 被阅读0次

    已记录

    public class RecyclerViewSpacesItemDecoration extends RecyclerView.ItemDecoration {
     
       private HashMap<String, Integer> mSpaceValueMap;
     
       public static final String TOP_DECORATION = "top_decoration";
       public static final String BOTTOM_DECORATION = "bottom_decoration";
       public static final String LEFT_DECORATION = "left_decoration";
       public static final String RIGHT_DECORATION = "right_decoration";
    ​
       public RecyclerViewSpacesItemDecoration(HashMap<String, Integer> mSpaceValueMap) {
           this.mSpaceValueMap = mSpaceValueMap;
       }
    ​
       @Override
       public void getItemOffsets(Rect outRect, View view,
                                   RecyclerView parent, RecyclerView.State state) {
           if (mSpaceValueMap.get(TOP_DECORATION) != null)
               outRect.top = mSpaceValueMap.get(TOP_DECORATION);
           if (mSpaceValueMap.get(LEFT_DECORATION) != null)
               outRect.left = mSpaceValueMap.get(LEFT_DECORATION);
           if (mSpaceValueMap.get(RIGHT_DECORATION) != null)
               outRect.right = mSpaceValueMap.get(RIGHT_DECORATION);
           if (mSpaceValueMap.get(BOTTOM_DECORATION) != null)
               outRect.bottom = mSpaceValueMap.get(BOTTOM_DECORATION);
       }
    }
    

    调用即可

    HashMap<String, Integer> stringIntegerHashMap = new HashMap<>();
    stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.TOP_DECORATION,50);//top间距
     
    stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.BOTTOM_DECORATION,100);//底部间距
     
    stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.LEFT_DECORATION,50);//左间距
     
    stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.RIGHT_DECORATION,100);//右间距
     
    mRecyclerView.addItemDecoration(newRecyclerViewSpacesItemDecoration(stringIntegerHashMap));
    

    参考文章:

    https://blog.csdn.net/yangbin0513/article/details/71515893

    相关文章

      网友评论

          本文标题:recyclerview的item之间的间距

          本文链接:https://www.haomeiwen.com/subject/iibuwftx.html