ListView的单选、多选模式:以官方Demo为例
来源:互联网 发布:提高淘宝综合排名多久 编辑:程序博客网 时间:2024/05/13 04:20
1. ListView的choiceMode属性
首先可以看链接的内容,详细解释了以下四种模式。
【ListView多选操作模式详解CHOICE_MODE_MULTIPLE与CHOICE_MODE_MULTIPLE_MODAL:http://blog.csdn.net/jianghejie123/article/details/40860565?utm_source=tuicool&utm_medium=referral】
ListView的choiceMode属性可以在XML中设置(推荐),例如:android:choiceMode="multipleChoice"。
表1,ListView的4种选择属性
选择模式
XML属性
CHOICE_MODE_NONE
默认,不用显式设置
CHOICE_MODE_SINGLE
单选并触发点击事件
CHOICE_MODE_MULTIPLE
多选并触发点击事件
CHOICE_MODE_MULTIPLE_MODAL
多选,但是屏蔽点击事件
2. 官方Demo
2.1 ListView设置
这里以Android6.0 SDK中CustomChoiceList(路径为sdk/samples/android-23/ui/CustomChoiceList,如果没有可以查看android-22,两者一致)为例。
首先看主界面sample_main.xml的设置:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:showDividers="middle" android:divider="?android:dividerHorizontal"> <TextView style="@style/Widget.DescriptionBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/intro_message" /> <!-- When a ListView has a choiceMode set, it will allow users to "choose" one or more items. The framework provides default list item layouts that show standard radio buttons or check boxes next to a single line of text: android.R.layout.simple_list_item_single_choice and android.R.layout.simple_list_item_multiple_choice. In some cases, you may want to customize this layout. When doing so, the root view must implement the Checkable interface. Lastly, remember to use padding on your ListViews to adhere to the standard metrics described in the Android Design guidelines. When doing so, you should set the android:scrollbarStyle such that the scrollbar doesn'isn't inset. --> <ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:paddingLeft="@dimen/page_margin" android:paddingRight="@dimen/page_margin" android:scrollbarStyle="outsideOverlay" android:choiceMode="multipleChoice" /></LinearLayout>
ListView上面一段英文讲了4件重要的注意事件:
1) 设置android:choiceMode="multipleChoice"之后ListView将支持多选;
2) Android的框架提供了标准的用于ListView的radiobuttons和check boxes即android.R.layout.simple_list_item_single_choice和android.R.layout.simple_list_item_multiple_choice,他们都是单独的CheckedTextView(TextView实现了Checkable 接口)。其中android.R.layout.simple_list_item_single_choice的XML文件为:
<?xml version="1.0" encoding="utf-8"?><CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" android:layout_width="match_parent" android:layout_height="?android:attr/listPreferredItemHeightSmall" android:textAppearance="?android:attr/textAppearanceListItemSmall" android:gravity="center_vertical" android:checkMark="?android:attr/listChoiceIndicatorMultiple" android:paddingStart="?android:attr/listPreferredItemPaddingStart" android:paddingEnd="?android:attr/listPreferredItemPaddingEnd" />
3) 在某些情况下,你需要自定义ListView条目(Item)的视图,这个视图的根必须实现Checkable 接口,就像CheckedTextView一样,否则Check的事件不能传入该视图的子视图。
4) 最后,根据官方的UI中关于ListView的设置指南,需要设置ListView的padding值,这样ListView的滑动条不用显示在ListView里面。关于这一条,可以参看:【关于android:scrollbarStyle属性:http://blog.csdn.net/duanyipeng/article/details/8591575】。
2.2自定义ListView的Item
自定义的Item视图必须实现Checkable接口,该接口有3个方法:
/** * Defines an extension for views that make them checkable. * */public interface Checkable { /** * Change the checked state of the view * * @param checked The new checked state */ void setChecked(boolean checked); /** * @return The current checked state of the view */ boolean isChecked(); /** * Change the checked state of the view to the inverse of its current state * */ void toggle();}
Dmeo中采用了线性布局来实现该接口:
/** * This is a simple wrapper for {@link android.widget.LinearLayout} that implements the {@link android.widget.Checkable} * interface by keeping an internal 'checked' state flag. * <p> * This can be used as the root view for a custom list item layout for * {@link android.widget.AbsListView} elements with a * {@link android.widget.AbsListView#setChoiceMode(int) choiceMode} set. */public class CheckableLinearLayout extends LinearLayout implements Checkable { private static final int[] CHECKED_STATE_SET = {android.R.attr.state_checked}; private boolean mChecked = false; public CheckableLinearLayout(Context context, AttributeSet attrs) { super(context, attrs); } public boolean isChecked() { return mChecked; } public void setChecked(boolean b) { if (b != mChecked) { mChecked = b; refreshDrawableState(); } } public void toggle() { setChecked(!mChecked); } @Override public int[] onCreateDrawableState(int extraSpace) { final int[] drawableState = super.onCreateDrawableState(extraSpace + 1); if (isChecked()) { mergeDrawableStates(drawableState, CHECKED_STATE_SET); } return drawableState; }}之后,用该Item作为容器,添加子类:<com.example.android.customchoicelist.CheckableLinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="8dp" android:paddingRight="8dp" android:minHeight="?android:listPreferredItemHeight" android:gravity="center_vertical"> <!-- The duplicateParentState attribute on this TextView, along with the color state list used in the textColor attribute causes its text color to change when its parent is checked or unchecked. --> <TextView android:id="@android:id/text1" android:duplicateParentState="true" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:textAppearance="?android:textAppearanceMedium" android:textColor="@color/hideable_text_color" /> <!-- The duplicateParentState attribute on this ImageView, along with the state list drawable in the src attribute causes its image to change when its parent is checked or unchecked. To use the standard radio or checkmark image, set the src to ?android:listChoiceIndicatorMultiple or ?android:listChoiceIndicatorSingle. These are system theme attributes that reference a state list drawable. --> <ImageView android:src="@drawable/ic_hideable_item" android:duplicateParentState="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="16dp" /></com.example.android.customchoicelist.CheckableLinearLayout>
这里水平放置了一个TextView和一个ImageView。注意,属性android:duplicateParentState="true"保证子类可以根据父类Check的状态而变化,而子类的变化情况都是根据drawable资源文件来设置的。
3. 总结
ListView的单选和多选模式使用频率比较高,官方Dmeo推荐的方式是:
1) 使用AndroidFrameWork推荐的用于ListView的标准单选和多选视图;
2) 自定义Item视图的容器,但必须实现Checkable接口,同时容器里响应的响应类必须支持Check事件,且要设置android:duplicateParentState="true"。
最后,如果是支持ListView特定数量的多选,比如,最多支持5个多选,那么久必须按照【ListView多选操作模式详解CHOICE_MODE_MULTIPLE与CHOICE_MODE_MULTIPLE_MODAL:http://blog.csdn.net/jianghejie123/article/details/40860565?utm_source=tuicool&utm_medium=referral】提到的“愚蠢”方法。
- ListView的单选、多选模式:以官方Demo为例
- ListView的单选模式
- ListView 的单选模式
- 单例模式的缺陷(以php为例)
- ListView的单选和多选模式的使用
- listview的单选/多选模式的实现
- ListView中的单选与多选Demo
- room的使用-以demo为例
- 微信在android平台上开发的注意事项(以官方提供的demo为例):
- 设置 listview为单选模式平且设置选中的item的背景颜色为系统主题颜色
- ListView 单选、多选、模式
- 单例模式 demo
- 比较官方的单例模式
- iOS单例模式的官方写法
- 观察者模式----以ListView和Rxjava为例
- 单例模式的简单demo
- 简单的单例模式Demo
- Android的ListView多选删除Demo
- ==和equals的区别
- 个人android命名(改)
- JAVA的字符集的部分汉字字节数的知识点
- 结构体作为对象的属性
- Eclipse编译class文件无法更新,以及资源文件未生效
- ListView的单选、多选模式:以官方Demo为例
- The server quit without updating PID file
- 【C语言】写冒泡排序可以排序多个字符串。
- BootStrap入门教程 (三)
- 开篇
- [神经网络]1.2-Using neural nets to recognize handwritten digits-Sigmoid neurons(翻译)
- core dump 初级实战
- WebApp_wex5(学习记录一)
- 四元数面向对象