美文网首页
RecyclerView动态设置间隔

RecyclerView动态设置间隔

作者: 徐子念 | 来源:发表于2021-04-21 21:28 被阅读0次

    首先需要用到自定义一个间距的speace

    public class SpacesItemDecoration extends RecyclerView.ItemDecoration{

    private int space;

    public SpacesItemDecoration(int space){

    this.space = space;

        }

    @Override

    public void getItemOffsets(Rect outRect, View view,

                                  RecyclerView parent, RecyclerView.State state){

            outRect.right = space;

    // Add top margin only for the first item to avoid double space between items

    if(parent.getChildPosition(view) ==0)

                outRect.left = space;

        }

    }

    然后自定义好之后我们来设置到我们的recyclerView上。

    ```

    int space = UIUtils.dip2Px(15);

    recyclerview.addItemDecoration(new SpacesItemDecoration(space));

    此处附加UIUtils.dip2Px方法

    public static int dip2Px(int dip){

    float density = getResource().getDisplayMetrics().density;

    int px = (int) ((float) dip * density +0.5F);

    return px;

        }

    到此我们就实现动态设置间隔了

    相关文章

      网友评论

          本文标题:RecyclerView动态设置间隔

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