【Android】ExpandableListView示例
来源:互联网 发布:kx tda200控制软件 编辑:程序博客网 时间:2024/06/05 20:41
ExpandableListView使用示例:
首先是布局文件:
<LinearLayout 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" android:orientation="vertical" > <ExpandableListView android:id="@+id/expandableListView1" android:layout_width="match_parent" android:layout_height="wrap_content" > </ExpandableListView></LinearLayout>
下面是主函数:
public class MainActivity extends Activity {public List<String> groups; //组列表public List<List<String>> child; //子列表ExpandableListView expandableListView;mExpandableListAdapter adapter; //数据适配器@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);expandableListView = (ExpandableListView) findViewById(R.id.expandableListView1);groups = new ArrayList<String>();child = new ArrayList<List<String>>(); addInfo("张胜男", new String[]{"女","漂亮"}); //添加数据addInfo("李子豪", new String[]{"男","帅气"});addInfo("万古琴", new String[]{"女","温婉"});addInfo("佘诗曼", new String[]{"女","好人"});expandableListView.setAdapter(new mExpandableListAdapter());}private void addInfo(String g, String[] c){groups.add(g);List<String> childItem = new ArrayList<String>();for(int i = 0; i < c.length; i++){childItem.add(c[i]);}child.add(childItem);}class mExpandableListAdapter extends BaseExpandableListAdapter{@Overridepublic Object getChild(int groupPosition, int childPosition) {return child.get(groupPosition).get(childPosition);}@Overridepublic long getChildId(int groupPosition, int childPosition) {return childPosition;}@Overridepublic View getChildView(int arg0, int arg1, boolean arg2, View arg3,ViewGroup arg4) {String s = child.get(arg0).get(arg1);return getGenericView(s);}@Overridepublic int getChildrenCount(int groupPosition) {return child.get(groupPosition).size();}@Overridepublic Object getGroup(int arg0) {return groups.get(arg0);}@Overridepublic int getGroupCount() {return groups.size();}@Overridepublic long getGroupId(int arg0) {return arg0;}@Overridepublic View getGroupView(int groupPosition, boolean arg1, View convertView, ViewGroup arg3) {String s = groups.get(groupPosition);return getGenericView(s);}//创建组/子视图 public TextView getGenericView(String s) { // Layout parameters for the ExpandableListView AbsListView.LayoutParams lp = new AbsListView.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, 64); TextView text = new TextView(MainActivity.this); text.setLayoutParams(lp); // Center the text vertically text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); // Set the text starting position text.setPadding(36, 0, 0, 0); text.setText(s); return text; } @Overridepublic boolean hasStableIds() {return false;}@Overridepublic boolean isChildSelectable(int arg0, int arg1) {return true;}}}运行结果如下图:
0 0
- 【Android】ExpandableListView示例
- ExpandableListView的使用示例
- ExpandableListView使用示例
- ExpandableListView 使用示例
- ExpandableListView Android
- android ExpandableListView
- android ExpandableListView
- android expandablelistview
- android ExpandableListView
- android ExpandableListView
- android ExpandableListView
- Android ExpandableListView
- Android ExpandableListView
- Android ExpandableListView
- ExpandableListView的简单代码示例
- Android ExpandableListView显示模板
- android ExpandableListView使用
- android-ExpandableListView(2)
- 2016【太原网络营销师】郭文军讲解如何优化网站长尾关键词
- PHP 程序员解决问题能力的八个级别
- Android开发-前后端控制器intent
- 配置Oracle Gateway 12连接到SQL server 2014
- 动态分配内存
- 【Android】ExpandableListView示例
- Linux下安装配置php5.5和nginx
- cppcheck工具的使用
- Mybatis中mapper文件中的两层循环
- nfs
- Android历代版本号
- win8.1下面的iis6.x配置跨域
- 阿里云Centos端口转发
- 八大排序算法