iT邦幫忙

2021 iThome 鐵人賽

DAY 28
0
Mobile Development

android studio 30天學習筆記系列 第 28

30天學習筆記 -day 28-ExpandableListView

ListView相信各位應該或多或少有使用過,是單層式,項目會一個個排列,而ExpandableListView(抽屜式列表)是雙層式,雖然外觀跟ListView相同,但可以透過點擊項目的方式做頁面的擴展與收縮。

先創三個layout

  1. activity_expandable_list_view_test.xml;在activity的xml檔加入ExpandableListView
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ExpandableListView.ExpandableListViewTestActivity">
    <ExpandableListView
        android:id="@+id/expandableList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="#00FFF7"
        android:childDivider="#EF12FF"
        android:dividerHeight="1dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

android:divider=群組項目的分隔線
android:childDivider=子項目的分隔線
android:dividerHeight=分隔線的粗細,群組項目和子項目統一設定

2.expandable_first_list.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="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="5dp"
        android:layout_marginBottom="20dp"/>
    <TextView
        android:id="@+id/bigtitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="50dp"
        android:layout_weight="1"
        android:textColor="#2B2BFF"
        android:textSize="25sp"/>
</LinearLayout>

3.expandable_second_list.xml;子項目的版面配置

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dp">
        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#FF2121"
            android:textSize="20sp"/>
    </LinearLayout>

</LinearLayout>

創建Adapter

extend BaseExpandableListAdapter可以alt+enter就會將方法設定出來
https://ithelp.ithome.com.tw/upload/images/20210913/20138966yzUbwLKczR.png

public class MyExpandableAdapter extends BaseExpandableListAdapter {
    private ArrayList<String> dapartments;
    private ArrayList<String> classes;
    private LayoutInflater inflater;
    public MyExpandableAdapter(Context context,ArrayList<String> dapartments,ArrayList<String>classes){
        this.inflater=LayoutInflater.from(context);
        this.dapartments=dapartments;
        this.classes=classes;
    }
    @Override
    public int getGroupCount() {//取得群組項目的數量
        return dapartments.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {//取得子項目的數量
        return classes.size();
    }

    @Override
    public Object getGroup(int groupPosition) {//取得群組項目的物件
        return null;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {//取得子項目的物件
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {////取得群組項目的Position ID
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {////取得子項目的Position ID
        return childPosition;
    }

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null){//設置群組項目的view

            convertView = inflater.inflate(R.layout.expandable_first_list,null);
        }
        convertView.setTag(R.layout.expandable_first_list,groupPosition);
        convertView.setTag(R.layout.expandable_first_list,-1);
        TextView textView = convertView.findViewById(R.id.bigtitle);
        textView.setText(dapartments.get(groupPosition));

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null){//設置子項目的View

            convertView = inflater.inflate(R.layout.expandable_second_list,null);
        }
        convertView.setTag(R.layout.expandable_second_list,groupPosition);
        convertView.setTag(R.layout.expandable_second_list,-1);
        TextView child1 = convertView.findViewById(R.id.title);
        child1.setText(classes.get(childPosition));
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {//定義子項目是否可以被點擊
        return false;
    }
}

ExpandableListViewTestActivity設定

public class ExpandableListViewTestActivity extends AppCompatActivity {
    //初始化
    private MyExpandableAdapter adapter;
    private ExpandableListView expandableListView;
    private ArrayList<String>dapartments=new ArrayList<>();//存入項目群組的資料
    private ArrayList<String> classes = new ArrayList<>();//存入子項目的資料
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_expandable_list_view_test);
        setDate();
        
        expandableListView=findViewById(R.id.expandableList);
        //將資料傳入MyExpandableAdapter,expandableListView設定adapter
        adapter=new MyExpandableAdapter(this,dapartments,classes);
        expandableListView.setAdapter(adapter);
    }
}

設置資料

將科系跟班級存放資料

private void setDate() {
        String[] department={"資料處理科","企業管理系","會計系"};
            dapartments.add(department[0]);
            dapartments.add(department[1]);
            dapartments.add(department[2]);

        String[] Studrnt1Class={"1年1班","1年2班","1年3班"};
            classes.add(Studrnt1Class[0]);
            classes.add(Studrnt1Class[1]);
            classes.add(Studrnt1Class[2]);
    }

這樣就完成了~
成圖:
https://ithelp.ithome.com.tw/upload/images/20211011/20138966HVRIljAkom.jpg
https://ithelp.ithome.com.tw/upload/images/20211011/201389665tGpQBzYmP.jpg


上一篇
30天學習筆記 -day 27-Motion Editor(下篇)
下一篇
30天學習筆記 -day 29-bottomsheetDialog
系列文
android studio 30天學習筆記30

尚未有邦友留言

立即登入留言