我有一个 RecycleView,设置为水平。现在我希望高度与 View 中项目的高度相匹配。我事先不知道高度。
目前我有这个:
主要 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" />
图片
<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"
/>
但它总是占据整个屏幕。所以中间有很多空白。有没有一种方法可以在回收 View 项目上使用 wrap_content?
这是目前的样子:
我目前放置了 2 个横幅,上面那个空白大、图片小的是带有 recyclerview 的那个。下面的图片只是放置在 imageview 中的图片(它应该是这样的,但我希望它带有 recyclerview,这样如果我有多个横幅,我就可以滚动浏览横幅)
最佳答案
Now i would like the height to match the height of an item in the view. I don't know the height in advance.
在布局过程中,LinearLayoutManager
无法提前预测适配器中尚未创建的所有 View 的高度。
您可以从适配器measure()
一个项目 View ,可能是第一个,然后通过代码设置RecycleView
的高度。
如果所有项目都可以有不同的高度,那么你将不得不测量它们,这当然不是一个好的 UI 的正确方法。
关于android - recycleview 上的 wrap_content,获取项目的高度(水平列表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28257216/