Horizontal RecyclerView with variable element height

I have a horizontal oriented RecyclerView inside a ConstraintLayout as its direct child. Recycler matches the parent widht and sets its height according to the height of the position representation. Problem. Recycler cannot increase or decrease its height depending on the height of the element.

For example, the 3 visible elements at the top of the screen have a similar height of 100dp. Next, I scroll - and I see an element of height 200 dp. It will be a partial fir 100dp window, and the user will not see all the content.

Is anyone facing this issue?

+6
source share
1 answer

, LinearLayoutManager .

LinearLayoutManager layoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
layoutManager.setMeasurementCacheEnabled(false);

YourRecyclerView.setLayoutManager(layoutManager);
YourRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            layoutManager.requestLayout();
        }
    });
0

Source: https://habr.com/ru/post/1687642/


All Articles