Recycler View Tutorial With Example In Android Studio in Hindi -
First create a new Empty project -
Step 1 -Design activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="@layout/item_list" />
</LinearLayout>
Step 2 - Create a new xml layout item_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical" android:layout_width="match_parent"
android:layout_height="171dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="121dp"
android:contentDescription="@string/todo"
android:scaleType="centerCrop"
app:srcCompat="@drawable/ic_launcher_background"
tools:ignore="VectorDrawableCompat" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="@color/pinkish"
android:fontFamily="@font/comfortaa_bold"
android:gravity="center"
android:text="item"
android:textColor="#FFFFFF"
android:textSize="24sp" />
</LinearLayout>
</LinearLayout>
Step 3 - Create a new java class name Model.java
public class Model {
int pic;
String text;
public Model(int pic, String text) {
this.pic = pic;
this.text = text;
}
public Model() {
}
public Model(int pic) {
this.pic = pic;
}
public int getPic() {
return pic;
}
public void setPic(int pic) {
this.pic = pic;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
Step 4- Create a new java class Adapter.java
public class Adapter extends RecyclerView.Adapter<Adapter.viewHolder> {
// Create a array list of model
ArrayList<Model> list;
Context context;
// Create a Constructor
public Adapter(ArrayList<Model> list, Context context) {
this.list = list;
this.context = context;
}
// These are three methods which are implemented
@NonNull
@Override
public viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
// inflate your layout
View view = LayoutInflater.from(context).inflate(R.layout.item_list, parent, false);
return new viewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull viewHolder holder, int position) {
Model model = list.get(position);
holder.imageView.setImageResource(model.getPic());
holder.textView.setText(model.getText());
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "Click On items" + position, Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return list.size();
}
// Create a custom viewHolder to hold item_list objects
public class viewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
TextView textView;
public viewHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView);
textView = itemView.findViewById(R.id.textView);
}
}
}
Step 5- MainActivity.java
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize first recycler view
recyclerView = findViewById(R.id.recyclerview);
// Create Model array list to add items
ArrayList<Model> list = new ArrayList<>();
list.add(new Model(R.drawable.ic_launcher_background, "Item 1"));
list.add(new Model(R.drawable.ic_launcher_background, "Item 2"));
list.add(new Model(R.drawable.ic_launcher_background, "Item 3"));
list.add(new Model(R.drawable.ic_launcher_background, "Item 4"));
list.add(new Model(R.drawable.ic_launcher_background, "Item 5"));
list.add(new Model(R.drawable.ic_launcher_background, "Item 6"));
list.add(new Model(R.drawable.ic_launcher_background, "Item 7"));
// Make a object of Adapter and set on recycler view
Adapter adapter = new Adapter(list, this);
recyclerView.setAdapter(adapter);
// Initialize a object of A layout to show list recycler item
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
/*
*Types of Layout
*
* Layout 1
StaggeredGridLayoutManager Staggered = new StaggeredGridLayoutManager(4,
StaggeredGridLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(Staggered);
* Layout 2
GridLayoutManager gridlayoutManager = new GridLayoutManager(this,2);
recyclerView.setLayoutManager(gridlayoutManager);
* Layout 3
LinearLayoutManager layoutManager = new LinearLayoutManager(this,
LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(layoutManager);
*/
}
}
Tags:
Android Development