关于ExpandableListAdapter
2016-09-23 16:21
302 查看
package jannonx.com.myapplication; import android.database.DataSetObserver; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.ExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.ExpandableListView.OnChildClickListener; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { ExpandableListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ExpandableListView) findViewById(R.id.list); final MyExpandableListAdapter adapter = new MyExpandableListAdapter(); listView.setAdapter(adapter); //为ExpandableListView的子列表单击事件设置监听器 listView.setOnChildClickListener(new OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "你单击了:" + adapter.getChild(groupPosition, childPosition), Toast.LENGTH_LONG).show(); return true; } }); //创建ExpandableListAdapter方式二 // ExpandableListAdapter adapter = new BaseExpandableListAdapter() { // int[] logos = new int[]{ // R.drawable.fw02, // R.drawable.fw05, // R.drawable.fw06, // R.drawable.fw07 // }; // private String[] armTypes = new String[]{ // "WORD", "EXCEL", "EMAIL", "PPT" // }; // private String[][] arms = new String[][]{ // {"文档编辑", "文档排版", "文档处理", "文档打印"}, // {"表格编辑", "表格排版", "表格处理", "表格打印"}, // {"收发邮件", "管理邮箱", "登录登出", "注册绑定"}, // {"演示编辑", "演示排版", "演示处理", "演示打印"}, // }; // // @Override // public int getGroupCount() { // return armTypes.length; // } // // @Override // public int getChildrenCount(int groupPosition) { // return arms[groupPosition].length; // } // // @Override // public Object getGroup(int groupPosition) { // return armTypes[groupPosition]; // } // // @Override // public Object getChild(int groupPosition, int childPosition) { // return arms[groupPosition][childPosition]; // } // // @Override // public long getGroupId(int groupPosition) { // return groupPosition; // } // // @Override // public long getChildId(int groupPosition, int childPosition) { // return childPosition; // } // // @Override // public boolean hasStableIds() { // return true; // } // // @Override // public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { // LinearLayout ll = new LinearLayout(MainActivity.this); // ll.setOrientation(LinearLayout.HORIZONTAL); // ImageView logo = new ImageView(MainActivity.this); // logo.setImageResource(logos[groupPosition]); // logo.setPadding(36, 15, 0, 0); // ll.addView(logo); // TextView textView = getTextView(); // textView.setText(getGroup(groupPosition).toString()); // textView.setPadding(10, 0, 0, 0); // ll.addView(textView); // return ll; // } // // @Override // public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { // TextView textView = getTextView(); // textView.setText(getChild(groupPosition, childPosition).toString()); // return textView; // } // // @Override // public boolean isChildSelectable(int groupPosition, int childPosition) { // return true; // } // // @Over 4000 ride // public boolean areAllItemsEnabled() { // return false; // } // // @Override // public boolean isEmpty() { // return false; // } // // @Override // public void onGroupExpanded(int groupPosition) { // // } // // @Override // public void onGroupCollapsed(int groupPosition) { // // } // // @Override // public long getCombinedChildId(long groupId, long childId) { // return 0; // } // // @Override // public long getCombinedGroupId(long groupId) { // return 0; // } // // private TextView getTextView() { // AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 64); // TextView textView = new TextView(MainActivity.this); // textView.setLayoutParams(lp); // textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); // textView.setPadding(36, 0, 0, 0); // textView.setTextSize(20); // return textView; // } // }; // listView.setAdapter(adapter); } class MyExpandableListAdapter implements ExpandableListAdapter { int[] logos = new int[]{ R.drawable.fw02, R.drawable.fw05, R.drawable.fw06, R.drawable.fw07 }; private String[] armTypes = new String[]{ "WORD", "EXCEL", "EMAIL", "PPT" }; private String[][] arms = new String[][]{ {"文档编辑", "文档排版", "文档处理", "文档打印"}, {"表格编辑", "表格排版", "表格处理", "表格打印"}, {"收发邮件", "管理邮箱", "登录登出", "注册绑定"}, {"演示编辑", "演示排版", "演示处理", "演示打印"}, }; private int[][] armsImage=new int[][]{ {R.drawable.fw07,R.drawable.fw06,R.drawable.fw05,R.drawable.fw02,}, {R.drawable.fw07,R.drawable.fw06,R.drawable.fw05,R.drawable.fw02,}, {R.drawable.fw07,R.drawable.fw06,R.drawable.fw05,R.drawable.fw02,}, {R.drawable.fw07,R.drawable.fw06,R.drawable.fw05,R.drawable.fw02,}, }; @Override public void registerDataSetObserver(DataSetObserver observer) { } @Override public void unregisterDataSetObserver(DataSetObserver observer) { } @Override public int getGroupCount() { return armTypes.length; } @Override public int getChildrenCount(int groupPosition) { return arms[groupPosition].length; } @Override public Object getGroup(int groupPosition) { return armTypes[groupPosition]; } @Override public Object getChild(int groupPosition, int childPosition) { return arms[groupPosition][childPosition]; } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return true; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { LinearLayout ll = new LinearLayout(MainActivity.this); ll.setOrientation(LinearLayout.HORIZONTAL); ImageView logo = new ImageView(MainActivity.this); logo.setImageResource(logos[groupPosition]); logo.setPadding(36, 15, 0, 0); ll.addView(logo); TextView textView = getTextView(); textView.setText(getGroup(groupPosition).toString()); textView.setPadding(10, 0, 0, 0); ll.addView(textView); return ll; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { LinearLayout ll=new LinearLayout(MainActivity.this); ll.setOrientation(LinearLayout.HORIZONTAL); ImageView imageView=new ImageView(MainActivity.this); imageView.setImageResource(armsImage[groupPosition][childPosition]); imageView.setPadding(50,15,0,0); ll.addView(imageView); TextView textView = getTextView(); textView.setText(getChild(groupPosition, childPosition).toString()); textView.setPadding(5,0,0,0); textView.setTextSize(18); ll.addView(textView); return ll; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } @Override public boolean areAllItemsEnabled() { return false; } @Override public boolean isEmpty() { return false; } @Override public void onGroupExpanded(int groupPosition) { } @Override public void onGroupCollapsed(int groupPosition) { } @Override public long getCombinedChildId(long groupId, long childId) { return 0; } @Override public long getCombinedGroupId(long groupId) { return 0; } private TextView getTextView() { AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 64); TextView textView = new TextView(MainActivity.this); textView.setLayoutParams(lp); textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); textView.setPadding(36, 0, 0, 0); textView.setTextSize(20); return textView; } } }
相关文章推荐
- php基础之函数入门
- 浏览器版本不同导致文件无法下载问题解决方法
- node npm grunt工具 package.json Gruntfile.js相关
- easyui 扩展layout的方法,支持动态添加删除块
- Android color资源
- scala的sliding window的多种实现和benchMark
- leetcode No65. Valid Number
- 10种最流行的国外博客程序
- Javascript数组及常用操作
- 个别VC6.0+用vs2012打开后调试提示系统找不到文件
- 解决adb无法连接手机问题
- 关于object转换类型
- 接上篇简单的Memcache应用
- 转:初学 Java Web 开发,请远离各种框架,从 Servlet 开发
- 技术填坑记
- 惯性导航领域书籍集锦
- Web前端面试指导(十一):样式导入有哪些方式?
- JTA事物
- 关于php pdo连接mysql,查询超时问题
- 简单总结下OPENCV访问像素值的方法