[JAVA] Notes on expand () and collapse () of Expandablerecyclerview

Introduction

I want to repeat "more" ⇔ "close" in the parent view of Expandablerecyclerview. Like this.

device.gif

Premise

First, implement each file by referring to this. http://qiita.com/iKASiH/items/6707f9062e2e4bc48696

Implementation

Required files

Additional features

Let's implement it.

Layout file

recyclerview.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

parent_item.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:gravity="center"
        android:id="@+id/parenttext"
        android:text="open"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize" />
</LinearLayout>

child_item.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/childtext"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize" />
</LinearLayout>

Function file

Parent_ViewHolder


package jp.app.oomae.hisaki.expandable_recyclerview;

import android.view.View;
import android.widget.TextView;
import com.thoughtbot.expandablerecyclerview.viewholders.GroupViewHolder;

public class Parent_ViewHolder extends GroupViewHolder {

    private TextView text1;

    public Parent_ViewHolder(View itemView) {//Get id of parent view
        super(itemView);
        text1 = (TextView)itemView.findViewById(R.id.parenttext);
    }
    public void open(){
        text1.setText("close");
    }
    public void close(){
        text1.setText("open");
    }

    @Override//Called when it spreads
    public void expand() {
        open();
    }
    @Override//Called when shrunk
    public void collapse() {
        close();
    }
}

Let's do it! !!

device.gif

It's "closed" even though it's not open ... Even though it is open, it is "open" ...: tired_face:

solution

The solution is to determine if it is open for each scroll and reflect the text.

Home_Adapter.java


package jp.app.oomae.hisaki.expandable_recyclerview;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.thoughtbot.expandablerecyclerview.ExpandCollapseController;
import com.thoughtbot.expandablerecyclerview.ExpandableRecyclerViewAdapter;
import com.thoughtbot.expandablerecyclerview.models.ExpandableGroup;

import java.util.List;

public class Home_Adapter extends ExpandableRecyclerViewAdapter<Parent_ViewHolder, Child_ViewHolder> {

    private ExpandCollapseController expandCollapseController;

    public Home_Adapter(List<? extends ExpandableGroup> groups) {
        super(groups);
        this.expandCollapseController = new ExpandCollapseController(expandableList, this);
    }

    @Override
    public Parent_ViewHolder onCreateGroupViewHolder(final ViewGroup parent, final int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.parent_item, parent, false);
        return new Parent_ViewHolder(view);
    }

    @Override
    public Child_ViewHolder onCreateChildViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.child_item, parent, false);
        return new Child_ViewHolder(view);
    }

    @Override
    public void onBindGroupViewHolder(Parent_ViewHolder holder, int flatPosition, ExpandableGroup group) {
        /*--------------Add here---------------*/
        if(isGroupExpanded(flatPosition)){
            holder.open();
        }
        else{
            holder.close();
        }
        /*---------------------------------------*/
    }

    @Override
    public void onBindChildViewHolder(Child_ViewHolder holder, int flatPosition, ExpandableGroup group, int childIndex) {
    }
}

There is an isGroupExpanded method in this library, which determines whether it is open or not. If flatposition is used as an argument, true or false will be returned depending on the state of the parent view. Use it to re-reflect the text. That's all there is to it. I was quite impatient. : frowning2:

Please leave a comment.

Github : https://github.com/hisakioomae/Expandable_Recyclerview_sample

Recommended Posts

Notes on expand () and collapse () of Expandablerecyclerview
Notes on Java path and Package
Released the No Todo app instead of Todo. .. (And notes on ridgepole)
Notes on Java's Stream API and SQL
Ruby on Rails ~ Basics of MVC and Router ~
Upload and download notes in java on S3
Notes on Protocol Buffers
python notes on docker
[Android] Notes on xml
credentials.yml.enc and master.key <Notes>
Notes on regular expressions
Modules and Mixins notes