Skip to content Skip to sidebar Skip to footer

Recyclerview With Viewholder Pattern

I am just an Android beginner & trying to work out RecyclerView with ViewHolder pattern with the sample at : https://guides.codepath.com/android/using-the-recyclerview#create-

Solution 1:

Once you defined your RecyclerView you have to define the single row layout. Then, you reference it in your adapter. Something like it:

publicclassContactAdapterextendsRecyclerView.Adapter<ContactAdapter.ContactViewHolder> {

    private List<ContactInfo> contactList; // your item listpublicContactAdapter(List<ContactInfo> contactList) {
      this.contactList = contactList;
    }

    @OverridepublicintgetItemCount() {
       return contactList.size();
    }

    @OverridepublicvoidonBindViewHolder(ContactViewHolder contactViewHolder, int i) {
       // binding
    }

    @Overridepublic ContactViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
      ViewitemView= LayoutInflater.from(viewGroup.getContext()).
      inflate(**your_row_layout**, viewGroup, false);
      returnnewContactViewHolder(itemView);
    }

      publicstaticclassContactViewHolderextendsRecyclerView.ViewHolder {
       ...
    }
  }

Hope it helps you.

Solution 2:

You can read at here. i see it is so clear.

http://www.androidhive.info/2016/01/android-working-with-recycler-view/

Thanks.

Solution 3:

You can add the recycler view in the content_main layout. Or if you dont want to complicate, remove the reference for content_main in your activity_main and add the recycler view in in the activity_main itself. Use activity_main in your BaseActivity.

Post a Comment for "Recyclerview With Viewholder Pattern"