javaandroidandroid-recyclerviewsectionedrecyclerviewadapter

ArrayList in RecyclerView using sectioned-recyclerview


Im use sectioned-recyclerview

In Head only TextView and button, in Item only LinearLayout.

After this code:

ViewGroup mContainerParent = (ViewGroup) mainContainerArr.get(section).getParent();
if (mContainerParent != null) {
    mContainerParent.removeView(mainContainerArr.get(section));
}

all views started shifting. I think need to do some notify, but i dont know which one of.

I tried do notifySectionChanged(section), notifyDataSetChanged() and etc but its not works.

Code:

Head:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/details_description_head_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <ImageView
        android:id="@+id/details_description_head_caret"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</RelativeLayout>

Item:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/details_description_main_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
</LinearLayout>

Adapter:

public class QuestDetailsRecyclerAdapter extends SectionedRecyclerViewAdapter<QuestDetailsRecyclerHolder> {

    private ArrayList<String> headTitleArr;
    private ArrayList<LinearLayout> mainContainerArr;

    int addSection(
            String head,
            LinearLayout container
    ) {
        // Add section
        headTitleArr.add(head);
        mainContainerArr.add(container);

        // Notify
        notifyDataSetChanged();

        return mainContainerArr.size() - 1;
    }

    QuestDetailsRecyclerAdapter(
            ArrayList<String> headTitleArr,
            ArrayList<LinearLayout> mainContainerArr
    ) {
        this.headTitleArr = headTitleArr;
        this.mainContainerArr = mainContainerArr;
    }

    @Override
    public int getSectionCount() {
        return headTitleArr.size();
    }

    @Override
    public int getItemCount(int section) {
        return mainContainerArr.get(section).getChildCount();
    }

    @Override
    public void onBindHeaderViewHolder(
            QuestDetailsRecyclerHolder holder,
            int section,
            boolean expanded
    ) {
        holder.title.setText(headTitleArr.get(section));
    }

    @Override
    public void onBindViewHolder(
            QuestDetailsRecyclerHolder holder,
            int section,
            int relativePosition,
            int absolutePosition
    ) {
        // Delete old parent if exists
        ViewGroup mContainerParent = (ViewGroup) mainContainerArr.get(section).getParent();
        if (mContainerParent != null) {
            mContainerParent.removeView(mainContainerArr.get(section));
        }

        // Add container (layout)
        holder.mainContainer.addView(mainContainerArr.get(section));
    }

    @Override
    public QuestDetailsRecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // Get layout id
        int layoutId;
        switch (viewType) {
            case VIEW_TYPE_HEADER:
                layoutId = R.layout.details_description_header;
                break;
            case VIEW_TYPE_ITEM:
                layoutId = R.layout.details_description_main;
                break;
            default:
                layoutId = R.layout.details_description_main;
        }

        // Inflate layout
        View view = LayoutInflater.from(parent.getContext())
                .inflate(layoutId, parent, false);

        return new QuestDetailsRecyclerHolder(view, this);
    }
}

Solution

  • I fixed this with multiple item types.

    Tutorial

    Solution commit in my project