Recyclerview Dynamic Sections not using any 3rd lib

杀马特。学长 韩版系。学妹 提交于 2019-12-10 10:28:31

问题


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


回答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.




回答2:


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.



来源:https://stackoverflow.com/questions/36246658/recyclerview-dynamic-sections-not-using-any-3rd-lib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!