如何折叠可扩展回收器视图中的所有子视图

how to collapse all childviews on expandable recycler view

我有我的可扩展 recyclerview 集,有两种布局 HEADER 和 CHILD。一切正常。

here is my screen shot.

我为 header 和 child 使用了不同的布局。

当用户单击一个 header 时,我希望我的程序检查是否有任何其他 header 被展开。如果是,则需要将其折叠。单击一个菜单时,最后展开的视图应该会折叠。

我试过使用布尔标志。 谁能帮帮我

如果您有布尔标志,请保存展开折叠状态。您可以编写一个函数来迭代您的数据集 ArrayList 并将其设置为 false/true 并调用

adapter.notifyDatasetChanged();

如果您使用此 library to create your expandable sections like in this example,您可以遍历 ExpandableContactsSection 的实例并检查标志 'expanded'。

Working with Big Nerd Ranch recycler:expand library ('com.bignerdranch.android:expandablerecyclerview:1.0.3')

In RecyclerAdapter.Java code...

 @Override
public void onParentItemClickListener(int position) {
    /**
     * @Params
     * Se comienza en -1, al clickear el primer grupo, se registra en la variable su posicion
     * al clickear el siguiente grupo, si la variable no es igual a su posicion se procede a
     * cerrar el grupo anterior.
     * */

    Object parent = mParentItemList.get(position);
    //Toast.makeText(mContext,"posicion "+String.valueOf(position),Toast.LENGTH_SHORT).show();

    if(lastExpanded == -1){
        lastExpanded = position;

    } else if(lastExpanded == position){
        lastExpanded = -1; //Reinicia Variable

        notifyItemChanged(position);
    }else{
        //Cierra grupo abierto
        int oldExpand = lastExpanded;
        Toast.makeText(mContext,"se cerro  "+String.valueOf(oldExpand),Toast.LENGTH_SHORT).show();
        lastExpanded = position;

        //Need the colapse group code

        notifyItemChanged(oldExpand);
        notifyItemChanged(position);
    }

    super.onParentItemClickListener(position);
}

我需要如何在单击另一个父组后折叠组...

如果你有解决办法,请评论我