Hello bloggers to this session of android development tutorial, In this session we are going to learn about how to implement the Recycler view in a list.

Recycler view is a widget which is more advanced and flexible version of list view. It gives user more memory to the device. Assume when there are unknown numbers of items in a scrollable list. It contains more space to free up the space recycler view is used it frees up the space by deleting the information which is out of layout(data above or below the screen of your phone) hence freeing up space user can get the data back when he scrolls back up the data is recreated.

Step 1: open android studio->create a new empty project.

Step 2: after the project has synced go to gradle scripts->build.gradle (module: app).

Step 3: copy the dependencies from this website to dependencies, after copying it sync the project. Dependencies may change so check in website before adding it in your code.

List Using Recycler View 1
List Using Recycler View 2

Step 4: now go to main activity.xml copy the following code in it.

·        <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/recycler_list"
        />

</RelativeLayout>

Step 5: right click on layout file ->select new -> new resource layout file and give it a name such as “list_item_text” and make sure to keep the root element as “LinearLayout” and give it an id. Then copy the following code in it. if an error occurs at tools:text click alt+enter.

·        <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     >
 
     <TextView
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:textSize="26dp"
         android:id="@+id/Text_List"
         tools:text="list"/>
  </LinearLayout>
 

Step 6: in main activity.java file we create a RecyclerView and tag it with id recycler_list and set LayoutManager in LinearLayoutManager. Then we create a list of array elements called stringList and fill the list. Make sure elements in the list is large enough to be scrollable or you can assign string list in another array list called list and by using .addAll method we create multiple values of same type . you can create  copy the following code into main_activity.java.

·        package com.example.recyclerlist;
 
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.support.v7.widget.LinearLayoutManager;
 import android.support.v7.widget.RecyclerView;
 
 import java.util.ArrayList;
 import java.util.List;
 
 public class MainActivity extends AppCompatActivity {
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
 
 
         RecyclerView recyclerView=(RecyclerView)findViewById(R.id.recycler_list);
         recyclerView.setLayoutManager(new LinearLayoutManager(this));
         ListAdapter listAdapter =new ListAdapter();
         recyclerView.setAdapter(listAdapter);
 
 
         List<String> stringList = new ArrayList<>();
         stringList.add("yahs");
         stringList.add("yats");
         stringList.add("yags");
         stringList.add("majs");
         stringList.add("prat");
         stringList.add("mayu");
         stringList.add("lodi");
         stringList.add("pakodi");
         stringList.add("bodi");
         stringList.add("gteri");
         stringList.add("hmaa");
         stringList.add("jki");
         stringList.add("glodi");
 
         List<String> list = new ArrayList<>();
         list.addAll(stringList);
         list.addAll(stringList);
         list.addAll(stringList);
         list.addAll(stringList);
 
 
         listAdapter.setItems(list);
     }
 }

Step 7: create a new class called ListAdapter. In ListAdapter.java, class List adapter extends to recyclerview.adapter “public class ListAdapter extends RecyclerView.Adapter  ” If the line shows error click alt+enter ->click implement methods->click ok, we create a method called getItemViewType which will be responsible for type of the item at that position. We create a static type class called Textviewholder and extend it to recyclerview.viewholder and create a constructor for it. RecyclerView will call onBindViewholder method whenever a view comes on screen so we create a method called bind so that we can use it to display text view. In recycler list it has only one text view, so we create mTextview which will hold the number of text in text view.

Array list of elements called list holds m Items. Then copy the following code.

·        package com.example.recyclerlist;
 
 import android.support.annotation.NonNull;
 import android.support.v7.widget.RecyclerView;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.TextView;
 
 import java.util.ArrayList;
 import java.util.List;
 
 public class ListAdapter extends RecyclerView.Adapter {
 
 
 
     private List<String> mItems =new ArrayList<>();
 
     public void setItems(List<String>items){
         mItems=items;
         notifyDataSetChanged();
 
     }
     @NonNull
     @Override
     public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
 
 
         if (viewType==0)
 
             return TextViewHolder.inflate(parent);
         else
             return null;
     }
 
     @Override
     public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
         if (holder instanceof TextViewHolder){
             ((TextViewHolder) holder).bind(mItems.get(position));
         }
 
     }
 
     @Override
     public int getItemCount() {
         return mItems.size();
     }
 
     @Override
     public int getItemViewType(int position) {
         return 0;
     }
 
     static class TextViewHolder extends RecyclerView.ViewHolder{
 
         private TextView mTextView;
 
         public static TextViewHolder inflate(ViewGroup parent){
            View view =LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_text,parent,false);
 
            return new TextViewHolder(view);
 
         }
 
 
         public TextViewHolder(@NonNull View itemView) {
             super(itemView);
 
             mTextView=itemView.findViewById(R.id.Text_list);
         }
 
         public void bind(String text){
 
             mTextView.setText(text);
         }
     }

Step 8: After you are done your app should look like this.

List Using Recycler View 3
List Using Recycler View 4

Conclusion

Congratulations, today you have learned how to create a list using recycler view. Using recycler view more complicated lists can be prepared (for e.g: playstore apps) recycler view manages the space allotment it can also be shown in grid layout by changing a single line of code but if you are going to keep detailed information then linear layout is best choice. You can use Recycler view and implement it in your code.

LEAVE A REPLY

Please enter your comment!
Please enter your name here