Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Horizontal RecyclerView wih variable item height

I have a horizontal oriented RecyclerView inside ConstraintLayout as its direct child. Recycler matches parent's widht, and sets its height accordingly to item's view height. The problem - Recycler can't increase or decrease its height depending on item's view height.

For example, 3 visible items on start of screen has similar 100dp height. Next, i scroll - and see 200 dp height item. It will partialy fir 100dp window and user won't see all content.

Does anyone face wiht this problem?

like image 682
Евгений Кравцов Avatar asked Oct 17 '17 13:10

Евгений Кравцов


1 Answers

Maybe you should set measurement cache disable with your LinearLayoutManager and request layout after scrolled.

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();
        }
    });
like image 67
brad.lan Avatar answered Nov 15 '22 16:11

brad.lan