2

How can I display my recycleview items in a row while keeping their original height?

I want to use a recycleviewer with FlexboxLayoutManager. Every item normally having their own height depending on their content. However, every recycleview item on the same row always ending up with an equal height somehow.

How can I display my recycleview items in a row while respecting their original height?

I need the items to be displayed like this:

Correct one

What I tried without success:

Fault one (The small items always stretches)

My code where I set the layout manager:

        adapter = new ProductAdapter(getContext(), productsList);

        layoutManager.setFlexWrap(FlexWrap.WRAP);
        layoutManager.setFlexDirection(FlexDirection.ROW);
       // layoutManager.setAlignContent(AlignContent.CENTER);

        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);

I have tried this layoutManager.setAlignContent(AlignContent.CENTER); However you can't set alignContent in a FlexboxLayoutManager for a recycleviewer. The app crash because alignContent is for FlexboxLayout (not manager).

EDITED: When using a StaggeredGridLayoutManager , this will solve the problem. However you will lose the responsive behaviour of the FlexboxLayoutManager.

I created my own responsive behaviour for a StaggeredGridLayoutManager:

 DisplayMetrics displayMetrics = getActivity().getResources().getDisplayMetrics();
        float dpWidth = displayMetrics.widthPixels / displayMetrics.density;
        int spanCount = (int) (dpWidth / 222);  // 222=total space for 1 item incl. margin

            recyclerView.setLayoutManager(new StaggeredGridLayoutManager(spanCount, StaggeredGridLayoutManager.VERTICAL));
4

2 回答 2

4

尝试这样的事情作为你的RecyclerView布局管理器:

recycler.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
于 2018-09-21T06:38:42.823 回答
0

从你的形象我认为你需要的是StaggeredGridLayoutManager

于 2018-09-21T00:18:15.203 回答