I was developing an simple App, and in the main View I would like to show a list of a Cards, which display different information about the providers.
An example cuold be something like this:
So I would like to do is a RecycleView, made from CardView items, but when I try to implement this, I realize that I don't have any idea about what the method onBindViewHolder() do.
Another question would be how to create the recycle view based on the CardsView items.
Here it a part of my code:
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>
And this is my 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>
If you know some example of the implementation of a CardView item insede of RecycleView, take thanks for advance!
You have to replace this code in the cardview file
<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">
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.