android - RecyclerView 包装其内容

标签 android android-fragments android-recyclerview android-linearlayout android-framelayout

我正在使用 RecyclerView 来膨胀线性垂直 ListView ,但是,尽 pipe View 的宽度设置为 match_parent,但 RecyclerView 在运行时会对其进行包装。

这是问题的屏幕截图:

enter image description here

item_recyclerView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">
        <TextView
            android:id="@+id/tag_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="0dp"
            android:maxLines="2"/>
        <TextView
            android:id="@+id/tag_role"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <com.google.android.material.imageview.ShapeableImageView
        android:id="@+id/user_pic"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_weight="0"
        android:rotation="-45"
        android:scaleType="centerCrop"
        android:src="@drawable/ic_user"
        app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.App.circleImageView" />

</LinearLayout>

最佳答案

膨胀布局文件时, Root View 上的 layout_ 属性将被忽略,除非您在 inflate() 中指定父 ViewGroup > 打电话。您目前没有这样做。

在您的 onCreateViewHolder() 方法中,替换以下内容:

ItemQuestionPostBinding binding = ItemQuestionPostBinding.inflate(inflater);

这样:

ItemQuestionPostBinding binding = ItemQuestionPostBinding.inflate(inflater, parent, false);

另请注意,您可能不希望使用 match_parent 作为 RecyclerView 项目的高度:

android:layout_width="match_parent"
android:layout_height="match_parent"

这将使 RecyclerView 中只有一个项目可见,因为每个项目都是屏幕的整个高度。目前这不是问题,原因与忽略 match_parent 宽度完全相同。

关于android - RecyclerView 包装其内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66617338/

相关文章:

android - 使用 ping 列出本地网络上的设备

android - 如何从一个 Activity 返回到一个 Fragment?

c# - 使用 View 寻呼机在 fragment 中选择的选项卡上重新加载数据

java - RecyclerView.onMeasure(...) 的空指针异常

java - 直接在指定位图下绘制文本

android - 更改初始屏幕图层列表中位图的大小

java - OpenGL ES 1.0 安卓 : Unable to load multiple textures

android - Mosby MVP - 如何在 onDestroyView() 之后重新创建 Fragment 的状态?

android - NativeAdsExpress 强制 RecyclerView 滚动以使 NativeAd 在首次加载时完全可见

Android PDF 从内部存储打开后立即关闭