Category

Android

Category

RecyclerView is the advanced form of ListView.  Android improved ListView in RecyclerView type. Before RecyclerView we used ListView for vertical listing. For the horizontal listing, we had to do a lot of work in Listview and for the grid, we used another GridView Android component.

Android introduced vertical and horizontal direction in LinearLayoutManager and GridLayoutManager for grid view. With the help of these two classes, we can set vertical, horizontal and grid view for example

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerview.setLayoutManager(linearLayoutManager);
 LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
 linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerview.setLayoutManager(linearLayoutManager);

Same for Grid

 GridLayoutManager gridLayoutManager = new GridLayoutManager(this,3);
 recyclerview.setLayoutManager(gridLayoutManager);

Before starting this tutorial, you should have the latest Android Studio, If you don’t know about the latest feature about Android Studio, have a look at these YouTube tutorial.

Step-1

Create Android Studio project called RecyclerViewExample.

Step-2

Now open activity_main.xml layout. drag RecyclerView in this layout and set constraints. Now right click on the RecyclerView and then select set Sample Data menu. Now you have a popup and you have more than one templates. so choose one.

Step-3

Now open Android Resource Manager editor. If you cannot see the Android Resource Manager on the left side of Android Studio, click on the View -> tool Window -> Resource Manager.

Step-4

Choose the RecyclerView item layout. Design the layout according to your prototype.

Step-5

We have not specific design, so we start with a list of country. We display the country name list in RecyclerView. Create an array string of countries in String.xml file.

<resources>
    <string name="app_name">RecyclerViewExample</string>
    <string-array name="array_countries">
        <item>Pakistan</item>
        <item>India</item>
        <item>United State</item>
        <item>United Kingdom</item>
        <item>New Zeeland</item>
    </string-array>
</resources>

We have the only name of the country, we make a model class with one property.

package com.becody.recyclerviewexample.modals;

/**
 * Created by Rana Aamer Sohail on 25,05,2019
 */
public class CountryM {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Now make a Java class of CountryAdapter. RecyclerView provide RecyclerView.Adapter class so after creating the CountryAdapter class we will extend RecyclerView.Adapter class. After extending this class, then we will override three methods of RecyclerView.Adapter.

1- onCreateViewHolder

2- onBindViewHolder

3- getItemCount

the full code of CountryAdapter class is

package com.becody.recyclerviewexample.adapters;

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 com.becody.recyclerviewexample.R;
import com.becody.recyclerviewexample.modals.CountryM;

import java.util.List;
import java.util.zip.Inflater;

/**
 * Created by Rana Aamer Sohail on 25,05,2019
 */
public class CountryAdapter extends RecyclerView.Adapter {
    private List<CountryM> countryMList;

    public class CountryViewHolder extends RecyclerView.ViewHolder {
        TextView txtCountry;


        public CountryViewHolder(@NonNull View itemView) {
            super(itemView);
            txtCountry = itemView.findViewById(R.id.textView1);
        }
    }

    public CountryAdapter(List<CountryM> countryMList) {
        this.countryMList = countryMList;
    }

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

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        CountryViewHolder countryViewHolder = (CountryViewHolder) viewHolder;
        CountryM countryM = countryMList.get(i);
        countryViewHolder.txtCountry.setText(countryM.getName());
    }

    @Override
    public int getItemCount() {
        return countryMList.size();
    }
}

Now call this CountryAdapter from your  MainActivity Java class like this

package com.becody.recyclerviewexample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.becody.recyclerviewexample.adapters.CountryAdapter;
import com.becody.recyclerviewexample.modals.CountryM;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView listing;
    private CountryAdapter countryAdapter;
    private List<CountryM> countryMList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initComponents();
    }

    private void initComponents() {
        listing = findViewById(R.id.listing);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        listing.setLayoutManager(linearLayoutManager);
        countryAdapter = new CountryAdapter(countryMList);
        listing.setAdapter(countryAdapter);
        showData();
    }

    private void showData() {
        String[] countryArr = getResources().getStringArray(R.array.array_countries);
        for (String str : countryArr) {
            CountryM countryM = new CountryM();
            countryM.setName(str);
            countryMList.add(countryM);
        }
        countryAdapter.notifyDataSetChanged();
    }
}

I hope you understood the basic concept of RecyclerView. In my next tutorial, I will explore RecyclerView with more detail. If you’re still any confusion about this tutorial, you can watch the full video of the tutorial in Urdu/Hindi.

 

Download Source Code