Hey Guys, This article will help you to better understand the implementations of RecyclerView in android with your own adapter and custom layout.
for more android an coding related stuff visit my channel https://www.youtube.com/channel/UClqD9_F5sCJN1st7tl30Q2A
Full video tutorial is here :
Part 1 : Creating RecyclerView & Adapter
Part 2 : Adding & Removing Items from RecyclerView on Run Time
you can find latest dependency for RecyclerView on the official google android developer page. https://developer.android.com/guide/topics/ui/layout/recyclerview
Copy code for adapter and follow Part 1 video instructions how to can use or make adapter for your recyclerview.
ToDoAdapter.java
package com.raj.recyclerviewexample;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class TodoAdapter extends RecyclerView.Adapter<TodoAdapter.MyViewHolder> {
Context context;
ArrayList<TodoItem> todoItems;
//con
public TodoAdapter(Context context, ArrayList<TodoItem> todoItems) {
this.context = context;
this.todoItems = todoItems;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.rv_item_layout, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {
holder.tv.setText(todoItems.get(position).getTodo_name());
if (todoItems.get(position).getComplete()) {
holder.ch.setChecked(true);
} else {
holder.ch.setChecked(false);
}
holder.delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
todoItems.remove(position);
notifyDataSetChanged();
}
});
}
@Override
public int getItemCount() {
return todoItems.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView tv;
CheckBox ch;
ImageView delete;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
tv = itemView.findViewById(R.id.tv);
ch = itemView.findViewById(R.id.ch);
delete = itemView.findViewById(R.id.delete);
}
}
}
Thanks for reading this article i hope it will help you learn recyclerview in android. follow my and subscribe to my channel to learn more about coding and life style.
visit my channel https://www.youtube.com/channel/UClqD9_F5sCJN1st7tl30Q2A