如何让GridLayoutManager使用动态空间? [重复]

问题描述 投票:0回答:3

这个问题在这里已有答案:

我目前正在使用GridLayoutManager作为RecyclerView,它显示不同文章的预览,这些文章具有不同的文本长度,因此具有不同的项目高度。问题是,如何使用全高度并且间距不要太大?

enter image description here

android android-layout android-gridlayout gridlayoutmanager
3个回答

0
投票

尝试为recyclerview设置这样的布局管理器。

adapter = new CustomAdapter(R.layout.layout, list, context);
final StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(numberOfrows, StaggeredGridLayoutManager.HORIZONTAL);
recyclerView_sk.setHasFixedSize(true);
recyclerView_sk.addItemDecoration(new SpacesItemDecorationNew(0));//For equal distribution of columns.
recyclerView_sk.setLayoutManager(layoutManager);
recyclerView_sk.setAdapter(adapter);

Space ItemDecoration新

public class SpacesItemDecorationNew extends RecyclerView.ItemDecoration {

    private int mItemOffset;

    public SpacesItemDecorationNew(int itemOffset) {
        mItemOffset = itemOffset;
    }

    public SpacesItemDecorationNew(@NonNull Context context, @DimenRes int itemOffsetId) {
        this(context.getResources().getDimensionPixelSize(itemOffsetId));
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
                               RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        outRect.set(mItemOffset, mItemOffset, mItemOffset, mItemOffset);
    }
}

0
投票

此外,你可以试试这个

        mDashboardAdapter = new DashboardAdapter(getActivity(), mDashBoardList, imageId);
        RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 4);
        grid.setLayoutManager(mLayoutManager);
        grid.addItemDecoration(new GridSpacingItemDecoration(4, 10, true));
        grid.setItemAnimator(new DefaultItemAnimator());
        grid.setAdapter(mDashboardAdapter);



    public static class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {

        private int spanCount;
        private int spacing;
        private boolean includeEdge;

        public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) {
            this.spanCount = spanCount;
            this.spacing = spacing;
            this.includeEdge = includeEdge;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            int position = parent.getChildAdapterPosition(view); // item position
            int column = position % spanCount; // item column

            if (includeEdge) {
                outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
                outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)

                if (position < spanCount) { // top edge
                    outRect.top = spacing;
                }
                outRect.bottom = spacing; // item bottom
            } else {
                outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
                outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f /    spanCount) * spacing)
                if (position >= spanCount) {
                    outRect.top = spacing; // item top
                }
            }
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.