水平RecyclerView跟随第一个项目的高度,包含wrap_content [英] Horizontal RecyclerView follows first item height with wrap_content

查看:153
本文介绍了水平RecyclerView跟随第一个项目的高度,包含wrap_content的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个水平布局的RecyclerView.单个适配器项目的高度取决于项目的内容.

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:

我现在遇到的问题是它wrap_content似乎只关心第一个问题的高度,因为我得到的结果看起来像这样:

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:

您会在其中看到第4个项目被截断.但是,如果我把最高的物品放在第一位,它就可以很好地工作.

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

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

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

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);

推荐答案

由于这个问题引起了很大的关注,我只在评论中发表了自己的个人解决方案,我也将提交答案.

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.

-

我决定使用 Google的Flexbox布局库来实现我想要的功能.它是非常可定制的,我会推荐给尝试解决类似问题的人.

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.

这篇关于水平RecyclerView跟随第一个项目的高度,包含wrap_content的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆