0

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-the-recyclerview-within-layout

While implementing the code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.v7.widget.RecyclerView
      android:id="@+id/rvContacts"
      android:layout_width="match_parent"
      android:layout_height="match_parent" />

</RelativeLayout>

it is asked to implement in res/layout/activity_users.xml

I have created the project with Basic Activity & has the files

activity_main.xml & content_main.xml

Is activity_users.xml another custom file? or a default file? Can you tell me where it is supposed to implement the above code?

seb tom
  • 11
  • 1
  • 1

3 Answers3

1

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

  public class ContactAdapter extends
         RecyclerView.Adapter<ContactAdapter.ContactViewHolder> {

    private List<ContactInfo> contactList; // your item list

    public ContactAdapter(List<ContactInfo> contactList) {
      this.contactList = contactList;
    }

    @Override
    public int getItemCount() {
       return contactList.size();
    }

    @Override
    public void onBindViewHolder(ContactViewHolder contactViewHolder, int i) {
       // binding
    }

    @Override
    public ContactViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
      View itemView = LayoutInflater.from(viewGroup.getContext()).
      inflate(**your_row_layout**, viewGroup, false);
      return new ContactViewHolder(itemView);
    }

      public static class ContactViewHolder extends RecyclerView.ViewHolder {
       ...
    }
  }

Hope it helps you.

FrancescoAzzola
  • 2,666
  • 2
  • 15
  • 22
0

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

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

Thanks.

sonnv1368
  • 1,547
  • 12
  • 17
0

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.

Sahana Prabhakar
  • 581
  • 1
  • 8
  • 21