[JAVA] Folding and unfolding the contents of the Recyclerview

Introduction

I want to fold and stretch the contents of the recyclerview!

device.gif

References

I referred to this. https://github.com/thoughtbot/expandable-recycler-view This one is easier to understand! !!

Implementation

First, add the build.gradle library.

build.gradle


dependencies {
    compile 'com.thoughtbot:expandablerecyclerview:1.3'
    compile 'com.android.support:recyclerview-v7:25.3.1'
}

Next, create a parent-only `ViewHolder``` and a child-only `ViewHolder```. ViewHolder makes the data and the View displayed on the screen related. Of course, since there are a parent view and a child view, two ViewHolders are required.

Parent_ViewHolder.java


package This is my package
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 set(String i){//Change the acquired id
        text1.setText(i);
    }
}

Child_ViewHolder.java


package This is my package;

import android.view.View;
import android.widget.TextView;

import com.thoughtbot.expandablerecyclerview.viewholders.ChildViewHolder;

public class Child_ViewHolder extends ChildViewHolder {

    private TextView text1;

    public Child_ViewHolder(View itemView) {//Get id of child view
        super(itemView);
        text1 = (TextView)itemView.findViewById(R.id.childtext);
    }
    public void set(String i){//Change the acquired id
        text1.setText(i);
    }
}

Then, we will create an adapter that associates these. Is it a place where parents and children are related, that is, like a home? : raised_hand:

Home_Adapter.java


package This is my package;

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) {
        holder.set(String.valueOf(flatPosition));//Parent_Call the function created by viewHolder
    }

    @Override
    public void onBindChildViewHolder(Child_ViewHolder holder, int flatPosition, ExpandableGroup group, int childIndex) {
        holder.set(String.valueOf(childIndex));//Child_Call the function created by viewHolder
    }

}
  1. onCreateViewHolder Extends the contents of the specified xml file for the specified view.
  2. onBindViewHolder Called by RecyclerView to display the data at the specified position.

Next, we will create a layout to be related.

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:background="@color/colorPrimaryDark"
        android:id="@+id/parenttext"
        android:text="parent"
        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:text="Child"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize" />
</LinearLayout>

Next, now that the adapter and layout are complete, let's run it as a program. : point_up:

MainActivity.java


package jp.app.oomae.hisaki.expandable_recyclerview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;

import static jp.app.oomae.hisaki.expandable_recyclerview.Home.makehome;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recyclerview);

        RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 1));
        Home_Adapter adapter = new Home_Adapter(makehome());
        recyclerView.setAdapter(adapter);//Set the adapter
    }
}

And let's set up a recyclerview on the main screen.

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:background="@color/colorAccent"
        android:id="@+id/recyclerview"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

Next, let's decide the number of parents and children to display. This time it is a static implementation, but if you change this, I think it will be dynamic. maybe,,, First, implement the parent and child data groups.

Parent.java


package my guy;
import com.thoughtbot.expandablerecyclerview.models.ExpandableGroup;
import java.util.List;

public class Parent extends ExpandableGroup<Child> {
    public Parent(String title, List<Child> items) {
        super(title, items);
    }
}

Child.java


package my guy;

import android.os.Parcel;
import android.os.Parcelable;

public class Child implements Parcelable {
    private String name;

    public Child(String name) {
        this.name = name;
    }

    protected Child(Parcel in) {
        name = in.readString();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<Child> CREATOR = new Creator<Child>() {
        @Override
        public Child createFromParcel(Parcel in) {
            return new Child(in);
        }

        @Override
        public Child[] newArray(int size) {
            return new Child[size];
        }
    };
}

Next, let's create a house that associates `Parent.java``` with `Child.java```. (There can be no house with multiple parents and multiple children, but it feels like an accommodation ??)

Home.java


package jp.app.oomae.hisaki.expandable_recyclerview;

import java.util.Arrays;
import java.util.List;

public class Home{
    final private static int parents_num = 9;//Number of parents
    final private static int child_num = 5;//Number of children

    public static List<Parent> makehome() {
        Parent[] num = new Parent[parents_num];
        for(int i = 0; i < parents_num; i++) {
            num[i] = new Parent("parent" + String.valueOf(i),receive_child());
        }
        return Arrays.asList(num);
    }

    public static List<Child> receive_child() {
        Child[] num = new Child[child_num];
        for(int i = 0; i < child_num; i++) {
            num[i] = new Child("child" + String.valueOf(i));
        }
        return Arrays.asList(num);
    }
}

Here we combine the child data and the parent data.

That's all the code. We are looking for criticisms and criticisms. : muscle_tone3:

This code

https://github.com/hisakioomae/Expandable_Recyclerview_sample/tree/master

Recommended Posts

Folding and unfolding the contents of the Recyclerview
Customize how to divide the contents of Recyclerview
[Rails] Check the contents of the object
Replace the contents of the Jar file
This and that of the JDK
[Ruby] Display the contents of variables
[Rails] Read the RSS of the site and return the contents to the front
Check the contents of the Java certificate store
Check the contents of params with pry
Now, I've summarized the basics of RecyclerView
About the operation of next () and nextLine ()
[Ruby] Cut off the contents of twitter-ads
Correct the stop position of smoothScrollToPosition of RecyclerView
About the mechanism of the Web and HTTP
Format the contents of LocalDate with DateTimeFormatter
The contents of the data saved by CarrierWave.
[Java] Contents of Collection interface and List interface
Check the version of the JDK installed and the version of the JDK enabled
Output the sum of each name and its contents from a multiple array
JAVA: jar, aar, view the contents of the file
Think about the combination of Servlet and Ajax
Verify the contents of the argument object with Mockito
Compare the speed of the for statement and the extended for statement.
[Java] The confusing part of String and StringBuilder
Table of contents Code refactoring and related articles
I compared the characteristics of Java and .NET
Overwrite the contents of config with Spring-boot + JUnit5
I want to var_dump the contents of the intent
[Ruby] "Reference to object" and "Contents of variable"
Learn the rudimentary mechanism and usage of Gradle 4.4
About next () and nextLine () of the Scanner class
What are the advantages of DI and Thymeleaf?
[Android 9.0 Pie Java] Implement setOnTouchListener in the margin of RecyclerView and close the soft keyboard
ArrayList and the role of the interface seen from List
List the contents of categories created with Active Hash
Please note the division (division) of java kotlin Int and Int
[For beginners] DI ~ The basics of DI and DI in Spring ~
The comparison of enums is ==, and equals is good [Java]
[Grails] About the setting area and the setting items of application.yml
Convert the array of errors.full_messages to characters and output
Test the contents of an Excel file with JUnit
Organizing the current state of Java and considering the future
Java language from the perspective of Kotlin and C #
[CentOS] Download and build the specified version of Git
Until the use of Spring Data and JPA Part 2
Verification of the relationship between Docker images and containers
Until the use of Spring Data and JPA Part 1
I summarized the types and basics of Java exceptions
[Technical memo] About the advantages and disadvantages of Ruby
[Rails] How to get the contents of strong parameters
Java: Use Stream to sort the contents of the collection
[Ruby] Class nesting, inheritance, and the basics of self
Automatically set the width and height of the UI Label
I want to be aware of the contents of variables!
Judgment of the calendar
The world of clara-rules (4)
The world of clara-rules (1)
The world of clara-rules (3)
I want to get a list of the contents of a zip file and its uncompressed size
The world of clara-rules (5)
The idea of quicksort