标签:listview checkbox button 滑动删除 选择删除
在项目开发过程中,常常需要对用户列表的信息进行删除的操作。Android中常用的删除操作方式有两种 ,一种就是类似微信的滑动出现删除按钮方式,还有一种是通过CheckBox进行选择,然后通过按钮进行删除的方式。本来的实例集成上述的两种操作方式来实现用户列表删除的效果。
设计思路:在适配器类MyAdapter一个滑动删除按钮显示或隐藏的Map,一个用于CheckBox是否选中的Map和一个与MainAcitivyt进行数据交互的接口ContentsDeleteListener,同时该接口包含两个方法,contentsDeleteSelect(int position, boolean isChecked)方法用于将选中或取消内容从选中List中加入或删除,contentDelete(int position)用于删除List中指定位置的列项。滑动的效果主要是通过当滑动的距离大于40时,显示滑动删除按钮,再执行删除操作时,将其他所有设置不可见,并设置CheckBox为不选中状态。
一.代码实现效果
二.代码实现
1. 主界面布局 activity_main.xml
<span style="font-size:18px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/my_delete_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="@string/delete" /> <ListView android:id="@+id/my_lv" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentTop="true" android:layout_above="@id/my_delete_btn" android:contentDescription="@string/member_list"/> </RelativeLayout> </span>主界面布局很简单,就一个删除Button和一个存放用户信息的ListView
2.用户项布局 activity_main_list_view.xml
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/my_rl" android:layout_width="match_parent" android:layout_height="200dp" > <CheckBox android:id="@+id/my_select_cb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:background="@drawable/my_list_checkbox_selector" android:button="@null" /> <TextView android:id="@+id/my_content_tv" android:layout_width="wrap_content" android:layout_height="60dp" android:layout_centerInParent="true" android:layout_marginLeft="20dp" android:layout_toRightOf="@id/my_select_cb" android:gravity="center" android:text="@string/delete" /> <TextView android:id="@+id/my_delete_tv" android:layout_width="80dp" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerInParent="true" android:layout_marginLeft="5dp" android:clickable="true" android:drawableLeft="@drawable/del_icon_normal" android:gravity="center" android:paddingLeft="10dp" android:paddingRight="10dp" android:text="@string/delete" android:visibility="gone" /> </RelativeLayout></span>该布局包含一个选择的CheckBox,显示内容的TextView 和一个能滑动实现删除的TextView
3.滑动效果anim_right_left.xml
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="200" android:fromXDelta="320" android:fromYDelta="0" android:toXDelta="0" android:toYDelta="0" /> </set></span>
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@drawable/my_list_check_box_bg_check" android:state_checked="true"></item> <item android:drawable="@drawable/my_list_check_box_bg_check" android:state_selected="true"></item> <item android:drawable="@drawable/my_list_check_box_bg_check" android:state_pressed="true"></item> <item android:drawable="@drawable/my_list__check_box_bg"></item> </selector> </span>
<span style="font-size:18px;">package com.example.slideandselectdeletedemo; import java.util.HashMap; import java.util.List; import java.util.Map; import android.annotation.SuppressLint; import android.content.Context; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.RelativeLayout; import android.widget.TextView; import com.example.slidedeleteandselectdemo.R; @SuppressLint("UseSparseArrays") public class MyAdapter extends BaseAdapter { private LayoutInflater mInflater; private List<String> mContentsList; private Context mContext; private ContentsDeleteListener mContentsDeleteListener; //设置滑动删除按钮是否显示 private Map<Integer, Integer> visibleDeleteTv; //CheckBox选择和未选择 private Map<Integer, Boolean> selectCb; //滑动后的X坐标点 private int mLastX = 0; // private int mLastY = 0; public MyAdapter(Context mContext, List<String> mContentsList, ContentsDeleteListener mContentsDeleteListener) { this.mContext = mContext; this.mContentsList = mContentsList; this.mContentsDeleteListener = mContentsDeleteListener; this.mInflater = (LayoutInflater) mContext .getSystemService(Context.LAYOUT_INFLATER_SERVICE); visibleDeleteTv = new HashMap<Integer, Integer>(); selectCb = new HashMap<Integer, Boolean>(); // 更新界面时,记录为 未选中和滑动删除按钮不可见 for (int i = 0; i < mContentsList.size(); i++) { visibleDeleteTv.put(i, View.GONE); selectCb.put(i, false); } } public void updateView(List<String> mContentsList) { this.mContentsList = mContentsList; this.notifyDataSetChanged(); } @Override public int getCount() { return mContentsList.size(); } @Override public Object getItem(int position) { return mContentsList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { final HolderView holderView; if (convertView == null) { holderView = new HolderView(); convertView = mInflater.inflate(R.layout.activity_main_list_view, null); holderView.listSelectCb = (CheckBox) convertView .findViewById(R.id.my_select_cb); holderView.listContentTv = (TextView) convertView .findViewById(R.id.my_content_tv); holderView.listDeleteTv = (TextView) convertView .findViewById(R.id.my_delete_tv); holderView.listRl = (RelativeLayout) convertView .findViewById(R.id.my_rl); convertView.setTag(holderView); } else { holderView = (HolderView) convertView.getTag(); if (holderView.listSelectCb.isChecked()) { holderView.listSelectCb.setChecked(false); } } // 显示内容 holderView.listContentTv.setText(mContentsList.get(position)); if (visibleDeleteTv != null) { holderView.listDeleteTv .setVisibility(visibleDeleteTv.get(position)); } if (selectCb != null) { holderView.listSelectCb.setChecked(selectCb.get(position)); mContentsDeleteListener.contentsDeleteSelect(position, selectCb.get(position)); } // 处理选择事件 holderView.listRl.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (visibleDeleteTv.containsValue(View.VISIBLE)) {//可见时,再次单击设置不可见,未选中 for (int i = 0; i < getCount(); i++) { visibleDeleteTv.put(i, View.GONE); selectCb.put(i, false); mContentsDeleteListener.contentsDeleteSelect(i, false); } notifyDataSetChanged(); } else { boolean isChecked = holderView.listSelectCb.isChecked() ? false : true; holderView.listSelectCb.setChecked(isChecked); mContentsDeleteListener.contentsDeleteSelect(position, isChecked); } } }); holderView.listSelectCb .setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { mContentsDeleteListener.contentsDeleteSelect(position, isChecked); } }); holderView.listSelectCb.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (visibleDeleteTv.containsValue(View.VISIBLE)) { for (int i = 0; i < getCount(); i++) { visibleDeleteTv.put(i, View.GONE); selectCb.put(i, false); mContentsDeleteListener.contentsDeleteSelect(i, false); } notifyDataSetChanged(); } } }); convertView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { final Animation alpha = AnimationUtils.loadAnimation(mContext, R.anim.anim_right_left); int x = (int) event.getX(); // int y = (int) event.getY(); // Log.d(TAG, "x=" + x + " y=" + y); // press down if (event.getAction() == MotionEvent.ACTION_DOWN) { alpha.cancel(); } else if (event.getAction() == MotionEvent.ACTION_MOVE) { alpha.cancel(); int deltaX = mLastX - x; // int deltaY = mLastY - y; // Log.d(TAG, "deltaX=" + deltaX + ",deltaY=" + deltaY); if (deltaX > 40) {//当滑动距离大于40时,显示该位置的删除按键 for (int i = 0; i < getCount(); i++) { visibleDeleteTv.put(i, View.GONE); selectCb.put(i, false); mContentsDeleteListener.contentsDeleteSelect(i, false); if (i == position) { visibleDeleteTv.put(position, View.VISIBLE); selectCb.put(i, true); mContentsDeleteListener.contentsDeleteSelect(i, true); if (visibleDeleteTv.get(position) == View.VISIBLE) { holderView.listDeleteTv .startAnimation(alpha); } } } notifyDataSetChanged(); } return true; } else {// other alpha.cancel(); } mLastX = x; // mLastY = y; return false; } }); holderView.listDeleteTv.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // Log.d(TAG, "onClick:position=" + position); mContentsList.remove(position); mContentsDeleteListener.contentDelete(position); for (int i = 0; i < mContentsList.size(); i++) { visibleDeleteTv.put(i, View.GONE); selectCb.put(i, false); mContentsDeleteListener.contentsDeleteSelect(i, false); } notifyDataSetChanged(); } }); return convertView; } public class HolderView { public TextView listContentTv, listDeleteTv; public CheckBox listSelectCb; public RelativeLayout listRl; } public void setContentsDeleteListener( ContentsDeleteListener mContentsDeleteListener) { this.mContentsDeleteListener = mContentsDeleteListener; } /** * 用于删除内容的接口 * * @author liangming.deng * */ public interface ContentsDeleteListener { /** * 根据isChecked 选择和取消选择的指定位置 * @param position * @param isChecked */ public void contentsDeleteSelect(int position, boolean isChecked); /** * 删除指定位置内容 * @param position */ public void contentDelete(int position); } public void setVisibleDeleteTv(Map<Integer, Integer> visibleDeleteTv) { this.visibleDeleteTv = visibleDeleteTv; } public void setSelectCb(Map<Integer, Boolean> selectCb) { this.selectCb = selectCb; } } </span>
<span style="font-size:18px;">package com.example.slideandselectdeletedemo; import java.util.ArrayList; import java.util.Collections; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ListView; import com.example.slideandselectdeletedemo.MyAdapter.ContentsDeleteListener; import com.example.slidedeleteandselectdemo.R; public class MainActivity extends Activity implements ContentsDeleteListener,OnClickListener{ private ListView myLv; private Button myDeleteBtn; private MyAdapter myAdapter; private String[] myContentsArray; private List<String> myContentsList = new ArrayList<String>(); private List<String> mySelectedList = new ArrayList<String>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findById(); myContentsArray = this.getResources().getStringArray(R.array.my_contents); if(myContentsArray != null){ Collections.addAll(myContentsList, myContentsArray); } myAdapter = new MyAdapter(this,myContentsList,this); myLv.setAdapter(myAdapter); } private void findById(){ myLv = (ListView) this.findViewById(R.id.my_lv); myDeleteBtn = (Button) this.findViewById(R.id.my_delete_btn); myDeleteBtn.setOnClickListener(this); } @Override public void onResume(){ super.onResume(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } /* * 根据isChecked,给选择的List中添加或删除数据 * (non-Javadoc) * @see com.example.slideandselectdeletedemo.MyAdapter.ContentsDeleteListener#contentsDeleteSelect(int, boolean) */ @Override public void contentsDeleteSelect(int position,boolean isChecked) { if(isChecked){ mySelectedList.add(myContentsList.get(position)); }else{ mySelectedList.remove(myContentsList.get(position)); } } /* * 删除指定位置的数据 * (non-Javadoc) * @see com.example.slideandselectdeletedemo.MyAdapter.ContentsDeleteListener#contentDelete(int) */ @Override public void contentDelete(int position) { // TODO Auto-generated method stub myContentsList.remove(position); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()){ case R.id.my_delete_btn: myContentsList.removeAll(mySelectedList); myAdapter.updateView(myContentsList); break; } } } </span>
上述主要部分给出了注释。
源码地址:http://download.csdn.net/detail/a123demi/7751141
Android 实现用户列表信息滑动删除功能和选择删除功能,布布扣,bubuko.com
标签:listview checkbox button 滑动删除 选择删除
原文地址:http://blog.csdn.net/a123demi/article/details/38534339