Step1 : MainActivity.xml
- Search for RecyclerView in Palette and drop it into the layout.
- Click ok to add the add the dependency
- Add an id. Select Width and Height to "match_parent".
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView android:id="@+id/rvMyList" android:layout_width="match_parent" android:layout_height="match_parent"/></FrameLayout>
Step 2 : Create a layout for holding each item in the RecyclerView
- Right click on the res > layout folder
- click new Layout Resource File
- Give the name recyclerview_item and set the parent as FrameLayout.
- set the width="match_parent" , height="wrap_content" , padding="16dp"
- Add a TextView
- Set id="tvItem" , textSize="30sp".
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="16dp">
<TextView android:id="@+id/tvItem" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30sp" /></FrameLayout>
Step 3: Create a RecyclerViewHolder class extend it to RecyclerView.ViewHolder class
Recycler view uses ViewHolder to generate each view as per needed
Recycler view uses ViewHolder to generate each view as per needed
RecyclerViewHolder.java
- Extend the class with RecyclerView.ViewHolder
- Implement the methods
- Get the View and Inflate the layout
- Create a method bind and set the bind method for getting each items.
package com.example.recyclerviewdemo;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
class RecyclerViewHolder extends RecyclerView.ViewHolder {
private TextView tvItem;
/* Constructor */
RecyclerViewHolder(@NonNull View itemView) {
super(itemView);
tvItem = itemView.findViewById(R.id.tvItem);
}
void bind(String item){
tvItem.setText(item);
}
}
Step 4: Create ViewHolderAdapter class
- Extend class with RecyclerView.Adapter<RecyclerViewHolder> class
- And update as following
package com.example.recyclerviewdemo;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class RecyclerViewAdapter extends RecyclerView.Adapter {
private int length;
public RecyclerViewAdapter(int numberOfItems){
length = numberOfItems;
}
@NonNull
@Override
public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.recyclerview_item,parent,false);
return new RecyclerViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) {
holder.bind(String.valueOf(position));
}
@Override
public int getItemCount() {
return length;
}
}
Comments
Post a Comment