listview,左划删除,选中,改变选中行状态,同时切换未选中行状态,最好还能随意扩展listview的item样式,以上这些功能在一两百行代码中完成,核心代码怕是只有几十行,极易复用!代码可能很烂,求指点,可以喷,拍砖,请不要侮辱人格。
重点说个差不多了,开始点儿废话,着急的兄弟直接略过。稍微看一下自己的博客,又好长时间没写日志了⊙﹏⊙b汗,还好学习还算没有太过拉下。希望大家不要像我一样。这次提供的日志内容也是因为我自己项目中有以上的功能需求,而网上的代码动辄挺复杂,所以自己在尽量挑选个代码不太多且简单的基础上扩张了自己的功能。吃水不忘挖井人,本文内容基于:http://blog.csdn.net/g540203430/article/details/40108089;在本文的基础上,做了选中与未选中的状态切换(刚好日志下面就有小伙伴喊咋没有点击事件,O(∩_∩)O哈哈~)。当然看看人家的blog水平,太汗颜了,真的差距很大,不怕差距大,慢慢努力追赶呗,毕竟自己接触android还不是太久。上面的日志是g540203430的,应该是鹏飞哥?该兄弟不只是提供了代码与介绍,甚至于学习方法。强烈建议大家去看一下。
下面直接上干货:其实只是在人家实现好的代码里加上了一点点改变。⊙﹏⊙b汗。代码很简单,只是希望节省像我一样的菜鸟的一点点时间。
希望大家有时间的时候都学好android的各种技术!
private class ScrollViewScrollImpl implements OnTouchListener {
/** 记录开始时的坐标 */
private float startX = 0;
private TextView lastView;//改变选中状态的时候记录上一次选中的view.
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// 如果有划出删除按钮的itemView,就让他滑回去并且锁定本次touch操作,解锁会在父组件的dispatchTouchEvent中进行
if (mScrollView != null) {
scrollView(mScrollView, HorizontalScrollView.FOCUS_LEFT);
mScrollView = null;
mLockOnTouch = true;
return true;
}
startX = event.getX();
break;
case MotionEvent.ACTION_UP:
HorizontalScrollView view = (HorizontalScrollView) v;
// 如果滑动了>50个像素,就显示出删除按钮
if (startX > event.getX() + 50) {
startX = 0;// 因为公用一个事件处理对象,防止错乱,还原startX值
scrollView(view, HorizontalScrollView.FOCUS_RIGHT);
mScrollView = view;
} else {
scrollView(view, HorizontalScrollView.FOCUS_LEFT);
ViewHolder holder = (ViewHolder) v.getTag();
if(lastView!=null){
lastView.setTextColor(Color.RED);
}
lastView=holder.infoTextView;
holder.infoTextView.setTextColor(Color.BLACK);
}
break;
}
return false;
}
}
下面聊一下item样式扩展 :暂时我的处理方法如下,因为我暂时只需要两个item样式,后期我肯定还会持续优化,只是抛一下思路,大家都可以指点下
//改item样式的代码并没有用在上传的下载资源中,只是给大家个思路
int resourceId = R.layout.item_horizontal_slide_listview;//item资源id int other=0;//改变item后,增加了相应字段,导致无法适配多出来的item内部组件,我是把多出来的内部组件长度写死,再减掉。我确实是太懒惰了,我绝对会进一步优化的 public HorizontalSlideAdapter(Context context, List<String> objects,String flag) { super(context, 0, objects); if(flag.equals("ADDRESS")){ resourceId = R.layout.item_horizontal_slide_listview; } if(flag.equals("BANKCARD")) { resourceId = R.layout.item_horizontal_slidecard_listview; other=180;//这儿是像素 } // 搞到屏幕宽度 Display defaultDisplay = ((Activity) context).getWindowManager() .getDefaultDisplay(); DisplayMetrics metrics = new DisplayMetrics(); defaultDisplay.getMetrics(metrics); mScreenWidth = metrics.widthPixels; mParams = new LinearLayout.LayoutParams(mScreenWidth-other, LinearLayout.LayoutParams.MATCH_PARENT);
//原item样式
<?xml version="1.0" encoding="utf-8"?> <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbars="none"> <!-- 这里因为父布局是HorizontalScrollView,所以宽度写match_parent不会生效--> <LinearLayout android:layout_width="wrap_content" android:layout_height="50dp" android:orientation="horizontal"> <!-- 这里的宽度之后会在代码里面动态改变 --> <TextView android:id="@+id/item_text" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginRight="10dp" android:background="@color/white" android:drawableRight="@mipmap/my_uncheck" android:gravity="center_vertical" android:padding="10dp" android:textColor="#3d3d3d" android:textSize="14sp" /> <Button android:id="@+id/item_delete" android:layout_width="wrap_content" android:layout_height="match_parent" android:background="@color/deletepink" android:text="删除" android:textColor="@color/white" android:textSize="16sp" /> </LinearLayout> </HorizontalScrollView>
//新item:
<?xml version="1.0" encoding="utf-8"?> <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbars="none"> <!-- 这里因为父布局是HorizontalScrollView,所以宽度写match_parent不会生效--> <LinearLayout android:layout_width="wrap_content" android:layout_height="50dp" android:background="@color/white" android:orientation="horizontal"> <LinearLayout android:layout_width="90dp" android:layout_height="wrap_content" android:background="@color/white" android:gravity="center_horizontal" android:layout_gravity="center" android:orientation="horizontal"> <TextView android:id="@+id/my_bank_name" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center" android:padding="10dp" android:text="建设银行" android:textColor="#597fac" android:textSize="12sp" /> <TextView android:layout_width="1dp" android:layout_height="match_parent" android:layout_margin="7dp" android:background="#999999" android:gravity="center" android:text="" /> </LinearLayout> <!-- 这里的宽度之后会在代码里面动态改变 --> <TextView android:id="@+id/item_text" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginRight="10dp" android:background="@color/white" android:drawableRight="@mipmap/my_uncheck" android:gravity="center_vertical" android:padding="10dp" android:textColor="#3d3d3d" android:textSize="13sp" /> <Button android:id="@+id/item_delete" android:layout_width="wrap_content" android:layout_height="match_parent" android:background="@color/deletepink" android:text="删除" android:textColor="@color/white" android:textSize="14sp" /> </LinearLayout> </HorizontalScrollView>//黑色背景的代码是用于实际项目中的,在下载的代码包大家是看不到的,只做参考。
android listview左划删除与点击事件下载地址
http://download.csdn.net/detail/lj489695332/9233679
可点击