027 Android 可扩展的listview:ExpandableListView的使用案例
生活随笔
收集整理的這篇文章主要介紹了
027 Android 可扩展的listview:ExpandableListView的使用案例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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.效果圖
?
轉載于:https://www.cnblogs.com/luckyplj/p/10861234.html
總結
以上是生活随笔為你收集整理的027 Android 可扩展的listview:ExpandableListView的使用案例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 软件设计作业 1
- 下一篇: C++开发WPF,开发环境配置