I have a RecyclerView
with a horizontal layout. The height of the individual adapter items can depend on the content of the item.
So in theory it can look like the following:
The problem I have now is that it wrap_content
only seems to care about the height of the first problem, because the result I am getting looks like this:
Where as you can see that the 4th item gets cut off. However, it works perfectly if I put the tallest item first.
And to get rid of the obvious solution; I don't know the height of the items. I could only do that during testing.
--
adapter_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="100dp" android:layout_height="wrap_content" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:animateLayoutChanges="true" android:padding="@dimen/padding_view_small"> <ImageView android:id="@+id/image" android:layout_width="50dp" android:layout_height="50dp" android:layout_gravity="center_horizontal" app:srcCompat="@drawable/flamme"/> <TextView android:id="@+id/name" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@color/black" android:layout_marginTop="@dimen/padding_view_small" android:text="@string/placeholder" android:gravity="center_horizontal"/> </LinearLayout>
parent.xml
<LinearLayout android:id="@+id/symbol_list_parent" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:background="@drawable/container_content" android:padding="@dimen/padding_view_normal"> <android.support.v7.widget.RecyclerView android:id="@+id/symbol_list" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"/> </LinearLayout>
Activity.java
... final ProductPictogramAdapter symbolAdapter = new ProductPictogramAdapter(); final View symbolListParent = findViewById(R.id.symbol_list_parent); RecyclerView symbolList = findViewById(R.id.symbol_list); symbolList.setLayoutManager(new LinearLayoutManager(ProductDetailActivity.this, LinearLayoutManager.HORIZONTAL, false)); symbolList.setAdapter(symbolAdapter); symbolList.setHasFixedSize(true);
solution: In the item layout xml, use RelativeLayout as root only and ensure the layout whose child view be dynamically added is surrounded with RelativeLayout too.
RecyclerView makes it easy to efficiently display large sets of data. You supply the data and define how each item looks, and the RecyclerView library dynamically creates the elements when they're needed. As the name implies, RecyclerView recycles those individual elements.
A RecyclerView is an advanced version of ListView with improved performance. When you have a long list of items to show you can use RecyclerView. It has the ability to reuse its views. In RecyclerView when the View goes out of the screen or not visible to the user it won't destroy it, it will reuse these views.
Since this question is getting quite a lot of traction and I only posted my personal solution in the comments I'll submit an answer as well.
--
I decided to use Google's Flexbox layout library to achieve what I wanted. It's very customizable and I would recommend it to people who are trying to solve similar problems.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With