Skip to content Skip to sidebar Skip to footer

Horizontal RecyclerView With Variable Item Heights Not Wrapping Properly

What I intend to achieve The item view should occupy the entire height of the item It could be that the item height is lesser than the height of the tallest item in the recyclerv

Solution 1:

I could not accept @Pradeep Kumar Kushwaha's answer because against one solution, I do not want different font sizes in the list. Consistency is a key element in design. Second alternative he gave couldn't work because with ellipsize I would need to give a "more" button of some sort for user to read the entire content and my text view is already taking a click action. Putting more some place else would again not be good design.

Changing the design with the simple compromise of resizing the recyclerview when the tallest, truncated item comes into focus, it turns into the simple use case of notifyItemChanged(). Even for the attempt I made using the view attached observer and scroll state listener, notifyItemChanged could be used but that approach is just too hacky. This I can live with in both code and design. Here goes the code required.

@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
    if (newState == RecyclerView.SCROLL_STATE_IDLE) {
        int position = ((LinearLayoutManager) binding.nextRv.getLayoutManager())
                .findFirstVisibleItemPosition();
        if (position != nextSnippetAdapter.getItemCount() - 1) {
            binding.nextRv.getAdapter().notifyItemRangeChanged(position, 2);
        } else {
            binding.nextRv.getAdapter().notifyItemChanged(position);
        }
    }
}

For my particular setup, calling for just these two elements works. It can further be optimized so as to call for single element at position + 1 in most cases, and checking and calling for the appropriate one in corner (literal) cases.


Solution 2:

Inside your adapter where I can find two cards one on top and another on bottom

How I would have defined my layout is like this:

Cardview1

    LinearLayout1 --> orientation vertical

    cardview2 (Top card where text is written)

    Linearlayout2 (where I can see icons such as like etc)-->orientation horizontal

Now fix the height of Linearlayout2 by setting it to wrap content.

And the height of cardview2 should be 0dp and add weight = 1

Now inside cardview2 add a TextView1 to matchparent in height and width.

Better inside textview1 add ellipsize to end and add max lines

If you want to show all lines try to find autoresizetextview library it can be founded here --> AutoResizeTextView

Hope it helps.


Solution 3:

I think the recyclerview can be set to height wrap_content. And the items can be make like height to match_parent.

<androidx.recyclerview.widget.RecyclerView
    android:layout_width="match_parent"
    android:layput_height="wrap_content"/>

Item as:

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   // your coode

</androidx.constraintlayout.widget.ConstraintLayout>

I had little more requirement than the question. Even my problem solved in the way.

Remember I am using:

androidx.recyclerview:recyclerview:1.0.0-beta01

dependency for the project


Post a Comment for "Horizontal RecyclerView With Variable Item Heights Not Wrapping Properly"