这个项目是Android studio的,eclipse的朋友可以复制一下资源就能用了。
知识点如下:
1、解决listView滚动时checkBox的选择状态会发生变化(絮乱);
2、ListView的Item长按,点击时间并存,实现同一UI控件多需求响应各类事件;
3、点击ListViewItem选中响应Item里的CheckBox;
4、计算选中的项数以及相应UI的隐显增强用户体验;
5、自定义AlertDialog布局。全部自己研究编写,测试运行Ok,为真实项目的所用的简化Demo。
希望和大家分享,若有觉得要改进的地方也请提出。
如上图,初始为隐藏ChcekBox,这个情况下点击ListView的Item只会响应点击时间,弹出自定义AlerDialog。当长按Item后会显示出CheckBox进入选择批量删除的模式,点击Item后选中相应CheckBox,这时滚动list不会改变Chenckbox的选择状态,不会发生絮乱且上方导航标题会显示选择项数,而上方导航栏的UI也相应改变,“返回”字样—>“取消”,"清空"按钮会变成一个删除的垃圾桶图标,点击其可以删除选择的Item,操作完毕后恢复CheckBox隐藏的普通状态。
也许这类的Demo网上有很多,但我找过的几乎都不怎么满意, 既想要滚动listview时不会絮乱,又想checkbox在删除操作时才显示并计算选中的删除项数,而且也应该要把相关UI显示的Activity跟Adapter分开,数据与list分开(网上很多Demo都全部写在一起了)。所以经过自己研究后写出了这个Dmeo。
下面贴些关键的代码,首先看xml的:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="@dimen/navigation_bar_height" android:background="@color/navigation_bar"> <include android:id="@+id/view_navigation_bar_include_returnbtn" layout="@layout/view_function_leftbtn" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_alignParentLeft="true" /> <LinearLayout android:id="@+id/view_navigation_bar_layout_cancelbtn" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:clickable="true" android:paddingLeft="10dp" android:paddingRight="10dp" android:visibility="gone"> <Button android:layout_width="@dimen/goback_btn_width" android:layout_height="@dimen/goback_btn_height" android:layout_gravity="center_vertical" android:layout_marginRight="3dp" android:background="@drawable/selector_goback_btn" android:padding="10dp" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center_vertical" android:text="@string/cancel" android:textColor="@color/white" android:textSize="13sp" /> </LinearLayout> <TextView android:id="@+id/view_navigation_bar_textview_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent&#