【问题标题】:How Create a CardView inside a recycleView?如何在 recycleView 中创建 CardView?
【发布时间】:2021-03-13 15:13:08
【问题描述】:

我正在开发一个简单的应用程序,我想在主视图中显示一个卡片列表,其中显示有关提供者的不同信息。

示例如下:

所以我想做一个 RecycleView,由 CardView 项组成,但是当我尝试实现它时,我意识到我不知道 onBindViewHolder() 方法的作用。

另一个问题是如何根据 CardsView 项目创建回收视图。

这是我的代码的一部分:

MyAdapter.class

package com.example.apaados;

import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    private String[] mDataset;

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class MyViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public CardView textView;
        public MyViewHolder(CardView v) {
            super(v);
            textView = v;
        }
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public MyAdapter(String[] myDataset) {
        mDataset = myDataset;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
                                                     int viewType) {
        // create a new view
        CardView v = (CardView) LayoutInflater.from(parent.getContext())
                .inflate(R.layout.card_view_main_view, parent, false);

        MyViewHolder vh = new MyViewHolder(v);
        return vh;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        /****** I don't know what i have to do here.*************/
holder.itemView.
        

    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.length;
    }
}


recycleView.xml

<?xml version="1.0" encoding="utf-8"?>
<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:layout_height="match_parent"
    tools:context=".MainView">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycleView"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout_editor_absoluteX="174dp"
        tools:layout_editor_absoluteY="152dp" >

        <androidx.cardview.widget.CardView

            <!--  HERE i WOULD LIKE TO RECOVER THE XML OF MY CARD VIEW -->
            android:layout_width="match_parent"
            android:layout_height="wrap_content"



    </androidx.recyclerview.widget.RecyclerView>
</androidx.constraintlayout.widget.ConstraintLayout>


这是我的 CardView xml:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:padding="16dp"
    >

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/cv"
        >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp"
            >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/photo_card"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:layout_marginRight="16dp"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/txtTitulo_Card"
                android:layout_toRightOf="@+id/txtDescripcion_Card"
                android:layout_alignParentTop="true"
                android:textSize="30sp"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/txtDescripcion_Card"
                android:layout_toRightOf="@+id/photo_card"
                android:layout_below="@+id/txtTitulo_Card"
                />

        </RelativeLayout>

    </androidx.cardview.widget.CardView>

</LinearLayout>

如果您知道在 RecycleView 中实现 CardView 项目的一些示例,感谢您的提前!

【问题讨论】:

  • 这与任何其他 recyclerview 实现没有技术差异。只需查找基本的 recyclerview 教程或文档,然后使用卡片视图作为查看器(就像您已经开始做的那样)
  • OKey,对,我应该在 onBindViewHolder() 中调用哪个方法,调用“holder.itemView.........”
  • 每个RecyclerView 都有一个适配器和一个list_item 布局,该布局被填充以显示多次,在您的情况下为CardViewonBindViewHolder() Adapter 类中的 onBindViewHolder() 根据数组列表在该位置的位置和数据填充您的每个 CardView,因此,它提供了一个参数 position,当它与该卡的视图 holder 结合时,您创建CardView 或任何其他Layout 的列表。因此,在MyViewHolder 类中声明您的视图,在onBindViewHolder() 中访问它们,如holder.textView 并设置其值或onClickListener()

标签: java android android-recyclerview android-cardview


【解决方案1】:

您必须在 cardview 文件中替换此代码

<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/cv"
    android:layout_margin="16dp"
    app:cardCornerRadius="5dp">

【讨论】:

    猜你喜欢
    • 2015-04-04
    • 2020-12-24
    • 1970-01-01
    • 1970-01-01
    • 2021-03-26
    • 2020-12-27
    • 1970-01-01
    • 2015-04-06
    • 1970-01-01
    相关资源
    最近更新 更多