第八课:Activity 及其子类的使用二:ExpandableListActivity
来源:互联网 发布:ubuntu uget chrome 编辑:程序博客网 时间:2024/06/07 22:01
例1:使用 ExpandableListActivity 实现可展开的 Activity
public class ExpandableListActivityTest extends ExpandableListActivity{public void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);ExpandableListAdapter adapter = new BaseExpandableListAdapter(){int[] logos = new int[]{R.drawable.p,R.drawable.z,R.drawable.t}; private String[] armTypes = new String[]{ "神族兵种", "虫族兵种", "人族兵种"};private String[][] arms = new String[][]{{ "狂战士", "龙骑士", "黑暗圣堂", "电兵" },{ "小狗", "刺蛇", "飞龙", "自爆飞机" },{ "机枪兵", "护士MM" , "幽灵" }};//获取指定组位置、指定子列表项处的子列表项数据@Overridepublic Object getChild(int groupPosition, int childPosition){return arms[groupPosition][childPosition];}@Overridepublic long getChildId(int groupPosition, int childPosition){return childPosition;}@Overridepublic int getChildrenCount(int groupPosition){return arms[groupPosition].length;}private TextView getTextView(){AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 64);TextView textView = new TextView(ExpandableListActivityTest.this);textView.setLayoutParams(lp);textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);textView.setPadding(36, 0, 0, 0);textView.setTextSize(20);return textView;}//该方法决定每个子选项的外观@Overridepublic View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent){TextView textView = getTextView();textView.setText(getChild(groupPosition, childPosition).toString());return textView;}//获取指定组位置处的组数据@Overridepublic Object getGroup(int groupPosition){return armTypes[groupPosition];}@Overridepublic int getGroupCount(){return armTypes.length;}@Overridepublic long getGroupId(int groupPosition){return groupPosition;}//该方法决定每个组选项的外观@Overridepublic View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent){LinearLayout ll = new LinearLayout(ExpandableListActivityTest.this);ll.setOrientation(0);ImageView logo = new ImageView(ExpandableListActivityTest.this);logo.setImageResource(logos[groupPosition]);ll.addView(logo);TextView textView = getTextView();textView.setText(getGroup(groupPosition).toString());ll.addView(textView);return ll;}@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition){return true;}@Overridepublic boolean hasStableIds(){return true;}};// 设置该窗口显示列表setListAdapter(adapter);}}
例2:xml布局的方式使用 ExpandableListActivity
1.首先在main.xml添加ExpandableListView
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ExpandableListView android:id="@id/android:list" //ExpandableListView的ID为Android自定义,不可改变 android:layout_width="fill_parent" android:layout_height="fill_parent"android:background="@drawable/background"/></LinearLayout>
2.为父目录创建布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/parent_group" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="8px" android:paddingLeft="50px" android:paddingBottom="6px" android:textColor="#ff00ff" android:textStyle="bold"/></LinearLayout>
3.为子目录创建布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="20dp" android:orientation="horizontal"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="20px" android:paddingTop="4dp" android:background="@drawable/child_image"/> <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/child_group" android:textSize="20sp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="地址:xx省xx市xx"/> </LinearLayout></LinearLayout>
4.DisplayExpandableList.java
package com.yin;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import android.app.ExpandableListActivity;import android.os.Bundle;import android.widget.ExpandableListAdapter;import android.widget.ExpandableListView;import android.widget.SimpleExpandableListAdapter;public class DisplayExpandableList extends ExpandableListActivity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);List<HashMap<String, String>> parent_groups = new ArrayList<HashMap<String, String>>();String[] parent_group_names = {"我的好友","初中同学","高中同学","大学同学","黑名单"};parent_groups = this.addParentItems(parent_groups, parent_group_names);List<List<HashMap<String, String>>> child_groups = new ArrayList<List<HashMap<String, String>>>();List<HashMap<String, String>> child_lists = new ArrayList<HashMap<String, String>>();child_groups = this.addChildsItems(child_groups, child_lists,"小华");/** * SimpleExpandableListAdapter的参数那是相当的多啊 * 参数 1:context * 2:父级目录的数据 * 3:父级目录的布局文件 * 4: 夫级目录的数据来源 * 5:指定父级目录显示数据的控件 * 6:子级目录的数据 * 7:子级目录的布局文件 * 8:子级目录的数据来源 * 9:指定子级目录显示数据的控件 */SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(this, parent_groups, R.layout.parent_layout,new String[]{"parent_group"}, new int[]{R.id.parent_group},child_groups, R.layout.child_layout, new String[]{"child_group"},new int[]{R.id.child_group});setListAdapter(adapter);}//为每个父母目录下子目录添加数据public List<List<HashMap<String, String>>> addChildsItems(List<List<HashMap<String, String>>> child_groups,List<HashMap<String, String>> child_list, String item) {child_groups = new ArrayList<List<HashMap<String, String>>>();child_list = new ArrayList<HashMap<String, String>>();HashMap<String, String> map = new HashMap<String, String>();map.put("child_group", item);child_list.add(map);child_groups.add(child_list);return child_groups;}//为父级目录分组,添加标识public List<HashMap<String, String>> addParentItems(List<HashMap<String, String>> parent_groups, String[] parent_group_names) {parent_groups = new ArrayList<HashMap<String, String>>();for(int i=0;i<parent_group_names.length;i++){HashMap<String, String> groups = new HashMap<String, String>();groups.put("parent_group", parent_group_names[i]);parent_groups.add(groups);}return parent_groups;}}
后续链接: Activity 及其子类的使用三:PreferenceActivity
源代码下载
0 0
- 第八课:Activity 及其子类的使用二:ExpandableListActivity
- 【Android】Activity类及其子类:LauncherActivity、PreferenceActivity和ExpandableListActivity
- 第十课:Activity 及其子类的使用四:配置 Activity
- UI组件之AdapterView及其子类(六)ExpandableListView组件和ExpandableListActivity的使用
- 第七课:Activity 及其子类的使用一:LauncherActivity
- 第九课:Activity 及其子类的使用三:PreferenceActivity
- 可展开的Activity ExpandableListActivity的使用
- 使用ExpandableListActivity实现可展开的Activity
- Android之对话框AlertDialog及其子类的使用(二)
- [Android] ExpandableListActivity的使用
- ExpandableListActivity的使用
- ExpandableListActivity 的使用
- Activity子类ListActivity的使用
- Android ExpandableListActivity实现可展开的activity
- ExpandableListActivity的学习和使用
- ViewAnimator及其子类ViewSwitcher,ViewFipper的使用
- UI组件之AdapterView及其子类(二)GridView网格视图的使用
- Android之ProgressBar及其子类SeekBar和RatingBar的使用(二)
- PHP下使用CURL方式POST数据至API接口的代码
- [Redis专辑][1]ubuntu12.04下安装php-redis的方法和步骤
- STL set的用法总结
- 高性能网络编程2----TCP消息的发送
- 永和维护---从问题中得到的一些感受
- 第八课:Activity 及其子类的使用二:ExpandableListActivity
- C++中类的内存分布
- chrome浏览器的一些常用的前端开发快捷键
- firefox的缓存目录位置、清除、修改
- WaitForSingleObject 的问题
- 不能包含中文的正则表达式
- 使用JUC并发工具包的Lock和Condition,实现生产者和消费者问题中的有界缓存
- 【ThinkPHP学习】ThinkPHP自动转义存储富文本编辑器内容导致读取出错
- 从头到尾彻底理解KMP