Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wrap_content on recycleview, take height of item (horizontal list)

I have a RecycleView, set to horizontal. Now i would like the height to match the height of an item in the view. I don't know the height in advance.

Currently i have this:

Main fragment:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="horizontal"
        android:layout_marginLeft="1dp"
        android:layout_marginRight="1dp"
        android:layout_marginTop="1dp"
        android:layout_gravity="top"
        android:id="@+id/my_listview"/>

</RelativeLayout>


<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />

The image

<ImageView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/banner_image"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"
    android:background="@color/dark_gray"
    />

but it always takes up the entire screen. so i have a lot of whitespace in between. Is there a way that i could use wrap_content on the recycleview items?

This is what it currently looks like: enter image description here

I have placed 2 banners currently, the top one with the big whitespace and small image is the one with the recyclerview. The one below is just the image placed in an imageview (is what it should look like, but i want it with a recyclerview so i can scroll through the banners if i have multiple)

like image 723
Maxim Avatar asked Jan 31 '15 22:01

Maxim


1 Answers

Now i would like the height to match the height of an item in the view. I don't know the height in advance.

During layout pass, LinearLayoutManager cannot predict the height of all the views down in the adapter that are yet to be created, ahead of time.

You can measure() an item view, probably the first one, from the adapter and then set the height of RecycleView through code.

If all items can have different height then you'll have to measure them all, which is certainly not the correct way for a good UI.

like image 164
S.D. Avatar answered Oct 18 '22 02:10

S.D.