【问题标题】:CardView inside RecyclerView scrolling verticallyRecyclerView中的CardView垂直滚动
【发布时间】:2020-08-22 17:58:21
【问题描述】:

我正在创建一个需要在 recyclerview 中垂直滚动的 cardview 的 android 应用程序。就像垂直滚动的多张卡片一样。但我不能在一个屏幕上获得超过一张卡。 我不知道我做错了什么。如何让卡片相邻滚动?

MainActivity 设置回收站视图布局

public class MainActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    CardView cardView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView=findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        CustomAdapter adapter =new CustomAdapter(this);



        LinearLayoutManager layoutManager =new LinearLayoutManager(this,LinearLayoutManager.VERTICAL ,false );
        recyclerView.setLayoutManager(layoutManager );
        recyclerView.setAdapter(adapter);
        recyclerView.setItemAnimator(new DefaultItemAnimator());


    }
}

Card_layout 是一个线性布局,卡片布局包含三个纯文本和一个按钮的线性布局。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/card"
        android:background="@color/card_bg"
        android:elevation="10dp"
        app:cardCornerRadius="10dp">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">


            <EditText
                android:id="@+id/editText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textPersonName"
                android:text="Name" />

            <EditText
                android:id="@+id/editText2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textPersonName"
                android:text="Name" />

            <EditText
                android:id="@+id/editText3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textPersonName"
                android:text="Name" />

            <Button
                android:id="@+id/button"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Button" />

        </LinearLayout>
    </androidx.cardview.widget.CardView>
</LinearLayout>

主要活动包含recyclerview。这是相对布局。 Adapter类在recyclerview中保存cardview布局

<androidx.constraintlayout.widget.ConstraintLayout 
    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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
         />

</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

  • @kashyap 我希望它并排垂直滚动。现在,它是一个屏幕上的一张卡片

标签: java android android-studio android-layout android-fragments


【解决方案1】:

在根View-Group LinearLayout 中将android:layout_height="match_parent" 更改为android:layout_height="wrap_content"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"

  // only change this below line
    android:layout_height="wrap_content">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/card"
        android:background="@color/card_bg"
        android:elevation="10dp"
        app:cardCornerRadius="10dp">


      //rest of the code here



    </androidx.cardview.widget.CardView>
</LinearLayout>

【讨论】:

    【解决方案2】:

    把你的xml代码改成这个

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/card"
            android:background="@color/card_bg"
            android:elevation="10dp"
            app:cardCornerRadius="10dp">
    
    
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical">
    
    
                <EditText
                    android:id="@+id/editText"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:inputType="textPersonName"
                    android:text="Name" />
    
                <EditText
                    android:id="@+id/editText2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:inputType="textPersonName"
                    android:text="Name" />
    
                <EditText
                    android:id="@+id/editText3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:inputType="textPersonName"
                    android:text="Name" />
    
                <Button
                    android:id="@+id/button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button" />
    
            </LinearLayout>
        </androidx.cardview.widget.CardView>
    </LinearLayout>
    

    使用 android:layout_width="match_parent" 是问题

    试试我发布的代码

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-29
      • 1970-01-01
      • 1970-01-01
      • 2017-05-02
      • 1970-01-01
      • 2016-10-26
      相关资源
      最近更新 更多