如何在 recycleView 中创建 CardView?
How Create a CardView inside a recycleView?
我正在开发一个简单的应用程序,我想在主视图中显示一个卡片列表,其中显示有关提供商的不同信息。
一个例子可以是这样的:
所以我想做的是一个 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 项目的一些示例,请提前感谢!
您必须在 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">
我正在开发一个简单的应用程序,我想在主视图中显示一个卡片列表,其中显示有关提供商的不同信息。
一个例子可以是这样的:
所以我想做的是一个 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 项目的一些示例,请提前感谢!
您必须在 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">