How to make GridLayoutManager use dynamic spaces? [duplicate]

痴心易碎 提交于 2019-12-11 18:55:53

问题


I am currently using a GridLayoutManager for a RecyclerView which displays Previews of different Articles, which have different text lengths and therefor different item heights. The question is, how can I use the full height and dont have too much spacing?


回答1:


Use StaggeredGridLayoutManager! https://developer.android.com/reference/android/support/v7/widget/StaggeredGridLayoutManager




回答2:


Try to set your layout manager like this for 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);

SpacesItemDecorationNew

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);
    }
}



回答3:


Also, you can try with this

        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
                }
            }
        }
    }


来源:https://stackoverflow.com/questions/55630139/how-to-make-gridlayoutmanager-use-dynamic-spaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!