2

When extend RecyclerView.Adapter and how it works?

I am having trouble with understanding a piece of code. Can someone please explain me when this method extend and what is it for?

Hear is my code:

public class CompaniesAdapter extends RecyclerView.Adapter<CompaniesAdapter.MyViewHolder>{
.....
} 

In My class:

 public class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView name, date;

        public MyViewHolder(View view) {
            super(view);
            name = (TextView) view.findViewById(R.id.txtName);
            date = (TextView) view.findViewById(R.id.txtDate);
        }
Ali
  • 3,346
  • 4
  • 21
  • 56

3 Answers3

2

The adapter is a component that stands between the data model we want to show in our app UI and the UI component that renders this information. In other words, an adapter guides the way the information are shown in the UI. So if we want to display our data in list, we need an adapter for the RecyclerView. This adapter must extend a class called RecyclerView.Adapter passing our class that implements the ViewHolder pattern:

public class MyAdapter extends RecyclerView.Adapter<MyHolder> { ..... }
MashukKhan
  • 1,946
  • 1
  • 27
  • 46
1

Recyclerview always need to extend it's own Adapter with ViewHolder. So RecyclerView.Adapter works like any other but ViewHolder contains all your views in the layout for the item.

Follow this below link and understand how ViewHolders works in RecyclerView.Adapter:

https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html

Yashaswi N P
  • 896
  • 8
  • 15
1

RecyclerView.Adapter provide access to data set and they are responsible for creating the correct layout for individual row of a items. Since RecyclerView.Adapter is abstract,you have to implement these three methods:

  1. public VH onCreateViewHolder(ViewGroup parent, int viewType)
  2. public void onBindViewHolder(VH holder, int position)
  3. public int getItemount() The VH in the method signature above is the generic type parameter.

For more in details go through this link https://www.androidhive.info/2016/01/android-working-with-recycler-view/