【问题标题】:RecyclerView displaying only the first element in the Habits arrayRecyclerView 仅显示 Habits 数组中的第一个元素
【发布时间】:2021-11-02 11:43:19
【问题描述】:

我是一名 Android Dev 初学者,无法让卡片布局显示所有习惯。我尝试交换步行和水,但我只能步行。

我是否遗漏了某些内容或者某些内容不正确? HabitsAdapter 类

    class HabitAdapter(private val habits: List<Habit>) :
    RecyclerView.Adapter<HabitAdapter.HabitViewHolder>() {

    class HabitViewHolder(val card: View) :
        RecyclerView.ViewHolder(card) {
        fun bind(habit: Habit) {
            card.findViewById<TextView>(R.id.text_view_title).text = habit.title
            card.findViewById<TextView>(R.id.text_view_description).text =
                habit.description
            card.findViewById<ImageView>(R.id.image_view_icon)
                .setImageResource(habit.image)

        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HabitViewHolder {
        return HabitViewHolder(
            LayoutInflater.from(parent.context).inflate(R.layout.single_item, parent, false)
        )
    }

    override fun onBindViewHolder(holder: HabitViewHolder, position: Int) {
        holder.bind(habits[position])

    }


    override fun getItemCount(): Int = habits.size
}

习惯类

    data class Habit(val title: String, val description: String, val image: Int)

fun getSampleHabits(): List<Habit> {
    return listOf(
        Habit(
            "Drink Water",
            "A refreshing glass of water gets you ready for a long day ahead",
            R.drawable.water
        ),
        Habit(
            "Go for a walk",
            "A nice walk helps clear your mind for a long day ahead",
            R.drawable.walk
        )
    )
}

MainActivity 类

    class MainActivity : AppCompatActivity() {
    private lateinit var mainBinding: ActivityMainBinding
    private lateinit var itemBinding: SingleItemBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mainBinding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(mainBinding.root)

        mainBinding.recyclerViewList.layoutManager = LinearLayoutManager(this)
        mainBinding.recyclerViewList.adapter = HabitAdapter(getSampleHabits())

    }
}

MainActivity XML

<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    tools:listitem="@layout/single_item"
    android:id="@+id/recycler_view_list"/>

包含习惯内容(即标题、描述和图标)的单张卡片的 XML 布局文件

<ScrollView android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="12dp"
        app:cardCornerRadius="12dp">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/image_view_icon"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_margin="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginBottom="8dp"
                android:scaleType="centerCrop"
                tools:src="@drawable/water" />

            <TextView
                android:id="@+id/text_view_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginTop="12dp"
                android:layout_toEndOf="@id/image_view_icon"
                android:textSize="28sp"
                tools:text="Drink Water" />
            <TextView
                android:id="@+id/text_view_description"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                tools:text="Drink Water"
                android:layout_toEndOf="@id/image_view_icon"
                android:layout_below="@id/text_view_title"
                android:layout_marginStart="8dp"/>

        </RelativeLayout>
    </androidx.cardview.widget.CardView>
</ScrollView>

【问题讨论】:

  • 包括 XML 代码。
  • 在帖子中添加了 xml。我听从了你的建议,但无济于事。
  • 每个项目的父 ViewGroupScrollView。这绝对不是你想要的。尝试删除它并将 CardView 改为父级。

标签: android android-studio kotlin android-recyclerview


【解决方案1】:

这就是你的 single_item.xml 的样子。由于您将 ScrollView 的高度保持为匹配父级,因此它占据了整个视图并且没有地方可以回收,因此使其包装内容可以解决问题。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="12dp"
        app:cardCornerRadius="12dp">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/image_view_icon"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_margin="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginBottom="8dp"
                android:scaleType="centerCrop" />

            <TextView
                android:id="@+id/text_view_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginTop="12dp"
                android:layout_toEndOf="@id/image_view_icon"
                android:textSize="28sp"
                tools:text="Drink Water" />
            <TextView
                android:id="@+id/text_view_description"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                tools:text="Drink Water"
                android:layout_toEndOf="@id/image_view_icon"
                android:layout_below="@id/text_view_title"
                android:layout_marginStart="8dp"/>

        </RelativeLayout>
    </androidx.cardview.widget.CardView>
</ScrollView>
```

【讨论】:

  • 这就是为什么只有一件物品可见的原因。在您之前取消勾选的问题中,我的适配器没有问题。也不推荐使用 ScrollView 作为父级,使用约束/线性/相对布局
【解决方案2】:

检查 single_item 布局,它的高度可能是 match_parent,这可能会导致问题。

【讨论】:

    最近更新 更多