这个例子是小弟上网搜了相关资料和文章和看了APIdemo后自己实现的一个例子,用在小项目中。都是从小学习到大的,所以都知道在不会的时候很想找人帮自己一把。希望这个对正在研究Android的兄弟(姐们)们有帮助。
不知道怎么显示截图,只能放在附件里,sorry!不多说,上菜。
1。首先:定义xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ExpandableListView
android:id="@+id/ex_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
我的截图中有不相关的控件定义,所以在这省略不写了。自己把握。
2。定义一个Adapter来继承BaseExpandableListView,并实现相关方法。
package com.ldci.second.tools;
import com.ldci.second.mypnone.CallInActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class CallInExpandableAdapter extends BaseExpandableListAdapter {
CallInActivity GIS;
public CallInExpandableAdapter(CallInActivity _GIS) {
super();
GIS = _GIS;
}
//父菜单内容和子菜单选项内容
private String[] in_groups = {"来电背景", "字体设置","号码设置"};
public String[][] in_children = {
{"启用背景图片"},
{"字体颜色","字体大小"},
{"号码颜色","号码大小"}
};
//**************************************
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
Log.v("MyAdapter","pos="+groupPosition);
TextView view = getGenericView();
view.setText(getGroup(groupPosition).toString());
return view;
}
public TextView getGenericView() {
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, 45);
TextView tv = new TextView(GIS);
tv.setTextSize(30);
tv.setLayoutParams(lp);
// Set the text starting position
tv.setPadding(36, 0, 0, 0);
return tv;
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public Object getGroup(int groupPosition) {
return in_groups[groupPosition];
}
public int getGroupCount() {
return in_groups.length;
}
//子列表属性设置
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView view = getChildTextView(groupPosition, childPosition);
view.setTextSize(24);
return view;
}
public TextView getChildTextView(int gos, int cos) {
TextView view = new TextView(GIS);
view.setText(in_children[gos][cos]);
return view;
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public Object getChild(int groupPosition, int childPosition) {
return in_children[groupPosition][childPosition];
}
public int getChildrenCount(int groupPosition) {
return in_children[groupPosition].length;
}
//**************************************
public boolean hasStableIds() {
return true;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
3。就是自己写一个Activity来显示在界面上,直观的看到效果
package com.ldci.second.mypnone;
import java.util.Timer;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.Toast;
import android.widget.ExpandableListView.OnChildClickListener;
import com.ldci.second.tools.CallInExpandableAdapter;
import com.ldci.second.tools.PhoneNumService;
import com.ldci.second.tools.ServiceStateMark;
public class CallInActivity extends Activity{
CallInExpandableAdapter adapter;
ExpandableListView expandableList;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.call_in);
adapter = new CallInExpandableAdapter(CallInActivity.this);
expandableList = (ExpandableListView) findViewById(R.id.ex_list);
expandableList.setAdapter(adapter);
expandableList.setOnChildClickListener(new OnChildClickListener(){
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
if(groupPosition==0 && childPosition==0){
}
if(groupPosition== 1&& childPosition==0){
}
if(groupPosition== 1&& childPosition==1){
}
if(groupPosition== 2&& childPosition==0){
}
if(groupPosition== 2&& childPosition==1){
}
return false;
}
});
}
}
三步下来就会实现ExpandableListView的效果。代码肯定有不足之处,欢迎大家指正,共同进步
- 大小: 13.3 KB
- 大小: 18.7 KB
分享到:
相关推荐
ExpandableListView实现购物车页面
Android使用ExpandableListview实现时间轴 博客:http://www.jianshu.com/u/7cc46191eb2c
Android自定义ExpandableListView实现二级列表
基于ExpandableListView实现评论和回复的功能。 > 说明 提供了模拟数据,实现评论和回复的插入数据 对评论和回复数据进行了异常处理 material design风格的详情页 处理了NestedScrollView、ExpandableListView和...
Android使用ExpandableListview实现时间轴TimeLineDemo
两种实现城市列表的方法,ExpandableListView推荐使用这种方法。
Android 源码开源 Expandablelistview实现,仿QQ 人员列表,聊天的实现,包括图片,语音 自制Shape Android 源码开源 Expandablelistview实现,仿QQ 人员列表,聊天的实现,包括图片,语音 自制Shape Android 源码...
android ExpandableListView 四级树形目录 可无限延伸。
android listview,ExpandableListView实现单选,多选,全选,edittext实现批量输入
基于ExpandableListView实现评论和回复的功能
Android ExpandableListView双层嵌套实现三级树形菜单
本demo实现功能为:Expandablelistview子项checkBox先获取已选择状态,在次过程中可修改子项选择,最终提交选择子类的项,并打印相关数据
记录了Android开发的学习历程,本demo为可拓展的列表控件ExpandableListView的使用方法,并含有要点整理文档。
android ExpandableListView实现分组购物车,方便商品的分组展示,并实现一键全选、取消等功能
Android购物车
通过重写ExpandableListView实现分组列表的滚动加载。可以用于评论回复功能中。
ExpandableListView嵌套GridView的实现,
android学习基础知识,不懂的童鞋看看,一定有收获的~
主要介绍了Android listview ExpandableListView实现多选,单选,全选,edittext实现批量输入的实例代码,非常不错,具有参考借鉴价值,需要的朋友可以参考下
ExpandableListView实现时间轴效果 ,不会让大家失望的!!!效果不错,博客有截图