Skip to main content

Android Recycler View Tutorial : Part 2

In part 1 we created recycler view without any model or view holder. In this part we will create data model and a view holder and bind both these to the recycler view.

Previous part :

Now lets first create a simple data model:
This class does nothing special it just generate some dummy data.

public class DummyModel {

    String textData = "not set yet !!";

    public DummyModel() {


     * constructor to set the dummy text
     * @param data
    public DummyModel(String data) {
        textData = data;

    static public List<DummyModel> getDummyModel(int length) {
        List<DummyModel> dummyModelsList = new ArrayList<>();
        for (int i = 0; i < length; i++) {
            dummyModelsList.add(new DummyModel("This is the model "+i));
        return dummyModelsList;

     * just a setter method !Not used!
     * @param text
    public void setText(String text) {
        this.textData = text;


Now lets create our view holder which will set the data to the view:
we are creating a separate class for our view holder. This enables to reuse the component with different recycler view or one recycler view to have different component

public class DummyVH extends RecyclerView.ViewHolder {

    TextView textView;

     * Constructor to set the items
     * @param itemView
    public DummyVH(View itemView) {
        textView = (TextView)itemView.findViewById(;
        textView.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(RecylerActivity.context, "yooo "+textView.getText(), Toast.LENGTH_SHORT).show();

     * functiion to set the data
     * @param dummyModel
    public void bindVH(DummyModel dummyModel)


Now lets create our adapter which will bind or view and the data model.
This class is same as the previous dataAdapter created in previous tutorial. Recycler layout item is also same. Just the view holder is just initiated from different class.

 * THis class will be the data adapter for the recycler view
 * This class must exxtend the Recycler view adapter.
public class RvDataAdpt2 extends RecyclerView.Adapter {
    List<DummyModel> dummyModelsList;

    public RvDataAdpt2(List<DummyModel> listDummyModels)
        dummyModelsList = listDummyModels;

    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        //lets populate our recyler view with the item created;
        //get the view from the layout inflator
        //  third parameter is set to false to prevent viewgroup to attach to root
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item,parent,false);
        return new DummyVH(view);

    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        //bind the view with the holder

    public int getItemCount() {
        return dummyModelsList.size(); // to display the 100 items


Now lets bind this adapter to the recycler view. I have used the project created in previous tutorial.

public class RecylerActivity extends AppCompatActivity {

        public static Context context;
    //Below are the componets which are required by the Recycler view
    RecyclerView recyclerView; //this will hold the recycler view from the layout
    RecyclerView.Adapter   mAdapter; //this will hold the adapter for the recycler view
    RecyclerView.LayoutManager mLayoutmanager; //holds the layout manager

    protected void onCreate(Bundle savedInstanceState) {
        context = this;
        setContentView(R.layout.activity_recyler); //layout which contain the recycler view

        //Find the Recycler view
        recyclerView = (RecyclerView)findViewById(; //got the recycler view from the layout

        //set the layout manager for the recycler view
        //standard layout managers( LinearLayoutManager or GridLayoutManager) can be used, or implement your own.
        //Layout Manager tells how the item are shown in your Recycler View
        mLayoutmanager = new LinearLayoutManager(this);

        //set the data adapter
        //Adapter contain the Data Which need to be shown in the view
        // mAdapter = new RvDataAdpt();
        mAdapter = new RvDataAdpt2(DummyModel.getDummyModel(100));

get the source from :-


Popular posts from this blog

Xamarin : Simple Calculator Tutorial Visual Studio C#

Simple Calculator in Android (Xamarin) using VisualStudio C#In Brief: This article will help you on building a simple calculator in android Xamarin with simple steps using C# Language in Visual Studio. Detail:
Building this simple calculator will let you learn how to work with the Android Buttons, Text View and simple functions. The UI is not so beautiful but is a great way to learn the basic functionalities.

Create Custom View in android MATRIX EFFECT CANVAS tutorial

Create Custom View in android MATRIX EFFECT CANVAS tutorial
Hi all as I stared my development with android I always wanted to create a Custom view or rather I want a canvas where I can draw freely. To learn about canvas I decided to create matrix rain effect which I can add to my android layout. Here is the complete tutorial which I have post. Hopping it will be use full to someone.

You can find the link to android project at the bottom of the post

Android Transparent status bar

Recently I wanted to get a transparent status bar so that my app background blends smoothly with the status bar . Below method worked