Matthew McCarrison
Matthew McCarrison

Reputation: 33

RecyclerView 2 lists in one

I am trying to create something similar to this:

RecyclerView

Instead of folders and files I want to have incomplete items and completed items.

I am new to RecyclerViews, how would I manage to get two unrelated lists such as folders and files into one RecyclerView that scrolls as one?

Upvotes: 0

Views: 1525

Answers (2)

Pavan
Pavan

Reputation: 777

You could use heterogenous RecyclerView which supports more than one viewType or view holders. Your dataSet could be List<Object> or a marker class which supports the models Files and Folders for example and then you could do something like this in your adapter :

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
class ViewHolderFolders extends RecyclerView.ViewHolder {
    ...
    public ViewHolderFolders(View itemView){
    ...
    }
}

class ViewHolderFiles extends RecyclerView.ViewHolder {
    ...
    public ViewHolderFiles(View itemView){
    ...
}

@Override
public int getItemViewType(int position) {
    //Let us say you return 0 for folders and 1 for files
    //This is just an example you could write your own logic but make sure to  differenciate the two
    //Folders and Files in here are model class used to populate the 
    //recyclerview with. This is just an example.
    if (yourDataSet.get(position) instanceof Folders) {
        return 0;
    } else{
        return 1;
    }

}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
     switch (viewType) {
         case 0: return new ViewHolderFolders(...);
         case 1: return new ViewHolderFiles(...);
         //Your code here
     }
}

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
    switch (holder.getItemViewType()) {
        case 0:
            ViewHolderFolders viewHolderFolders = (ViewHolderFolders)holder;
            ...
            break;

        case 1:
            ViewHolderFiles viewHolderFiles = (ViewHolderFiles)holder;
            ...
            break;
    }
}

}

Upvotes: 1

MezzDroid
MezzDroid

Reputation: 560

You can use a sectioned recyclerView for this. Where you can have section as header and each header with its own items.

Refer to this library: Sectioned RecyclerView

Upvotes: 0

Related Questions