You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Android开发:RecyclerView搭配CardView无法运行,如何修复?

Fixing Your CardView for RecyclerView

Hey there! Let's fix your CardView layout to work properly with RecyclerView. I've spotted a few key issues in your code that are probably causing the problem:

Key Issues in Your Current Layout

  • CardView height set to match_parent: Each RecyclerView item will take up the entire screen height, which breaks the list behavior. It should be wrap_content to fit the item's content.
  • LinearLayout width set to 2000dp: This way-too-large width pushes content off-screen and breaks the layout flow. Change it to match_parent to fill the CardView.
  • No proper space distribution: The TextView and Button will overlap or get squeezed. We need to use layout_weight to let the TextView take up remaining space.
  • Redundant background: MaterialCardView already handles its own background, so android:background="@color/white" is unnecessary (you can use app:cardBackgroundColor="@color/white" instead if needed).

Fixed CardView Layout Code

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/list_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    app:cardCornerRadius="10dp"
    app:cardBackgroundColor="@color/white">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="16dp"> <!-- Added padding for better visual spacing -->

        <RadioButton
            android:id="@+id/ratio_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"/> <!-- Align with text vertically -->

        <TextView
            android:id="@+id/task"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_gravity="center_vertical"
            android:text="Do the work"
            android:textSize="20sp"/>

        <Button
            android:id="@+id/delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:backgroundTint="@color/red"
            android:text="Delete"
            android:visibility="invisible"/>
    </LinearLayout>
</com.google.android.material.card.MaterialCardView>

Additional RecyclerView Setup Checks

Even with a fixed item layout, RecyclerView won't work if these steps are missing:

  • Ensure your parent layout's RecyclerView has android:layout_width="match_parent" and android:layout_height="match_parent".
  • In your Activity/Fragment, set a LayoutManager to the RecyclerView (example code):
    // Kotlin example
    recyclerView.layoutManager = LinearLayoutManager(this)
    
    // Java example
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    
  • Verify your Adapter correctly inflates this CardView layout in onCreateViewHolder, and binds data to the views in onBindViewHolder.

内容的提问来源于stack exchange,提问作者Paraboy_toxic

火山引擎 最新活动