Recyclerview was introduced with Android 5.0 aka Android L. In simplest terms recycler view widget is advanced form of Android List View widget. Recycler view is Simpler but yet very powerful and enhances performance of android application when compared android list view.
A point to be noted here is that for application running on android devices having OS prior to Android L , Android Support library is required to be added .
Lets get our hands dirty with code :).
Changes in Main XML Layout
Changes in java Code use of Fragment.
Creating a customized row layout
Creating an adapter
Changes in Main XML Layout (main_layout.xml)
2 Changes in Java Code With Fragment
Although we are using Fragment for this User Interface but Android Activity can also be used to create UX as per need and recycler view can be added.
In onCreateView callback for your fragment inflate the xml layout we created in step 1, and initialize your recylcerviewList variable . Oh sorry before initialization of recylcerviewList variable its declaration is required.
RecyclerView recyclerViewList;
Now initialize your recyclerviewList. (MainFragment.java)
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View v = inflater.inflate(R.layout.main_layout, null);
recyclerViewList = (RecyclerView)v.findViewById(R.id.recList);
return v;
}
In onActivityCreated callback for your fragment and create and intialize a variable LinearLayoutManager llm.
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
set this as layout manager for recyclerview list. This step is important , otherwise if this step is missed even after successful implementation of other steps , nothing would be displayed on screen. Just to add my experience when first day I encountered recylcer view i did this mistake and wasted 4 hours to find out the problem. Ok. ok.. I know you people are intelligent and won’t do this silly mistake.
3. Creating a customized row layout (rowlayout.xml)
4. Creating an Adapter (NavigationAdapter.java)
public class NavigationAdapter extends RecyclerView.Adapter
}
remember to extend your class from RecyclerView.Adapter.
create one static class called ViewHolder that again extends RecyclerView.Holder
public static class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View itemView) {
// initialize your customized row layout view items here.
}
}
Now inside public NavigationAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
} method , a callback method inside NavigationAdapter class.
inflate your customized row layout and initialize viewholder.
public NavigationAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.rowlayout, parent, false); //Inflating the layout
ViewHolder vhItem = new ViewHolder(v); //Creating ViewHolder and passing the object of type view
return vhItem;
}
Now you have created you view holder and and intialized it with real view objects. Next text is to bind actual data. We do it in another helper callback .
public void onBindViewHolder(NavigationAdapter.ViewHolder holder, int position) {
// holder…… = as per postion in the arrayList.
}
Then there are few callbacks method for your adapter class like
public int getItemCount() {
}
public int getItemViewType(int position) {
}
which are needed to be overriden.
Happy Coding !.
Note : Dear Copy-Paste Community :).
Hope you are doing good.
If you are a software engineer study the material before copy pasting.
Else knowledge gets increased when it is shared.