Recyclerview + Cardview Illustration Alongside Imageview – Listing Available Drawable

Posted on

Last post service exhibit how to “get all available drawable as well as its value“. This event exhibit how to display the drawables, names as well as values inwards RecyclerView + CardView.


To purpose RecyclerView + CardView, nosotros convey to “Add Support Libraries of RecyclerView, CardView to Android Studio Project“.

Create layout of the CardView, layout/layout_cardview.xml

<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:card_view="http://schemas.android.com/apk/res-auto"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:layout_margin="10dp"     card_view:cardCornerRadius="20sp"     card_view:cardElevation="5sp">      <LinearLayout         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:orientation="vertical">          <ImageView             android:id="@+id/item_image"             android:layout_width="wrap_content"             android:layout_height="wrap_content" />         <TextView             android:id="@+id/item_name"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:textSize="18dp"/>         <TextView             android:id="@+id/item_value"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:textSize="16dp"/>      </LinearLayout>  </android.support.v7.widget.CardView>  

Create our custom MyRecyclerViewAdapter extends RecyclerView.Adapter, com.example.androidlistdrawable.MyRecyclerViewAdapter.java

package com.example.androidlistdrawable;  import android.content.Context; import android.graphics.drawable.Drawable; import android.support.v7.widget.CardView; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView;  import java.util.ArrayList; import java.util.List;  populace shape MyRecyclerViewAdapter extends         RecyclerView.Adapter<MyRecyclerViewAdapter.ItemHolder> {      somebody List<String> itemsName;     somebody List<Integer> itemsValue;     somebody LayoutInflater layoutInflater;     somebody Context context;      populace MyRecyclerViewAdapter(Context context){         this.context = context;         layoutInflater = LayoutInflater.from(context);         itemsName = novel ArrayList<String>();         itemsValue = novel ArrayList<Integer>();     }      @Override     populace MyRecyclerViewAdapter.ItemHolder onCreateViewHolder(ViewGroup viewGroup, int i) {         CardView itemCardView = (CardView)layoutInflater.inflate(R.layout.layout_cardview, viewGroup, false);         provide novel ItemHolder(itemCardView, this);     }      @Override     populace void onBindViewHolder(MyRecyclerViewAdapter.ItemHolder itemHolder, int i) {         itemHolder.setItemName(itemsName.get(i));         int value = itemsValue.get(i);         itemHolder.setItemValue(String.valueOf(value));         Drawable drawable = context.getResources().getDrawable(value);         itemHolder.setImageView(drawable);     }      @Override     populace int getItemCount() {         provide itemsName.size();     }      populace void add(int location, String iName, int iValue){         itemsName.add(location, iName);         itemsValue.add(location, iValue);         notifyItemInserted(location);     }      populace void remove(int location){         if(location >= itemsName.size())             return;          itemsName.remove(location);         itemsValue.remove(location);         notifyItemRemoved(location);     }      populace static shape ItemHolder extends RecyclerView.ViewHolder{          somebody MyRecyclerViewAdapter parent;         somebody CardView cardView;         TextView textItemName;         TextView textItemValue;         ImageView imageView;          populace ItemHolder(CardView cView, MyRecyclerViewAdapter parent) {             super(cView);             cardView = cView;             this.parent = parent;             textItemName = (TextView) cardView.findViewById(R.id.item_name);             textItemValue = (TextView) cardView.findViewById(R.id.item_value);             imageView = (ImageView) cardView.findViewById(R.id.item_image);         }          populace void setItemName(CharSequence name){             textItemName.setText(name);         }          populace CharSequence getItemName(){             provide textItemName.getText();         }          populace void setItemValue(CharSequence val){             textItemValue.setText(val);         }          populace CharSequence getItemValue(){             provide textItemValue.getText();         }          populace void setImageView(Drawable drawable){             imageView.setImageDrawable(drawable);         }      } }  

Modify layout to add together RecyclerView, layout/activity_main.xml.

<LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:padding="10dp"     android:orientation="vertical"     tools:context=".MainActivity">      <TextView         android:id="@+id/title"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:autoLink="web"         android:text="https://rootandroid.org//"         android:textStyle="bold" />      <android.support.v7.widget.RecyclerView         android:id="@+id/myrecyclerview"         android:layout_width="match_parent"         android:layout_height="match_parent"/> </LinearLayout>  

com.example.androidlistdrawable.MainActivity.java

package com.example.androidlistdrawable;  import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.StaggeredGridLayoutManager;  import java.lang.reflect.Field;  populace shape MainActivity extends AppCompatActivity {      somebody RecyclerView myRecyclerView;     somebody StaggeredGridLayoutManager staggeredGridLayoutManagerVertical;     somebody MyRecyclerViewAdapter myRecyclerViewAdapter;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         myRecyclerView = (RecyclerView)findViewById(R.id.myrecyclerview);          staggeredGridLayoutManagerVertical =                 novel StaggeredGridLayoutManager(                         2, //The lay out of Columns inwards the grid                         LinearLayoutManager.VERTICAL);         myRecyclerViewAdapter = novel MyRecyclerViewAdapter(this);         myRecyclerView.setAdapter(myRecyclerViewAdapter);         myRecyclerView.setLayoutManager(staggeredGridLayoutManagerVertical);          prepareItems();     }      somebody void prepareItems(){         Field[] fieldDrawables = android.R.drawable.class.getFields();         for(int i=0; i<fieldDrawables.length; i++){             Field plain = fieldDrawables[i];             endeavor {                 int value = (int) field.get(fieldDrawables);                 myRecyclerViewAdapter.add(                         myRecyclerViewAdapter.getItemCount(),                         "R.drawable." + field.getName(),                         value);             } choose handgrip of (IllegalAccessException e) {                 e.printStackTrace();             }          }     }  }  

get all available drawable as well as its value RecyclerView + CardView event amongst ImageView - listing available DrawableDownload the files (Android Studio Format).


More step-by-step examples of RecyclerView + CardView.