1.ExpandableListView简介
ExpandableListView是一种用于垂直滚动展示两级列表的视图,和 ListView 的不同之处就是它可以展示两级列表,分组可以单独展开显示子选项。这些选项的数据是通过 ExpandableListAdapter 关联的。
2.xml页面布局
(1)主界面布局(CommonNumberQueryActivity对应布局)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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"android:orientation="vertical"tools:context=".CommonNumberQueryActivity"><TextViewstyle="@style/TitleStyle"android:text="常用号码查询" /><!--可以扩展的listview:ExpandableListView--><ExpandableListViewandroid:id="@+id/elv_common_number"android:layout_width="match_parent"android:layout_height="wrap_content"></ExpandableListView> </LinearLayout>
(2)elv_child_item_group.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:padding="5dp"android:orientation="vertical" ><TextViewandroid:id="@+id/tv_group_name"android:text="分组名称"android:layout_marginLeft="40dp"android:textSize="16sp"android:textColor="@color/red"android:layout_width="match_parent"android:layout_height="wrap_content"/></LinearLayout>
(3)elv_child_item_child.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:padding="5dp"android:orientation="vertical" ><TextViewandroid:id="@+id/tv_name"android:text="电话名称"android:textSize="16sp"android:textColor="#000"android:layout_width="match_parent"android:layout_height="wrap_content"/><TextViewandroid:id="@+id/tv_number"android:text="电话号码"android:textSize="16sp"android:textColor="#000"android:layout_width="match_parent"android:layout_height="wrap_content"/></LinearLayout>
3.java后台代码
package com.example.administrator.test62360safeguard;import android.graphics.Color; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.TypedValue; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.TextView;import com.example.administrator.test62360safeguard.engine.CommonNumberDao;import java.util.List;public class CommonNumberQueryActivity extends AppCompatActivity {ExpandableListView elv_common_number;List<CommonNumberDao.Group> groupList;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_common_number_query);initUI();initData();}/*** 给可扩展的listview:ExpandableListView准备数据,并填充* 首先将对应的数据库文件放入assets目录下*/private void initData() {CommonNumberDao commonNumberDao=new CommonNumberDao();//获取数据库中的数据groupList = commonNumberDao.getGroup();System.out.println("groupList:"+groupList);//给ExpandableListView设置数据适配器elv_common_number.setAdapter(new MyAdapter());}private void initUI() {elv_common_number = findViewById(R.id.elv_common_number);}private class MyAdapter extends BaseExpandableListAdapter {@Overridepublic int getGroupCount() {return groupList.size();}@Overridepublic int getChildrenCount(int groupPosition) {return groupList.get(groupPosition).childList.size();}@Overridepublic CommonNumberDao.Group getGroup(int groupPosition) {return groupList.get(groupPosition);}@Overridepublic CommonNumberDao.Child getChild(int groupPosition, int childPosition) {return groupList.get(groupPosition).childList.get(childPosition);}@Overridepublic long getGroupId(int groupPosition) {return groupPosition;}@Overridepublic long getChildId(int groupPosition, int childPosition) {return childPosition;}/*** 固定写法不需要修改*/@Overridepublic boolean hasStableIds() {return false;}@Overridepublic View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {View view = View.inflate(getApplicationContext(), R.layout.elv_child_item_group, null);TextView tv_group_name = view.findViewById(R.id.tv_group_name);tv_group_name.setText(getGroup(groupPosition).name);return view;}@Overridepublic View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {View view = View.inflate(getApplicationContext(), R.layout.elv_child_item_child, null);TextView tv_name = view.findViewById(R.id.tv_name);TextView tv_number = view.findViewById(R.id.tv_number);tv_name.setText(getChild(groupPosition, childPosition).name);tv_number.setText(getChild(groupPosition, childPosition).number);return view;}/*** @return 孩子节点是否响应事件*/@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {return false;}} }
4.效果图