1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 027 Android 可扩展的listview:ExpandableListView的使用案例

027 Android 可扩展的listview:ExpandableListView的使用案例

时间:2020-07-10 00:53:06

相关推荐

027 Android 可扩展的listview:ExpandableListView的使用案例

1.ExpandableListView简介

ExpandableListView是一种用于垂直滚动展示两级列表的视图,和 ListView 的不同之处就是它可以展示两级列表,分组可以单独展开显示子选项。这些选项的数据是通过 ExpandableListAdapter 关联的。

2.xml页面布局

(1)主界面布局(CommonNumberQueryActivity对应布局)

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"xmlns:app="/apk/res-auto"xmlns:tools="/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="/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="/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.monNumberDao;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.效果图

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。