使用sectioned-recyclerview在RecyclerView中的ArrayList

时间:2017-11-12 18:51:40

标签: java android android-recyclerview sectionedrecyclerviewadapter

我使用sectioned-recyclerview

仅限Head TextView和按钮,在Item Only LinearLayout中。

在此代码之后:

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

所有观点都开始转变。我认为需要做一些通知,但我不知道哪一个。

我尝试过notifySectionChanged(section),notifyDataSetChanged()等,但它不起作用。

代码:

目:

<?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>

档案:

<?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>

适配器:

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);
    }
}

1 个答案:

答案 0 :(得分:0)

我用多种项目类型修复了这个问题。

Tutorial

Solution commit in my project