RecyclerView in Android

In this tutorial, we will learn about RecyclerView in Android. Recycler view is introduced from android 6.0(Marshmallow). RecyclerView is used to create a list or grids in android. You can say it RecyclerView is the next version of ListView. RecyclerView provides much better performance than ListView. Here we will create simple RecyclerView.

Info! Please follow each step carefully.

Implement Dependencies:

Our first step would be to add two dependencies in our project. so add these two given dependencies in your Gradle (Module: app).

implementation 'com.android.support:recyclerview-v7:26.1.0'

Now write the following code in main_activity.xml to add RecyclerView in activity.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main"
    tools:context=".MainActivity">
 
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical" />
 
</RelativeLayout>

Our next step is to create model. Name this class as DataModel.java.(Model totally depends upon the requirements of project. Here we are just taking two variables title and description in model).

public class DataModel {
    private String post_title;
    private String post_description;


    public TopicTitleModel(String post_title, String post_description) {
        this.post_title = post_title;
        this.post_description = post_description;
    }

    public String getPost_title() {
        return post_title;
    }
  
    public String getPost_description(){
        return post_description;
    }
}

Our next step is to create layout for recyclerView item. Design is totally depends upon the project requirements. Here we are just creating simple layout to show title and description. we named this layout as single_item_layout.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:clickable="true"
    android:focusable="true"
    android:orientation="vertical"
    android:padding="16dp"
    >
 
    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:textSize="16dp"
        android:textStyle="bold" />
 
    <TextView
        android:id="@+id/description"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/title" />
 
 
</RelativeLayout>

After creating single_item_layout.xml our next step is to create a Adapter for RecyclerView. We named this adapter as PostAdapter.java.

public class PostAdapter extends RecyclerView.Adapter<PostAdapter.ViewHolder> {

    List<DataModel> list=new ArrayList<>();
    Context context;

    public TitlesAdapter(List<DataModel> list, Context context){
        this.list=list;
        this.context=context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.single_item_layout,viewGroup,false);
        return new ViewHolder(v);
    }

    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {
        viewHolder.text.setText(list.get(i).getPost_title());
      viewHolder.description.setText(list.get(i).getPost_description());
    }
    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        TextView text;
        TextView description;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            text=itemView.findViewById(R.id.title);
            description=itemView.findViewById(R.id.description);
        }
    }
}

No we reached at our final step. In this step we will get Recycler we in MainActivity.java then we will set adapter and LayoutManager to RecyclerView.

public class MainActivity extends AppCompatActivity {
    private List<DataModel> list = new ArrayList<>();
    private RecyclerView recyclerView;
    private PostAdapter postAdapter;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
 
        postAdapter = new PostAdapter(movieList,getApplication());
      
        prepareData();
      
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(postAdapter);
    }
 
    private void prepareData() {
        list.add(new DataModel("Color Red","Apple is Red"));
        list.add(new DataModel("Color Blue","Sky is Blue"));
        list.add(new DataModel("Color Green","Grass is Green"));
    }
}

 

Spread the love
Scroll to Top
×