3

i want to add header to recycle view i am trying to achieve it using

 @Override
public int getItemViewType(int position) {
    // depends on your problem
    if (position == 0 || position == 4) {
        return 2;
    } else {
        return 1;
    }
}

 @Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                  int viewType) {
    // create a new view
    if (viewType == 1) {
        View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.cardview_row, null);
        return new ViewHolder(itemLayoutView);

    } else if (viewType == 2) {
        View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.cardview_row1, null);
        return new ViewHolders(itemLayoutView);
    }


    return null;


}

but how i can do it in run time like when i don't know position when it should show section like i have json

{
"DATA": [
    "hi",
    "heloo",
    "bye"
],
"MAHA": [
    "ans",
    "rs",
    "re",
    "jab",
    "bad"
]

}

where data and maha is section i want to display other elements

currently i am making arraylist of all elements and adding hardcore value for section but how i can do this viva using above json

bob
  • 155
  • 3
  • 13
  • With your current design, you could keep a member array or List of header positions in your Adapter, and return type 2 from `getItemViewType()` if the position value is in the array/List. Create a setter method for it, figure it when parsing the JSON, and set it before calling `notifyDataSetChanged()`. – Mike M. Mar 27 '16 at 11:29

2 Answers2

1

What you need is an expandable recyclerview with parents(headers) and children(entries). Here you go: https://github.com/bignerdranch/expandable-recycler-view

If you want to show the entries always and dont profit from the expandable feature, just do: expAdapter.expandAllParents().

I know you dont want to use 3rd library parties but in my opinion this is the best way to deal with it and saves you a lot of time. Moreover if someone else has the same question he maybe finds this solution useful.

Oliver U.
  • 336
  • 1
  • 4
  • 14
0

Create a class like this for your JSON data, and create one Section class per node (in your example, one for DATA and another for MAHA):

class Section {
    String header;
    List<String> itemList;
}

Then create your custom adapter:

public class SectionedRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<Section> sectionList;

    public SectionedRecyclerViewAdapter(List<Section> sectionList) {
        this.sectionList = sectionList;
    }

    @Override
    public int getItemViewType(int position) {
        int currentPos = 0;

        for (Section section : sectionList) {
            // check if position is in this section
            // +1 for the header, -1 because idx starts at 0
            if (position >= currentPos && position <= (currentPost + section.itemList.size() + 1 - 1)) {
                if (position == currentPos) {
                    return 2; // header
                } else {
                    return 1; // item
                }
            }

            // +1 for the header
            currentPos += section.itemList.size() + 1;
        }

        throw new IndexOutOfBoundsException("Invalid position");
    }

    // use the onCreateViewHolder method from your question...
}

I know you don't want to use a 3rd party lib, but you can check how the getItemViewType method of SectionedRecyclerViewAdapter is done here.

Gustavo Pagani
  • 6,583
  • 5
  • 40
  • 71