android - 水平 RecylerView 多行显示不正确

标签 android android-layout android-fragments android-recyclerview horizontal-scrolling

我正在使用包含多行项目的Horizo​​ntal RecyclerView。我已使用 FlexboxLayoutManager 将所有项目拆分为多行,但 View 未按我想要的方式创建。它自动削减TextView的字符数

我想要如下图所示的 View

enter image description here

我的代码:

XML:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="@dimen/dp4"
android:layout_marginBottom="@dimen/dp4">


<TextView
    android:id="@+id/btnProduct"
    android:layout_width="wrap_content"
    android:layout_height="@dimen/dp30"
    android:layout_gravity="center"
    android:background="@drawable/bg_white_box"
    android:gravity="center"
    android:maxLines="1"
    android:padding="@dimen/dp5"
    android:singleLine="true"
    android:text="Denim Jeans Outfit goes"
    android:textAlignment="center"
    android:textAllCaps="false" />

代码:

    RecommendProductAdapter recommendProductAdapter = new RecommendProductAdapter(getActivity(), recommendedProductList, SearchProductFragment.this);
                                FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(getActivity());
                                layoutManager.setFlexDirection(FlexDirection.ROW);
                                layoutManager.setJustifyContent(JustifyContent.FLEX_START);
                                listRecommend.setLayoutManager(layoutManager);
                                listRecommend.setAdapter(recommendProductAdapter);

我没有指定 4 列,但每次都会得到 4 列。

如果我将 TextView 的 宽度设置为“wrap_content”,则下图。我也没有指定字符长度,但它会将我的文本剪切为...。我想要全文。

enter image description here

如果我将 TextView 的宽度指定为“176dp”,那么下面是输出

enter image description here

请帮忙做点什么。

最佳答案

您必须将flex-wrap设置为wrap

This attribute controls whether the flex container is single-line or multi-line, and the direction of the cross axis. Possible values are:

  • nowrap(默认)
  • 换行
  • wrap_reverse

关于android - 水平 RecylerView 多行显示不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59767111/

相关文章:

Android Studio 正在为 3 个月大的 react-native 项目生成非常旧的 apk

android - 设置 Android PreferenceFragment 的正确方法是什么?

android - Gridview 图像在滚动时更改/随机播放

android - 布局底部的 ListView

android - 选择项目后,微调器在布局中向下移动

android - 在 viewPager 中的 fragment 之间滑动时,选项卡高光不会改变

android - RecylerView 上的 OnclickListener 并获取点击项目的 Id

缺少 Android Intent 参数 FLAG_ACTIVITY_SINGLE_TASK?

java - 无法解析 TabHost 方法上的符号 android

android - 在 ViewPager 中重新加载 fragment 时出现问题