3

I've been trying to implement this sectionedRecyclerView library in my app, but i need it to have cases where there are no section headers/footers (while still displaying items) and cases where there are. This library always needs at least 1 section/header.

Also, this library only has a single viewHodler. I want to be able to use different viewHolders, for items, headers and footers.

What methods would I need to override/change to accomplish this?

nebuchadnezzar I
  • 125
  • 1
  • 12

1 Answers1

0

Firstly, you should have a type variable on your model.

public class MyModel{
    private int type;
    //setters and getters...
}

Then, create 2 ViewHolders on your adapter but the class that you which will use on Adapter<> will be the mother class(ViewHolder).

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    class ViewModel1 extends RecyclerView.ViewHolder{
        //your code
    }

    class ViewModel2 extends RecyclerView.ViewHolder{
        //your code
    }
}

After that, you should implement some methods on your adapter.

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<MyModel> mModelList;

    public MyAdapter(List<MyModel> models){
        this.mModelList = models;
    }

    @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        switch (viewType) {
            case TYPE_1:
                view = inflater.inflate(R.layout.view_type_1, parent, false);
                return new ViewModel1(view);
            case TYPE_2:
                view = inflater.inflate(R.layout.view_type_2, parent, false);
                return new ViewModel2(view);
        }
        return null;
    }

    @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final MyModel item = mModelList.get(position);
        switch (item.type) {
            case TYPE_1:
                final ViewHolder1 vh1 = (ViewHolder1)holder;
                //your code
            break;
        case TYPE_2:
            final ViewHolder2 vh2 = (ViewHolder2)holder;
            //your code
            break;
       }
    }

    @Override public int getItemViewType(int position) {
        return mModelList.get(position).type;
    }

    @Override public int getItemCount() {
        return mModelList.size();
    }
}
Arunjith R S
  • 792
  • 12
  • 23
jmarkstar
  • 1,335
  • 14
  • 21
  • Does this take the library into account? I already know how to create recyclerView with different viewHolders, I just want to know how to modify the current library, without having to change the entire thing. There are methods and variables that depend on the current structure of the library and I just need to change the parts that relate to sections and headers – nebuchadnezzar I Jul 18 '17 at 07:45