ExpandableListView扩展下拉列表
来源:互联网 发布:立体设计软件下载 编辑:程序博客网 时间:2024/05/21 04:44
MainActivity.java
package com.cccrunner.csdn.myapplication1;import android.graphics.Color;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.Gravity;import android.view.View;import android.view.ViewGroup;import android.widget.AbsListView;import android.widget.BaseExpandableListAdapter;import android.widget.ExpandableListAdapter;import android.widget.ExpandableListView;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;/** * @author 王德强 * 功能:ExpandableListView扩展下拉列表 */public class MainActivity extends AppCompatActivity { String[] type = new String[]{"我的好友","大学同学","亲戚朋友"}; String[][] info = new String[][]{{"张三","张思","王五"},{"李四","李斯"},{"王五","王六","王二","王三"}}; int[] groupImgs = new int[]{R.drawable.g1,R.drawable.g2,R.drawable.g3}; int[][] imgIds = new int[][]{{R.drawable.a1,R.drawable.a2,R.drawable.a3}, {R.drawable.a4,R.drawable.a5,R.drawable.a6}, {R.drawable.a7,R.drawable.a8,R.drawable.a9,R.drawable.a10} }; ExpandableListAdapter myAdapter = new BaseExpandableListAdapter() { @Override public int getGroupCount() { return type.length; } private TextView getTextView(){//自己定义的一个获取TextView的方法 AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); //设置宽度和高度 TextView textView = new TextView(MainActivity.this); textView.setLayoutParams(lp); textView.setGravity(Gravity.CENTER_VERTICAL); textView.setTextSize(20); textView.setPadding(30,0,0,0); textView.setTextColor(Color.BLACK); return textView; } @Override public int getChildrenCount(int groupPosition) { return info[groupPosition].length; } @Override public Object getGroup(int groupPosition) { return type[groupPosition]; } @Override public Object getChild(int groupPosition, int childPosition) { return info[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 false; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { LinearLayout layout = new LinearLayout(MainActivity.this); layout.setOrientation(LinearLayout.HORIZONTAL); layout.setGravity(Gravity.CENTER_VERTICAL); ImageView groupImg = new ImageView(MainActivity.this); groupImg.setImageResource(groupImgs[groupPosition]); layout.addView(groupImg); TextView textView = getTextView(); textView.setText(getGroup(groupPosition).toString()); layout.addView(textView); return layout; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { LinearLayout layout = new LinearLayout(MainActivity.this); layout.setOrientation(LinearLayout.HORIZONTAL); layout.setPadding(20,0,0,0); ImageView itemImage = new ImageView(MainActivity.this); itemImage.setPadding(20,0,0,0); itemImage.setImageResource(imgIds[groupPosition][childPosition]); layout.addView(itemImage); TextView textView = getTextView(); textView.setText(getChild(groupPosition,childPosition).toString()); layout.addView(textView); return layout; } //子项是否可以选择 @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ExpandableListView myExpandable = (ExpandableListView) findViewById(R.id.myExpandable); myExpandable.setAdapter(myAdapter); }}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"><ExpandableListView android:id="@+id/myExpandable" android:layout_width="match_parent" android:layout_height="match_parent" android:drawSelectorOnTop="false" ></ExpandableListView></LinearLayout>
阅读全文
0 0
- ExpandableListView扩展下拉列表
- 扩展下拉列表ExpandableListView(一)
- 扩展下拉列表ExpandableListView(二)
- 可扩展的下拉列表—ExpandableListView
- ExpandableListView可扩展的下拉列表简单的使用
- ExpandableListView------下拉列表
- ExpandableListView(下拉列表)实现
- android ExpandableListView可扩展列表
- android ExpandableListView可扩展列表
- ExpandableListView 实现QQ的下拉列表
- 可扩展列表视图(ExpandableListView)
- Android UI ExpandableListView实现扩展列表
- android中用ExpandableListView实现三级扩展列表
- 可扩展的列表组件(ExpandableListView)
- android 列表ListView和可扩展列表ExpandableListView的实现
- 【Android基础篇】使用ExpandableListView实现自定义的下拉列表
- 【Android基础篇】使用ExpandableListView实现自定义的下拉列表
- android 实现QQ好友列表(扩展listview:ExpandableListView)
- 6月3号学习笔记
- C语言——念整数
- 学习一个ZKW线段树
- 常见问题
- Android5.0系统wifi一直在向wps发送scan问题
- ExpandableListView扩展下拉列表
- cygwin中文输入
- ASP.NET MVC实现权限管理以及权限分配
- linux命令说明
- nginx 编译安装与配置
- SpringBoot 自定义properties
- oracle11g 数据库可用内存使用和分配
- Android NDK日志篇(二)
- Android中Fragment生命周期