ExpandableListView初体验
来源:互联网 发布:二维码制作软件 编辑:程序博客网 时间:2024/05/17 01:45
这次是要实现一个订单号详情展示,以前用过两个ListView嵌套使用的方法,这次突然发现Android本身提供了一个叫ExpandableListView的控件,所以花了点时间研究了下。
废话不多说,直接贴代码:
这是首页,其中Dad和Child只是我写的对象,大家可自行定义。
package com.example.testandroid;import java.util.ArrayList;import java.util.List;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.view.View;import android.widget.ExpandableListView;import android.widget.ExpandableListView.OnChildClickListener;import android.widget.ExpandableListView.OnGroupClickListener;import android.widget.Toast;public class MainActivity extends ActionBarActivity {private ExpandableListView listView;private List<Dad> listDad;private List<List<Child>> listChild;private Adapter adapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);listView = (ExpandableListView) findViewById(R.id.listview);listDad = new ArrayList<>();listChild = new ArrayList<>();adapter = new Adapter(listDad, listChild, this);listView.setAdapter(adapter);initData();}private void initData() {for (int i = 0; i < 5; i++) {listDad.add(new Dad("100000000" + i, "¥1000.00"));}for (int j = 0; j < 5; j++) {List<Child> list = new ArrayList<>();if (j % 2 == 0) {list.add(new Child(0, "宝马", "2", "¥666666"));list.add(new Child(0, "奔驰", "3", "¥888888"));listChild.add(list);} else {list.add(new Child(0, "宝马", "2", "¥666666"));list.add(new Child(0, "奔驰", "3", "¥888888"));list.add(new Child(0, "奥迪", "4", "¥999999"));listChild.add(list);}}adapter.notifyDataSetChanged();// 默认展开全部for (int i = 0; i < listDad.size(); i++) {listView.expandGroup(i);}// 外层的点击监听listView.setOnGroupClickListener(new OnGroupClickListener() {@Overridepublic boolean onGroupClick(ExpandableListView arg0, View arg1,int arg2, long arg3) {// true则不能折叠/展开return true;}});// 内层的点击监听listView.setOnChildClickListener(new OnChildClickListener() {@Overridepublic boolean onChildClick(ExpandableListView arg0, View arg1,int arg2, int arg3, long arg4) {Toast.makeText(MainActivity.this,"这是第" + (arg2 + 1) + "里的第" + (arg3 + 1) + "个", 1000).show();return false;}});}}然后是适配器,需要实现的方法基本上都可以看懂,两个layout分别是外层和内层的,都可自己定义:
package com.example.testandroid;import java.util.List;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseExpandableListAdapter;import android.widget.TextView;public class Adapter extends BaseExpandableListAdapter {public Adapter(List<Dad> listDad, List<List<Child>> listChild,Context context) {super();this.listDad = listDad;this.listChild = listChild;this.context = context;}private List<Dad> listDad;private List<List<Child>> listChild;private Context context;@Overridepublic Object getChild(int arg0, int arg1) {return listChild.get(arg0).get(arg1);}@Overridepublic long getChildId(int arg0, int arg1) {return arg1;}@Overridepublic View getChildView(int arg0, int arg1, boolean arg2, View arg3,ViewGroup arg4) {if (arg3 == null) {LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);arg3 = inflater.inflate(R.layout.child_layout, null);}arg3.setTag(R.layout.dad_layout, arg0);arg3.setTag(R.layout.child_layout, arg1);TextView child_name = (TextView) arg3.findViewById(R.id.child_name);child_name.setText(listChild.get(arg0).get(arg1).getName());TextView child_count = (TextView) arg3.findViewById(R.id.child_count);child_count.setText(listChild.get(arg0).get(arg1).getCount());TextView child_price = (TextView) arg3.findViewById(R.id.child_price);child_price.setText(listChild.get(arg0).get(arg1).getPrice());return arg3;}@Overridepublic int getChildrenCount(int arg0) {return listChild.get(arg0).size();}@Overridepublic Object getGroup(int arg0) {return listDad.get(arg0);}@Overridepublic int getGroupCount() {return listDad.size();}@Overridepublic long getGroupId(int arg0) {return arg0;}@Overridepublic View getGroupView(int arg0, boolean arg1, View arg2, ViewGroup arg3) {if (arg2 == null) {LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);arg2 = inflater.inflate(R.layout.dad_layout, null);}arg2.setTag(R.layout.dad_layout, arg0);arg2.setTag(R.layout.child_layout, -1);TextView dad_num = (TextView) arg2.findViewById(R.id.dad_num);dad_num.setText(listDad.get(arg0).getNumString());TextView dad_part = (TextView) arg2.findViewById(R.id.dad_part);dad_part.setText(listDad.get(arg0).getPart());return arg2;}@Overridepublic boolean hasStableIds() {return false;}@Overridepublic boolean isChildSelectable(int arg0, int arg1) {return true;}}最后是效果图:
如果大家内层里面有checkbox的话,记得在布局里加上 android:focusable="false",否则发现内层监听不能实现,checkbox抢占了焦点
0 0
- ExpandableListView初体验
- ExpandableListView
- ExpandableListView
- ExpandableListView
- ExpandableListView
- ExpandableListView
- ExpandableListView
- expandableListView
- ExpandableListView
- expandablelistview
- ExpandableListview
- ExpandableListView
- ExpandableListView
- ExpandableListView
- Expandablelistview
- ExpandableListView
- ExpandableListView
- ExpandableListView
- 探索关于,Android工程 混淆 proguard. 自定义混淆接口的实现类的内部类混淆处理
- Eclipse 实用操作(1)
- android弹出了一个全局性质的对话框
- android逆向工具
- markdown常用语法
- ExpandableListView初体验
- 2017届南京富士通南大软件校招软件工程师面经
- 我收藏的好多学习网站
- JavaScript在页面中的执行顺序
- 深度解析RabbitMQ集群
- 解决table中<thead>固定,只有列表数据滚动的问题
- iOS - Xcode联网设置
- Utils
- 几种常用的sql分页语句