欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

StaggeredGridLayoutManager不规则Item 高度间隔问题

程序员文章站 2022-03-12 07:52:25
分割线public class MyItemDecoration extends RecyclerView.ItemDecoration { private final int spec; public MyItemDecoration(Context context, int dpValue) { spec = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, context....

分割线


public class MyItemDecoration extends RecyclerView.ItemDecoration {

    private final int spec;

    public MyItemDecoration(Context context, int dpValue) {
        spec = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, context.getResources().getDisplayMetrics());
    }

    @Override
    public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
    }


    @Override
    public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
    }


    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {

        outRect.set(spec, spec, 0, 0);
    }
}



 StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, RecyclerView.VERTICAL);
        recyclerView.addItemDecoration(new MyItemDecoration(this,10));
        recyclerView.setLayoutManager(layoutManager);

布局:

 <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/dp_10"
        android:layout_marginRight="@dimen/dp_20"
        android:layout_weight="1"/>

本文地址:https://blog.csdn.net/qq_37382732/article/details/108583812

相关标签: Android