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(R.id.title);    description = (TextView) itemView.findViewById(R.id.description);    fullNews = (TextView) itemView.findViewById(R.id.fullNews);}


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(R.id.recycler_view);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.


Comments

Popular posts from this blog

The specified initialization vector (IV) does not match the block size for this algorithm