首页 技术 正文
技术 2022年11月14日
0 收藏 496 点赞 3,166 浏览 4768 个字

layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <ExpandableListView
android:id="@+id/expandable_lv"
android:layout_width="match_parent"
android:layout_height="match_parent"></ExpandableListView></RelativeLayout>

expandable_childe_item.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:orientation="vertical" >
<TextView
android:id="@+id/child_text"
android:layout_height="wrap_content"
android:layout_width="match_parent"/></LinearLayout>

expandable_group_item.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:orientation="vertical" >
<TextView
android:id="@+id/group_text"
android:layout_height="wrap_content"
android:layout_width="match_parent"/></LinearLayout>

main.java

package com.example.day09_expandablelistview;import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.TextView;public class MainActivity extends Activity {
private int lastGroupPosition = -1;
private ExpandableListView expandable_lv;
private String groupData[] = {"同事","老师","朋友"};
private String childData[][] = {{"小小","小明","饭饭","流浪"},{"李老师","张老师","吴老师","肖老师","柳老师"},{"雯雯","哔哔","嘻嘻"}};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
expandable_lv = (ExpandableListView) findViewById(R.id.expandable_lv);
//给ExpandableListAdapter设置适配器---自定义适配器需要继承BaseExpandableListAdapter()实现其中的方法
MyExpandableListAdapter myExpandableListAdapter = new MyExpandableListAdapter();
//设置适配器
expandable_lv.setAdapter(myExpandableListAdapter);
//去掉group默认的箭头
expandable_lv.setGroupIndicator(null);
//设置组可拉伸的监听器,拉伸时会调用其中的onGroupExpand()方法
expandable_lv.setOnGroupExpandListener(new OnGroupExpandListener() { @Override
public void onGroupExpand(int groupPosition) {
/**
* 实现打开只能打开一个组的功能,打开一个组,已将打开的组会自动收缩
*/
if(lastGroupPosition != groupPosition){
expandable_lv.collapseGroup(lastGroupPosition);
}
lastGroupPosition = groupPosition;
}
});
//设置组收缩的监听器,收缩时会调用其中的onGroupCollapse()方法
expandable_lv.setOnGroupCollapseListener(new OnGroupCollapseListener() { @Override
public void onGroupCollapse(int groupPosition) { }
});
}
/**
* 自定义实现类继承BaseExpandandableListAdapter
* @author my
*
*/
class MyExpandableListAdapter extends BaseExpandableListAdapter{
/**
* 得到组的数量
*/
@Override
public int getGroupCount() {
return groupData.length;
}
/**
* 得到每个组的元素的数量
*/
@Override
public int getChildrenCount(int groupPosition) {
return childData[groupPosition].length;
}
/**
* 获得组的对象
*/
@Override
public Object getGroup(int groupPosition) {
return groupData[groupPosition];
}
/**
* 获得子对象
*/
@Override
public Object getChild(int groupPosition, int childPosition) {
return childData[groupPosition][childPosition];
}
/**
* 得到组id
*/
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
/**
* 得到子id
*/
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
/**
* 表示数据是否稳定,对监听事件有影响
*/
@Override
public boolean hasStableIds() {
return true;
}
/**
* 确定一个组的展示视图--groupPosition表示的当前需要展示的组的索引
*/
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
LayoutInflater systemService = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = systemService.inflate(R.layout.enpandable_group_item, null);
TextView group_text = (TextView) view.findViewById(R.id.group_text);
group_text.setText(groupData[groupPosition]);
return view;
}
/**
* 确定一个组的一个子的展示视图--groupPostion表示当前组的索引,childPosition表示的是需要展示的子的索引
*/
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
LayoutInflater systemService = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = systemService.inflate(R.layout.enpandable_child_item, null);
TextView child_text = (TextView) view.findViewById(R.id.child_text);
child_text.setText(childData[groupPosition][childPosition]);
return view;
} @Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
} }}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,075
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,893