RecyclerView आइटम्स में CardView - मटेरियल कार्ड्स
CardView (मटेरियल कंपोनेंट्स) एक फ्रेम लेआउट है जिसमें गोल कोने, एलिवेशन शैडो, और वैकल्पिक स्ट्रोक होता है। यह RecyclerView आइटम्स के लिए मानक कंटेनर है।
निर्भरताएं
dependencies {
implementation "androidx.recyclerview:recyclerview:1.3.2"
implementation "com.google.android.material:material:1.11.0"
}
आइटम लेआउट (res/layout/item_card.xml)
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
app:cardCornerRadius="12dp"
app:cardElevation="4dp"
app:cardUseCompatPadding="true"
app:strokeWidth="1dp"
app:strokeColor="@color/card_stroke">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<ImageView
android:id="@+id/ivThumbnail"
android:layout_width="match_parent"
android:layout_height="160dp"
android:scaleType="centerCrop"
android:src="@drawable/placeholder"
android:contentDescription="@string/image_desc" />
<TextView
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="कार्ड शीर्षक"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tvDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="विवरण पाठ यहां जाता है"
android:textSize="14sp"
android:textColor="#757575" />
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
मुख्य MaterialCardView विशेषताएं
| विशेषता | विवरण |
|---|---|
cardCornerRadius | कोने का त्रिज्या (dp) |
cardElevation | शैडो गहराई (dp) |
cardUseCompatPadding | प्री-लॉलीपॉप पर पैडिंग जोड़ता है ताकि शैडो क्लिप न हो |
strokeWidth / strokeColor | आउटलाइन बॉर्डर (API 21+) |
cardBackgroundColor | पृष्ठभूमि रंग (थीम-संवेदनशील) |
checkedIcon / checkable | चयन योग्य कार्ड्स के लिए |
एडॉप्टर (ListAdapter + DiffUtil)
public class CardAdapter extends ListAdapter{ public CardAdapter() { super(DIFF); } private static final DiffUtil.ItemCallback DIFF = new DiffUtil.ItemCallback () { @Override public boolean areItemsTheSame(@NonNull CardItem o, @NonNull CardItem n) { return o.id == n.id; } @Override public boolean areContentsTheSame(@NonNull CardItem o, @NonNull CardItem n) { return o.title.equals(n.title) && o.desc.equals(n.desc); } }; @NonNull @Override public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_card, parent, false); return new VH(v); } @Override public void onBindViewHolder(@NonNull VH h, int pos) { h.bind(getItem(pos)); } static class VH extends RecyclerView.ViewHolder { ImageView iv; TextView tvTitle, tvDesc; VH(View v) { super(v); iv = v.findViewById(R.id.ivThumbnail); tvTitle = v.findViewById(R.id.tvTitle); tvDesc = v.findViewById(R.id.tvDescription); } void bind(CardItem item) { tvTitle.setText(item.title); tvDesc.setText(item.desc); iv.setImageResource(item.imageRes); } } }
गतिविधि सेटअप
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView rv = findViewById(R.id.recyclerView);
rv.setLayoutManager(new LinearLayoutManager(this));
rv.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
CardAdapter adapter = new CardAdapter();
rv.setAdapter(adapter);
List items = List.of(
new CardItem(1, "पहला कार्ड", "विवरण एक", R.drawable.img1),
new CardItem(2, "दूसरा कार्ड", "विवरण दो", R.drawable.img2),
new CardItem(3, "तीसरा कार्ड", "विवरण तीन", R.drawable.img3)
);
adapter.submitList(items);
}
}
उन्नत: क्लिक करने योग्य/चयन योग्य कार्ड्स
// लेआउट में:
app:checkable="true"
app:checkedIcon="@drawable/ic_check"
// एडॉप्टर में:
holder.itemView.setOnClickListener(v -> {
int pos = holder.getBindingAdapterPosition();
if (pos != RecyclerView.NO_POSITION) {
CardItem item = getItem(pos);
item.selected = !item.selected;
notifyItemChanged(pos, "selection"); // पेलोड के साथ आंशिक अपडेट
}
});
@Override
public void onBindViewHolder(@NonNull VH h, int pos, @NonNull List
सर्वोत्तम अभ्यास
- MaterialCardView का उपयोग करें (androidx.cardview नहीं) — मटेरियल थीमिंग, स्ट्रोक, चेक करने योग्य स्थिति का समर्थन करता है।
- cardUseCompatPadding="true" सेट करें सभी API स्तरों पर सुसंगत शैडो के लिए।
- ItemDecoration के साथ मार्जिन जोड़ें, कार्ड लेआउट में नहीं, ताकि स्पेसिंग RecyclerView द्वारा संभाली जाए।
- बड़ी इमेज के लिए Glide/Coil का उपयोग करें जिसमें
override()हो। - एनिमेशन: ListAdapter स्वचालित रूप से सम्मिलन/हटाने/चाल को एनिमेट करता है।
मूल उदाहरण की समीक्षा
मूल कोड लिगेसी android.support.v7.widget.CardView, हार्डकोडेड रंग, कोई स्ट्रोक/चेक करने योग्य समर्थन नहीं, और मैन्युअल notifyDataSetChanged का उपयोग करता था। ऊपर दिया गया संस्करण MaterialCardView, ListAdapter/DiffUtil, और आधुनिक मटेरियल थीमिंग का उपयोग करता है।