如何在recycleview中创建cardview?

qnyhuwrf  于 2021-07-07  发布在  Java
关注(0)|答案(1)|浏览(312)

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

所以我想做的是一个recycleview,由cardview项生成,但是当我尝试实现它时,我意识到我不知道onbindviewholder()方法做什么。
另一个问题是如何基于cardsview项创建recycle视图。
这是我代码的一部分:
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>

如果你知道一些cardview项目在recycleview中的实现示例,请提前感谢!

cld4siwp

cld4siwp1#

您必须在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">

相关问题