按钮形式的可展开的listview
发布日期:2022-02-14 23:02:39
浏览次数:21
分类:技术文章
本文共 10879 字,大约阅读时间需要 36 分钟。
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <ExpandableListView android:id="@+id/el_list" android:layout_width="120dp" android:layout_height="715dp" android:layout_gravity="left" android:background="#ffffff" android:cacheColorHint="#abcdefff" android:listSelector="#cdefabdc" > <!-- android:cacheColorHint="#00000000",这个设置拖动view时背景色 android:listSelector="#00000000" ,设置选中时的底色 --> </ExpandableListView> <ExpandableListView android:id="@+id/el_list2" android:layout_width="120dp" android:layout_height="698dp" android:background="#ffffff" android:cacheColorHint="#abcdefff" android:listSelector="#cdefabdc" > <!-- android:cacheColorHint="#00000000",这个设置拖动view时背景色 android:listSelector="#00000000" ,设置选中时的底色 --> </ExpandableListView> <ExpandableListView android:id="@+id/el_list3" android:layout_width="120dp" android:layout_height="698dp" android:background="#ffffff" android:cacheColorHint="#abcdefff" android:listSelector="#cdefabdc" > <!-- android:cacheColorHint="#00000000",这个设置拖动view时背景色 android:listSelector="#00000000" ,设置选中时的底色 --> </ExpandableListView> <Button android:layout_width="120dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="返回"/></LinearLayout>
主函数
package com.example.test;
import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.BaseExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.ExpandableListView.OnChildClickListener; import android.widget.LinearLayout; import android.widget.TextView; public class TestExpandableListView2 extends Activity { private ExpandableListView mView; private ExpandableListView mView2; private ExpandableListView mView3; private String[] generalsTypes = new String[] { "作物: " }; //子视图显示文字 private String[][] generals = new String[][] { { "小麦", "玉米", "花生", "高粱", "水稻", "大豆" }}; private String[] generalsTypes2 = new String[] { "/亩" }; //子视图显示文字 private String[][] generals2 = new String[][] { { "200-250kg", "250-300kg", "300-400kg", "400-500kg" }}; private String[] generalsTypes3 = new String[] { "施肥模式" }; //子视图显示文字 private String[][] generals3 = new String[][] { { "化肥", "复合肥", "配方肥"}}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.expandable_listview2); mView =(ExpandableListView) findViewById(R.id.el_list); mView.setAdapter(new MyAdapter()); mView2 =(ExpandableListView) findViewById(R.id.el_list2); mView2.setAdapter(new MyAdapter2()); mView3 =(ExpandableListView) findViewById(R.id.el_list3); mView3.setAdapter(new MyAdapter3()); //点击子条目的时候更新主条目的文字 mView.setOnChildClickListener(new OnChildClickListener() { public boolean onChildClick(ExpandableListView v, View arg1, int group, int child, long arg4) { if(child==0){ generalsTypes = new String[] { "作物:小麦" }; mView.setAdapter(new MyAdapter()); } if(child==1){ generalsTypes = new String[] { "作物:玉米" }; mView.setAdapter(new MyAdapter()); } if(child==2){ generalsTypes = new String[] { "作物:花生" }; mView.setAdapter(new MyAdapter()); } if(child==3){ generalsTypes = new String[] { "作物:高粱" }; mView.setAdapter(new MyAdapter()); } if(child==4){ generalsTypes = new String[] { "作物:水稻" }; mView.setAdapter(new MyAdapter()); }if(child==5){ generalsTypes = new String[] { "作物:大豆" }; mView.setAdapter(new MyAdapter()); } return false; } }); mView2.setOnChildClickListener(new OnChildClickListener() { public boolean onChildClick(ExpandableListView v, View arg1, int group, int child, long arg4) { if(child==0){ generalsTypes2 = new String[] { "200-250kg/亩" }; mView2.setAdapter(new MyAdapter2()); } if(child==1){ generalsTypes2 = new String[] { "250-300kg/亩" }; mView2.setAdapter(new MyAdapter2()); } if(child==2){ generalsTypes2 = new String[] { "300-400kg/亩" }; mView2.setAdapter(new MyAdapter2()); } if(child==3){ generalsTypes2 = new String[] { "400-500kg/亩" }; mView2.setAdapter(new MyAdapter2()); } return false; } }); mView3.setOnChildClickListener(new OnChildClickListener() { public boolean onChildClick(ExpandableListView v, View arg1, int group, int child, long arg4) { if(child==0){ generalsTypes3 = new String[] { "化肥" }; mView3.setAdapter(new MyAdapter3()); } if(child==1){ generalsTypes3 = new String[] { "复合肥" }; mView3.setAdapter(new MyAdapter3()); } if(child==2){ generalsTypes3 = new String[] { "配方肥" }; mView3.setAdapter(new MyAdapter3()); } return false; } }); } class MyAdapter extends BaseExpandableListAdapter { //自己定义一个获得textview的方法 TextView getTextView() { //设置条目的宽高 AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 50); TextView textView = new TextView(TestExpandableListView2.this); textView.setLayoutParams(lp); textView.setGravity(Gravity.CENTER_VERTICAL); textView.setPadding(36, 0, 0, 0); textView.setTextSize(20); textView.setTextColor(Color.BLACK); return textView; } @Override public int getGroupCount() { return generalsTypes.length; } @Override public int getChildrenCount(int groupPosition) { return generals[groupPosition].length; } @Override public Object getGroup(int groupPosition) { //返回的数据在后面填主条目充用 return generalsTypes[groupPosition]; } @Override public Object getChild(int groupPosition, int childPosition) { //返回的数据在后面填充子条目用 return generals[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 //获得主条目view对象 public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { LinearLayout ll = new LinearLayout(TestExpandableListView2.this); ll.setOrientation(0); // ImageView logo = new ImageView(ExpandableList.this); // logo.setImageResource(logos[groupPosition]); // logo.setPadding(50, 0, 0, 0); // ll.addView(logo); TextView textView = getTextView(); textView.setTextColor(Color.BLUE); textView.setText(getGroup(groupPosition).toString()); ll.addView(textView); ll.setPadding(6, 0, 0, 0); return ll; } @Override //将子条目内容加载到childview public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { LinearLayout ll = new LinearLayout(TestExpandableListView2.this); ll.setOrientation(0); TextView textView = getTextView(); textView.setText(getChild(groupPosition, childPosition).toString()); ll.addView(textView); return ll; } public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } } class MyAdapter2 extends BaseExpandableListAdapter { //自己定义一个获得textview的方法 TextView getTextView() { //设置条目的宽高 AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 50); TextView textView = new TextView(TestExpandableListView2.this); textView.setLayoutParams(lp); textView.setGravity(Gravity.CENTER_VERTICAL); textView.setPadding(36, 0, 0, 0); textView.setTextSize(20); textView.setTextColor(Color.BLACK); return textView; } @Override public int getGroupCount() { return generalsTypes2.length; } @Override public int getChildrenCount(int groupPosition) { return generals2[groupPosition].length; } @Override public Object getGroup(int groupPosition) { //返回的数据在后面填主条目充用 return generalsTypes2[groupPosition]; } @Override public Object getChild(int groupPosition, int childPosition) { //返回的数据在后面填充子条目用 return generals2[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; } //获得主条目view对象,一个textview用于显示主条目 public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { LinearLayout ll = new LinearLayout(TestExpandableListView2.this); ll.setOrientation(0); TextView textView = getTextView(); textView.setTextColor(Color.BLUE); textView.setText(getGroup(groupPosition).toString()); ll.addView(textView); ll.setPadding(6, 0, 0, 0); return ll; } @Override //将子条目内容加载到childview public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { LinearLayout ll = new LinearLayout(TestExpandableListView2.this); ll.setOrientation(0); TextView textView = getTextView(); textView.setText(getChild(groupPosition, childPosition).toString()); ll.addView(textView); return ll; } public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } } class MyAdapter3 extends BaseExpandableListAdapter { //自己定义一个获得textview的方法 TextView getTextView() { //设置条目的宽高 AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 50); TextView textView = new TextView(TestExpandableListView2.this); textView.setLayoutParams(lp); textView.setGravity(Gravity.CENTER_VERTICAL); textView.setPadding(36, 0, 0, 0); textView.setTextSize(20); textView.setTextColor(Color.BLACK); return textView; } @Override public int getGroupCount() { return generalsTypes3.length; } @Override public int getChildrenCount(int groupPosition) { return generals3[groupPosition].length; } @Override public Object getGroup(int groupPosition) { //返回的数据在后面填主条目充用 return generalsTypes3[groupPosition]; } @Override public Object getChild(int groupPosition, int childPosition) { //返回的数据在后面填充子条目用 return generals3[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; } //获得主条目view对象,一个textview用于显示主条目 public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { LinearLayout ll = new LinearLayout(TestExpandableListView2.this); ll.setOrientation(0); TextView textView = getTextView(); textView.setTextColor(Color.BLUE); textView.setText(getGroup(groupPosition).toString()); ll.addView(textView); ll.setPadding(6, 0, 0, 0); return ll; } @Override //将子条目内容加载到childview public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { LinearLayout ll = new LinearLayout(TestExpandableListView2.this); ll.setOrientation(0); TextView textView = getTextView(); textView.setText(getChild(groupPosition, childPosition).toString()); ll.addView(textView); return ll; } public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } } }源码地址 http://download.csdn.net/detail/fei900724/8705943
转载地址:https://blog.csdn.net/fei900724/article/details/45766985 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月07日 07时18分59秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
tensorflow环境准备
2019-04-26
julia-Char字符String字符串(第7讲)
2019-04-26
Java Web 网络商城案例演示十五 订单详情功能(提交订单支付界面)
2019-04-26
时间如漏斗
2019-04-26
自学编程,我该如何寻找自学资源?(含视频)
2019-04-26
android su程序分析
2019-04-26
android Superuser.apk源码分析
2019-04-26
Android从触碰屏幕开始的事件采集,解析及分发(1)
2019-04-26
福利来袭,免费获取 Android 开发资料以及直播课程
2019-04-26
优势丧失
2019-04-26
【上市啦】“Python 之父” 力荐的蓝皮书,你知道是哪本吗?
2019-04-26
Python 爬虫面试题 170 道:2019 版
2019-04-26
歪门邪道
2019-04-26
我的前六年程序生涯
2019-04-26
知识地图
2019-04-26
罗马总会建成
2019-04-26
程序通过技术赚钱的八个途径
2019-04-26
我在爬坡阶段
2019-04-26
大疆机甲大师教育机器人Python开发:中文命名变量初尝试
2019-04-26
大疆机甲大师教育机器人Python开发:API中文化初尝试
2019-04-26