I want to fold and stretch the contents of the recyclerview!
I referred to this. https://github.com/thoughtbot/expandable-recycler-view This one is easier to understand! !!
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
}
}
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:
https://github.com/hisakioomae/Expandable_Recyclerview_sample/tree/master
Recommended Posts