ExpandableListView小结
来源:互联网 发布:java web获取项目路径 编辑:程序博客网 时间:2024/06/02 16:15
Java代码: package com.example.expandablelistviewdemo;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import android.os.Bundle;import android.app.Activity;import android.content.Context;import android.view.Menu;import android.view.View;import android.widget.CheckedTextView;import android.widget.ExpandableListView;import android.widget.TextView;import android.widget.ExpandableListView.OnChildClickListener;/** * Example application with ExpandableListView and CheckedTextView as list item. * Texts of selected list items are displayed in parent view. * * @author Lauri Nevala * * */public class MainActivity extends Activity {private SettingsListAdapter adapter;private ExpandableListView categoriesList;private ArrayList<Category> categories;protected Context mContext;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mContext = this;categoriesList = (ExpandableListView)findViewById(R.id.categories);categories = Category.getCategories();adapter = new SettingsListAdapter(this, categories, categoriesList); categoriesList.setAdapter(adapter); categoriesList.expandGroup(0); categoriesList.setOnChildClickListener(new OnChildClickListener() {@Overridepublic boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition, long id) {CheckedTextView checkbox = (CheckedTextView)v.findViewById(R.id.list_item_text_child);checkbox.toggle();// find parent view by tagView parentView = categoriesList.findViewWithTag(categories.get(groupPosition).name);if(parentView != null) {TextView sub = (TextView)parentView.findViewById(R.id.list_item_text_subscriptions);if(sub != null) {Category category = categories.get(groupPosition);if(checkbox.isChecked()) {// add child category to parent's selection listcategory.selection.add(checkbox.getText().toString());// sort list in alphabetical orderCollections.sort(category.selection, new CustomComparator());}else {// remove child category from parent's selection listcategory.selection.remove(checkbox.getText().toString());}// display selection listsub.setText(category.selection.toString());}}return true;}});}public class CustomComparator implements Comparator<String> { @Override public int compare(String o1, String o2) { return o1.compareTo(o2); } } @Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}activity_main.xml布局代码:<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:background="@drawable/food" tools:context=".MainActivity" ><ExpandableListView android:id="@+id/categories" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true"> categoriesList.expandGroup(0);</ExpandableListView></RelativeLayout>setting_list_item_child.xml代码:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/list_item_child" android:gravity="center_vertical"> <CheckedTextView android:id="@+id/list_item_text_child" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:drawableLeft="?android:attr/listChoiceIndicatorMultiple" android:checked="false" android:gravity="center_vertical" android:padding="10dp"android:layout_weight="1"android:layout_marginLeft="35dp" /></LinearLayout> settings_list_item_parent.xml代码:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:layout_width="fill_parent"android:layout_height="fill_parent"android:id="@+id/list_item"><RelativeLayout android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_gravity="center_vertical" > <TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:id="@+id/list_item_text_view"style="?android:attr/preferenceStyle"android:textSize="15sp"android:paddingBottom="3dp"android:paddingTop="15dp"android:layout_marginLeft="45dp" /><TextView android:id="@+id/list_item_text_subscriptions" style="?android:attr/preferenceStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/list_item_text_view" android:layout_marginLeft="45dp" android:textColor="#696969" android:textSize="12sp" android:paddingBottom="3dp" /></RelativeLayout> </LinearLayout>
显示好友QQ那样的列表,可以展开,可以收起,在android中,以往用的比较多的是listview,虽然可以实现列表的展示,但在某些情况下,我们还是希望用到可以分组并实现收缩的列表,那就要用到android的ExpandableListView,天研究了一下这个的用法,也参考了很多资料动手写了一个小demo,实现了基本的功能,但界面优化方面做得还不够好,有待改进。下面直接上效果图以及源代码如上~!
0 0
- ExpandableListView小结
- android expandablelistview使用小结
- Android ExpandableListView使用小结
- 【ExpandableListView】---3 小结和练习
- Android ExpandableListView使用小结(一)
- Android ExpandableListView使用小结(二)
- ExpandableListView
- ExpandableListView
- ExpandableListView
- ExpandableListView
- ExpandableListView
- ExpandableListView
- expandableListView
- ExpandableListView
- expandablelistview
- ExpandableListview
- ExpandableListView
- ExpandableListView
- Linux下的文件内容查阅
- Python中split的用法
- 银行卡BIN码大全
- LED恒流驱动芯片
- linux 查看当前用户id、whoami用户列表、用户组、用户权限/etc/passwd、/etc/group、/etc/shadow以及切换用户身份、su、sudo、/etc/sudoers
- ExpandableListView小结
- 1004. 成绩排名
- Core Data入门
- Mina框架学习笔记(一)
- DWR3.0框架入门(3) —— ScriptSession的维护及优化
- Ajax 处理xml
- css3 filter 实现滤镜 模糊 改变色相 黑白照等功能
- 将Android\IOS迁移至Win10
- Visual Studio 调试小技巧(2)-从查看窗口得到更多信息