Android Basics – Learn how to create Recycler View

So far you have created an Adapter and added a View Holder and also code to onBindViewHolder.

Now it’s time to create a Recycler View which will use this Adapter. It will also pass data to this Adapter.

In order to do this you can create a new Activity, for the purpose of understanding. Let’s call this MaterialActivity. You will call this from the MainActivity on click of a button.

In the layout file for the new Activity you need to add the RecyclerView.

<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/sample_recycler"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:scrollbars="vertical"
    />

In the Activity class you need to set the Adapter.

public class MaterialActivity extends AppCompatActivity {

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

        String[] inputData = {"Hello Sam", "Hello John", "Hello Riki"};

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.sample_recycler);

        SampleAdapter adapter = new SampleAdapter(inputData);
        recyclerView.setAdapter(adapter);
    }

}

Source code is available here.

In the next Article we will take this further.

Author: Ankur

I am Enthusiastic about Learning new things and sharing my Knowledge. I like programming and have a pretty good background in Computer Science.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: