自定义listview,带按钮,多选框,处理item事件
产品中要用到这个效果:listitem中带多选框,用来删除;有按钮,处理相应事件;item的click处理跳转。同事说这个设计操作起来很二,有点反社会,再加个图片和可以展开就反人类了。不管怎样,产品说了,我们就得做。
思路:item上有控件,就不能使item失去焦点,因此,各种button,checkbox就不能去获取焦点,但是可以click。其实主要就这些,其他也没多少东西。
1、首先贴个效果图,全选/取消全选,删除,item的单击,checkbox的单击,button的单击:
2、代码部分:
MainActivity.java
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:minHeight="30dip" android:paddingLeft="6dp" android:paddingRight="6dp" android:paddingTop="4dp" > <CheckBox android:id="@+id/cbSelect" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:clickable="true" android:focusable="false" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_toLeftOf="@+id/btClick" android:layout_toRightOf="@+id/cbSelect" android:orientation="vertical" > <TextView android:id="@+id/tvContent" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@android:color/black" android:text="content" /> <TextView android:id="@+id/tvItemID" android:textColor="@android:color/black" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="id" /> </LinearLayout> <Button android:id="@+id/btClick" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:clickable="true" android:focusable="false" android:text="click" /></RelativeLayout>
Demo:下载
Pulltorefresh:csdn下载,github下载
先导入pulltorefresh library,然后再运行demo
4、有很多改进的地方,欢迎吐槽