android recycleview and fragment

Creating Android Recycleview is pretty straight forward. All I need to do is :-

a) Setup Adapter

b) Set Layout

Let's walk through the process. First we need to include proper information in gradle

Then we need to layout our recycleview

Now lets create some adapter.

Noticed that we our adapter contains a view holder - just think of viewholder as your android row level layout file that has been inflated.
We are able to get a reference to our control as shown in the code below :-

public ViewHolder(View itemView) {
    super(itemView);    title = (TextView) itemView.findViewById(;    description = (TextView) itemView.findViewById(;    fullNews = (TextView) itemView.findViewById(;}

To get it to work we need the code below. All we need here is to configure data adapter and set our layout. Here, I am using DividerItemDecoration that comes with Android.

recyclerView = (RecyclerView) view.findViewById(;adapter = new NewsDataAdapter(news);RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), LinearLayoutManager.VERTICAL));recyclerView.setLayoutManager(layoutManager);recyclerView.setAdapter(adapter);recyclerView.setItemAnimator(new DefaultItemAnimator());

For a complete list of the project, please go here.


Popular posts from this blog

Solving Sonarqube :- Project was never analyzed. A regular analysis is required before a branch analysis

spark - pyspark reading from excel files