做开发有一段时间了。发现做的东西有时候会记不起来。决定开始写博客记录起来
最近项目中需要实现 ListView侧滑出现删除按钮的功能,刚开始自己尝试通过OnItemClick和OnTouch 来实现。编写过程中发现两个事件冲突问题一直得不到很好的解决,在网上找了一下相关源码,终于找到了解决方法。直接贴码吧:
这是Adapter的代码
public class CutItemAdapter extends BaseAdapter {
private List<ApkInfo> arrays = null;
private Context mContext;
private Button curDel_btn;
private float x, ux;
private MainActivity mActivity;
public CutItemAdapter(MainActivity activity, Context mContext,
List<ApkInfo> arrays) {
this.mContext = mContext;
this.arrays = arrays;
mActivity = activity;
}
@Override
public int getCount() {
return this.arrays.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View view, ViewGroup arg2) {
ViewHolder viewHolder = null;
if (view == null) {
viewHolder = new ViewHolder();
view = LayoutInflater.from(mContext).inflate(R.layout.apkinfo_item,
null);
viewHolder.riv_friendsIcon = (RoundImageView) view.findViewById(R.id.img_icon);
viewHolder.tv_friendsName = (TextView) view.findViewById(R.id.tv_nikename);
viewHolder.tv_friendsLastMsg = (TextView) view.findViewById(R.id.tv_msg);
viewHolder.tv_friendsLastMsgTime = (TextView) view.findViewById(R.id.tv_msgTime);
viewHolder.bt_deleitem = (Button) view.findViewById(R.id.bt_del);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
// 换掉了原来listview中的onItemClick
view.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
ViewHolder holderV = (ViewHolder) v.getTag();
Intent intent = new Intent();
intent.putExtra("nickName", holderV.tv_friendsName.getText());
intent.setClass(mContext, TalkFriendsActivity.class);
mContext.startActivity(intent);
}
});
// 为每一个view项设置触控监听
view.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
final ViewHolder holder = (ViewHolder) v.getTag();
// 当按下时处理
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// //设置背景为选中状态
// v.setBackgroundResource(R.drawable.mm_listitem_pressed);
// 获取按下时的x轴坐标
x = event.getX();
// 判断之前是否出现了删除按钮如果存在就隐藏
if (curDel_btn != null) {
if (curDel_btn.getVisibility() == View.VISIBLE) {
curDel_btn.setVisibility(View.GONE);
return true;
}
}
} else if (event.getAction() == MotionEvent.ACTION_UP) {// 松开处理
// 设置背景为未选中正常状态
// v.setBackgroundResource(R.drawable.mm_listitem_simple);
// 获取松开时的x坐标
ux = event.getX();
// 判断当前项中按钮控件不为空时
if (holder.bt_deleitem != null) {
// 按下和松开绝对值差当大于20时显示删除按钮,否则不显示
if (Math.abs(x - ux) > 20) {
holder.bt_deleitem.setVisibility(View.VISIBLE);
curDel_btn = holder.bt_deleitem;
return true;
}
}
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {// 当滑动时背景为选中状态
return true;
// v.setBackgroundResource(R.drawable.mm_listitem_pressed);
} else {// 其他模式
// 设置背景为未选中正常状态
// v.setBackgroundResource(R.drawable.mm_listitem_simple);
}
return false;
}
});
//viewHolder.tvTitle.setText(this.arrays.get(position));
// 为删除按钮添加监听事件,实现点击删除按钮时删除该项
viewHolder.bt_deleitem.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (curDel_btn != null)
curDel_btn.setVisibility(View.GONE);
arrays.remove(position);
notifyDataSetChanged();
}
});
return view;
}
final static class ViewHolder {
RoundImageView riv_friendsIcon;
TextView tv_friendsName;
TextView tv_friendsLastMsg;
TextView tv_friendsLastMsgTime;
Button bt_deleitem;
}
}
ListViewItem的布局文件
<?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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp" >
<com.example.movecutitem.view.RoundImageView
android:id="@+id/img_icon"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/apk" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:gravity="center_vertical"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_nikename"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="风一般的少年"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_msg"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="昨天我从火星回来" />
</LinearLayout>
</LinearLayout>
<Button
android:id="@+id/bt_del"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#23008812"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:visibility="gone"
android:text="删除" />
<TextView
android:id="@+id/tv_msgTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:text="昨天" />
</RelativeLayout>
最后实现效果:
感谢http://www.cnblogs.com/ProgramBull/p/3477917.html,提供的源码