01 Step ( Add Library For Dependencies) build.gradle
implementation "androidx.recyclerview:recyclerview:1.2.1"implementation "androidx.cardview:cardview:1.0.0"
02 Step ( Create Item View ) \app\src\main\res\layout\itemview.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/relativeLayout" android:background="@drawable/page" android:orientation="horizontal"> <ImageView android:id="@+id/imageview" android:layout_width="120px" android:layout_height="120px" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:src="@color/design_default_color_primary" /> <TextView android:id="@+id/textview" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toRightOf="@id/imageview" android:layout_margin="10dp" android:text="abcd" android:textColor="#000" android:textSize="30sp" /></RelativeLayout>
03 Step ( Create Drawable File ) \app\src\main\res\drawable\page.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#FFFFFF" /> <stroke android:width="1dp" android:color="#CCCCCC" /></shape>
04 Step ( Create MyListData File ) \app\src\main\java\com\example\app\MyListData.java
public class MyListData{ private String description; private int imgId; public MyListData(String description, int imgId) { this.description = description; this.imgId = imgId; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public int getImgId() { return imgId; } public void setImgId(int imgId) { this.imgId = imgId; }}
05 Step (Create MyListAdapter)\app\src\main\java\com\example\app\MyListAdapter.java
public class MyListAdapter extends RecyclerView.Adapter<MyListAdapter.ViewHolder>{ private MyListData[] listdata; // RecyclerView recyclerView; public MyListAdapter(MyListData[] listdata) { this.listdata = listdata; } // RecyclerView Layout Set @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext()); View listItem= layoutInflater.inflate(R.layout.itemview, parent, false); ViewHolder viewHolder = new ViewHolder(listItem); return viewHolder; } // RecyclerView Set Text And Image Or Click @Override public void onBindViewHolder(ViewHolder holder, int position) { final MyListData myListData = listdata[position]; holder.textView.setText(listdata[position].getDescription()); holder.imageView.setImageResource(listdata[position].getImgId()); holder.relativeLayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(view.getContext(),"click on item: "+myListData.getDescription(), Toast.LENGTH_LONG).show(); } }); } // RecyclerView Set Text And Image Or Click End // RecyclerView Set View item limit @Override public int getItemCount() { return listdata.length; } // RecyclerView Set View item limit End public static class ViewHolder extends RecyclerView.ViewHolder { public ImageView imageView; public TextView textView; public RelativeLayout relativeLayout; public ViewHolder(View itemView) { super(itemView); this.imageView = (ImageView) itemView.findViewById(R.id.imageView); this.textView = (TextView) itemView.findViewById(R.id.textView); relativeLayout = (RelativeLayout)itemView.findViewById(R.id.relativeLayout); } }}
06 Step ( Layout MainActivity File ) \app\src\main\res\layout\activity_main.xml
<androidx.recyclerview.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/view"/>
07 Step (MainActivity)\app\src\main\java\com\example\app\MainActivity.java
MyListData[] myListData = new MyListData[] { new MyListData("Email", android.R.drawable.ic_dialog_email), new MyListData("Info", android.R.drawable.ic_dialog_info), new MyListData("Delete", android.R.drawable.ic_delete), new MyListData("Dialer", android.R.drawable.ic_dialog_dialer), new MyListData("Alert", android.R.drawable.ic_dialog_alert), new MyListData("Map", android.R.drawable.ic_dialog_map), new MyListData("Email", android.R.drawable.ic_dialog_email), new MyListData("Info", android.R.drawable.ic_dialog_info), new MyListData("Delete", android.R.drawable.ic_delete), new MyListData("Dialer", android.R.drawable.ic_dialog_dialer), new MyListData("Alert", android.R.drawable.ic_dialog_alert), new MyListData("Map", android.R.drawable.ic_dialog_map),};RecyclerView recyclerView = (RecyclerView) findViewById(R.id.view);MyListAdapter adapter = new MyListAdapter(myListData);recyclerView.setHasFixedSize(true);recyclerView.setLayoutManager(new LinearLayoutManager(this));recyclerView.setAdapter(adapter);