Android ExpandableListView 的简单使用
来源:互联网 发布:吉他谱曲软件 编辑:程序博客网 时间:2024/06/16 05:17
MAinActivity的代码如下所示:
public class MainActivity extends Activity {private ExpandableListView mExpandableListView;private ArrayList<String> list;private ArrayList<String[]> arrayList;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mExpandableListView = (ExpandableListView) findViewById(R.id.activity_main_expandableListView); //外层布局数据list = new ArrayList<String>();//内层布局数据arrayList = new ArrayList<String[]>();list.add("我的好友");list.add("黑名单");list.add("陌生人");list.add("同事");list.add("老师");arrayList.add(new String[]{"欣哥","张三","李四","王五"});arrayList.add(new String[]{"欣哥","张三","李四","王五"});arrayList.add(new String[]{"张三","李四","王五"});arrayList.add(new String[]{"欣哥","张三","李四","王五"});arrayList.add(new String[]{"张三","李四","王五"}); MyExpandableListViewAdapter adapter = new MyExpandableListViewAdapter(); mExpandableListView.setAdapter(adapter); mExpandableListView.setOnGroupExpandListener(new OnGroupExpandListener() {@Overridepublic void onGroupExpand(int groupPosition) {}});}//ExpandableListViewAdapter组件的父类适配是BaseExpandableListAdapterclass MyExpandableListViewAdapter extends BaseExpandableListAdapter{//外层Item数量@Overridepublic int getGroupCount() {return list.size();}//当前内层Item数量@Overridepublic int getChildrenCount(int groupPosition) {return arrayList.get(groupPosition).length;}//当前外层Item内容@Overridepublic Object getGroup(int groupPosition) {return list.get(groupPosition);}//当前内层Item内容@Overridepublic Object getChild(int groupPosition, int childPosition) {return arrayList.get(groupPosition)[childPosition];}//当前外层Item Id@Overridepublic long getGroupId(int groupPosition) {return groupPosition;}//当前内层Item Id@Overridepublic long getChildId(int groupPosition, int childPosition) {return childPosition;} //是否使用同一个id@Overridepublic boolean hasStableIds() {return false;}//获得当前外层Item的视图@Overridepublic View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {//创建一个TextView的对象TextView text = new TextView(MainActivity.this);//创建一个矩形布局LayoutParams params=new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);//给TextView对象设置宽高text.setLayoutParams(params);//给TextView对象设置padding属性(左,上,右,下)text.setPadding(10, 5, 0, 5);text.setText(list.get(groupPosition));//给TextView设置重心text.setGravity(Gravity.CENTER_HORIZONTAL);return text;}//获得当前内层Item的视图@Overridepublic View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {TextView text = new TextView(MainActivity.this);LayoutParams params=new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);text.setLayoutParams(params);text.setPadding(10, 5, 0, 5);text.setTextColor(Color.RED);text.setGravity(Gravity.CENTER_HORIZONTAL);text.setText(arrayList.get(groupPosition)[childPosition]);return text;}//设置内层视图是否可点击@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {return true;}}}
activity_main.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" tools:context=".MainActivity" > <ExpandableListView android:id="@+id/activity_main_expandableListView" android:layout_width="wrap_content" android:layout_height="wrap_content" /></RelativeLayout>
0 0
- Android ExpandableListView 的简单使用
- android ExpandableListView简单使用
- ExpandableListView的简单使用
- ExpandableListView的简单使用
- ExpandableListView的简单使用
- Android ExpandableListView的使用
- Android ExpandableListView的使用
- Android ExpandableListView的使用
- Android ExpandableListView的使用
- Android ExpandableListView的使用
- Android ExpandableListView的使用
- Android ExpandableListView的使用
- android-ExpandableListView的使用
- android中经常用到的 ExpandableListView 的简单使用
- android--ExpandableListView的简单运用
- Android中ExpandableListView的使用
- Android中ExpandableListView的使用
- Android中ExpandableListView的使用
- ocp-190
- 自动属性
- 初始化器
- 委托
- 泛型
- Android ExpandableListView 的简单使用
- 基于B/S模式分布式图书管理系统研究
- Android开发总结笔记 Spannable(文本样式[上]) 1-1-6-2
- Sql数据库学习
- 20个快乐的习惯
- 软件测试基础知识
- switch case 语句内部变量定义
- 各种数据库连接方式
- 根据客户区大小反推窗口大小