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


<<:  Day30 赛後心得

>>:  [Day29]程序菜鸟自学C++资料结构演算法 – 桶排序法(Bucket sort)

【Day 2】Google Apps Script - 平台介绍

什麽是 Google Apps Script ??。 今日要点: 》Google Apps Scr...

D4: [漫画]工程师太师了-第2.5话

工程师太师了: 第2.5话 杂记: 报名的时候没注意到有最低300个字限制阿~~~~(抱头 本来只想...

DAY28 - [React] useContext 概念篇

今日文章目录 前言 参考资料 前言 在 Day25-[React] props 中我们练习用pro...

Day21 - Spinner(一)

Spinner下拉式选单 这也很常用在表单中 如:选择地区、居住地、选择语言、学校 那今天就来学习点...

[Day 18] Migration

当多人一起开发专案时,Migration可以让团队修改、设定资料库的内容,像是资料库的版本控制,会纪...