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.