1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Expandablerecyclerviewのexpand()とcollapse()の注意点

Last updated at Posted at 2017-07-01

はじめに

Expandablerecyclerviewの親viewにて「もっと見る」⇔「閉じる」を繰り返したい.
こんな感じ.

device.gif

前提

まずこちらを参考にして各ファイルを実装する.
http://qiita.com/iKASiH/items/6707f9062e2e4bc48696

実装

必要ファイル

  • layout file
    • recyclerview.xml
    • parent_item.xml
    • child_item.xml
  • java function file
    • Parent_ViewHolder.java
    • Child_ViewHolder.java
    • Home_Adapter.java
    • MainActivity.java
  • java entity file
    • Home.java
    • Child.java
    • Parent.java

追加機能

  • layout file
    • 目がチカチカするので背景色を削除(parent_item.xmlrecyclerview.xml)
    • 最初は全て閉まっているので全てにもっと見るを配置(parent_item.xml)
  • function file
    • 広げたときにtextを閉じるに変換(Parent_ViewHolder.xml)
    • 縮めたときにtextをもっと見るに変換(Parent_ViewHolder.xml)
  • java entity file
    • 改変無し(そのまま用いる)

では実装します.

レイアウト(layoutfile)

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="開く"
        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) {//親viewのid取得
        super(itemView);
        text1 = (TextView)itemView.findViewById(R.id.parenttext);
    }
    public void open(){
        text1.setText("閉じる");
    }
    public void close(){
        text1.setText("開く");
    }

    @Override//広がる時呼ばれる
    public void expand() {
        open();
    }
    @Override//縮ませた時呼ばれる
    public void collapse() {
        close();
    }
}

いざ実行!!

device.gif

開いていないのに「閉じる」になっていたり...
開いているのに「開く」になっていたり...:tired_face:

解決策

解決策は,スクロール毎に開いているかを判定して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) {
        /*--------------ここを追加---------------*/
        if(isGroupExpanded(flatPosition)){
            holder.open();
        }
        else{
            holder.close();
        }
        /*---------------------------------------*/
    }

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

このライブラリにはisGroupExpandedメソッドが存在して,このメソッドは,開いている状態か開いていない状態かを判定してくれます.
flatpositionを引数にすると,親viewの状態によってtrue or falseがreturnされます.
それを用いてtextを再反映します.
これで完成です.結構焦りました.:frowning2:

是非,コメントよろしくお願いします.

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

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?