การสร้าง Expandable ListView

  • แก้ไขไฟล์ activity_main.xml
  • แก้ไขไฟล์ MainActivity.java
  • เพิ่ม Package ชื่อ ExpandableList
  • เพิ่มไฟล์ ชื่อ ExpandableList/ChildItem.java และ layout/child_item.xml
  • เพิ่มไฟล์ ชื่อ ExpandableList/GroupItem.java และ layout/group_item.xml
  • เพิ่มไฟล์ ชื่อ ExpandableList/ChildViewHolder.java และ ExpandableList/GroupViewHolder.java
  • เพิ่มไฟล์ ชื่อ ExpandableList/CustomAdapter.java

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ExpandableListView
        android:id="@+id/expandableListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:indicatorLeft="?android:attr/expandableListPreferredItemIndicatorLeft"
        android:listSelector="#ffc" />

</LinearLayout>

MainActivity.java

package com.phaisarn.expandable;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;

import com.phaisarn.expandable.ExpandableList.ChildItem;
import com.phaisarn.expandable.ExpandableList.CustomAdapter;
import com.phaisarn.expandable.ExpandableList.GroupItem;

import java.util.ArrayList;
import java.util.Arrays;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ExpandableListView expandableListView = findViewById(R.id.expandableListView);
        final ArrayList<GroupItem> groupArray = new ArrayList<>();

        ArrayList<ChildItem> childArray;
        GroupItem groupItem;
        String groupName;

        groupName = "Asia";
        childArray = new ArrayList<>();
        childArray.addAll(Arrays.asList(
                new ChildItem("Thailand"),
                new ChildItem("Japan"),
                new ChildItem("Korea")));
        groupItem = new GroupItem(groupName, childArray);
        groupArray.add(groupItem);

        groupName = "Europe";
        childArray = new ArrayList<>();
        childArray.addAll(Arrays.asList(
                new ChildItem("UK"),
                new ChildItem("France"),
                new ChildItem("Germany"),
                new ChildItem("Spain")));
        groupItem = new GroupItem(groupName, childArray);
        groupArray.add(groupItem);

        groupName = "America";
        childArray = new ArrayList<>();
        childArray.addAll(Arrays.asList(
                new ChildItem("USA"),
                new ChildItem("Canada"),
                new ChildItem("Mexico")));
        groupItem = new GroupItem(groupName, childArray);
        groupArray.add(groupItem);

        groupName = "Africa";
        childArray = new ArrayList<>();
        childArray.addAll(Arrays.asList(
                new ChildItem("Nigeria"),
                new ChildItem("Cameroon"),
                new ChildItem("Morocco")));
        groupItem = new GroupItem(groupName, childArray);
        groupArray.add(groupItem);

        groupName = "Latin";
        childArray = new ArrayList<>();
        childArray.addAll(Arrays.asList(
                new ChildItem("Brazil"),
                new ChildItem("Argentina"),
                new ChildItem("Peru"),
                new ChildItem("Chile")));
        groupItem = new GroupItem(groupName, childArray);
        groupArray.add(groupItem);

        CustomAdapter adapter = new CustomAdapter(this, groupArray);
        expandableListView.setAdapter(adapter);
        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView exp, View view, int i, int i1, long l) {
                //ใช้เมธอดของคลาส GroupItem และ ChildItem
                String item = groupArray.get(i).childItems.get(i1).text;
                Toast.makeText(getBaseContext(), item, Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    }
}

ExpandableList/ChildItem.java

package com.phaisarn.expandable.ExpandableList;

public class ChildItem {
    public String text;

    public ChildItem(String text) {
        this.text = text;
    }
}

layout/child_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:descendantFocusability="blocksDescendants"
    android:orientation="vertical">

    <TextView
        android:id="@+id/child_textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="7dp"
        android:paddingLeft="40dp"
        android:paddingTop="7dp"
        android:textSize="16sp" />

</LinearLayout>

ExpandableList/GroupItem.java

package com.phaisarn.expandable.ExpandableList;

import java.util.ArrayList;

public class GroupItem {
    public String text;
    public ArrayList<ChildItem> childItems;

    public GroupItem(String text, ArrayList<ChildItem> childItems) {
        this.text = text;
        this.childItems = childItems;
    }
}

layout/group_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#def"
    android:orientation="vertical">

    <TextView
        android:id="@+id/group_textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="40dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:textSize="20sp" />
</LinearLayout>

ExpandableList/ChildViewHolder.java

package com.phaisarn.expandable.ExpandableList;

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

import com.phaisarn.expandable.R;

public class ChildViewHolder {
    TextView textView;

    public ChildViewHolder(View view) {
        textView = view.findViewById(R.id.child_textView);
    }
}

ExpandableList/GroupViewHolder.java

package com.phaisarn.expandable.ExpandableList;

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

import com.phaisarn.expandable.R;

public class GroupViewHolder {
    TextView textView;

    public GroupViewHolder(View view) {
        textView = view.findViewById(R.id.group_textView);
    }
}

ExpandableList/CustomAdapter.java

package com.phaisarn.expandable.ExpandableList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;

import com.phaisarn.expandable.R;

import java.util.ArrayList;

public class CustomAdapter extends BaseExpandableListAdapter {
    private Context mContext;
    private ArrayList<GroupItem> mGroupItems;
    private GroupViewHolder mGroupHolder;
    private ChildViewHolder mChildHolder;

    public CustomAdapter(Context context, ArrayList<GroupItem> items) {
        mContext = context;
        mGroupItems = items;
    }

    @Override
    public int getGroupCount() {
        return mGroupItems.size();
    }

    //ให้ gPos => groupPosition, cPos => childPosition
    @Override
    public int getChildrenCount(int gPos) {
        return mGroupItems.get(gPos).childItems.size();
    }

    @Override
    public Object getGroup(int gPos) {
        return mGroupItems.get(gPos);
    }

    @Override
    public Object getChild(int gPos, int cPos) {
        return null;
    }

    @Override
    public long getGroupId(int gPos) {
        return gPos;
    }

    @Override
    public long getChildId(int gPos, int cPos) {
        return cPos;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int gPos, int cPos) {
        return true;
    }

    @Override
    public View getGroupView(int gPos, boolean isLast, View view, ViewGroup viewGroup) {
        if (view == null) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            view = inflater.inflate(R.layout.group_item, viewGroup, false);
            mGroupHolder = new GroupViewHolder(view);
            view.setTag(mGroupHolder);
        } else {
            mGroupHolder = (GroupViewHolder) view.getTag();
        }
        GroupItem groupItemInfo = (GroupItem) getGroup(gPos);
        mGroupHolder.textView.setText(groupItemInfo.text);
        return view;
    }

    @Override
    public View getChildView(int gPos, int cPos, boolean isLast, View view, ViewGroup viewGroup) {

        if (view == null) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            view = inflater.inflate(R.layout.child_item, viewGroup, false);
            mChildHolder = new ChildViewHolder(view);
            view.setTag(mChildHolder);
        } else {
            mChildHolder = (ChildViewHolder) view.getTag();
        }
        ArrayList<ChildItem> childItems = mGroupItems.get(gPos).childItems;
        mChildHolder.textView.setText(childItems.get(cPos).text);
        return view;
    }
}