Android开发之无bug滑动删除源码(非第三方库)
生活随笔
收集整理的這篇文章主要介紹了
Android开发之无bug滑动删除源码(非第三方库)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Android開發之無bug滑動刪除源碼(非第三方庫源碼請在最后面自行下載)
1.我們先來看下效果圖:上邊是抽取出來的demo,下邊是公司用到的項目
? ? ? ??
?
?? ?? ??
2.我們來看下如何調用(我們這里以listView為講解,因為本公司現在項目用的就是listView)
package voicenotes.listviewdelete;import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.ListView; import android.widget.Toast;import java.util.ArrayList; import java.util.List;public class MainActivity extends Activity {private static final String TAG = "zxt";private ListView mLv;private List<SwipeBean> mDatas;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mLv = (ListView) findViewById(R.id.test);initDatas();mLv.setAdapter(new CommonAdapter<SwipeBean>(this, mDatas, R.layout.item_cst_swipe) {@Overridepublic void convert(final ViewHolder holder, SwipeBean swipeBean, final int position) {//((SwipeMenuLayout)holder.getConvertView()).setIos(false);//這句話關掉IOS阻塞式交互效果holder.setText(R.id.content, swipeBean.name);holder.setOnClickListener(R.id.content, new View.OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(MainActivity.this, "position:" + position, Toast.LENGTH_SHORT).show();}});holder.setOnClickListener(R.id.btnDelete, new View.OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(MainActivity.this, "刪除了:" + mDatas.get(position).name, Toast.LENGTH_SHORT).show();//在ListView里,點擊側滑菜單上的選項時,如果想讓擦花菜單同時關閉,調用這句話((SwipeMenuLayout) holder.getConvertView()).quickClose();mDatas.remove(position);notifyDataSetChanged();}});}});}private void initDatas() {mDatas = new ArrayList<>();for (int i = 0; i < 50; i++) {mDatas.add(new SwipeBean("招商銀行 信用卡 (256" + i + ")"));}} }?
3.看下數據如何傳遞的
?
4.看下數據的格式如何設置的
public class SwipeBean {/*** 如果有其他的數據格式請自行在下面添加即可*/public String name;public SwipeBean(String name) {this.name = name;} }5.看下adapter:
package voicenotes.listviewdelete; /** Copyright (c) 2018, smuyyh@gmail.com All Rights Reserved.* # ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #* # ? ? ? ? ? ? ? ? ? ? ? _oo0oo_ ? ? ? ? ? ? ? ? ? ? #* # ? ? ? ? ? ? ? ? ? ? ?o8888888o ? ? ? ? ? ? ? ? ? ?#* # ? ? ? ? ? ? ? ? ? ? ?88" . "88 ? ? ? ? ? ? ? ? ? ?#* # ? ? ? ? ? ? ? ? ? ? ?(| -_- |) ? ? ? ? ? ? ? ? ? ?#* # ? ? ? ? ? ? ? ? ? ? ?0\ ?= ?/0 ? ? ? ? ? ? ? ? ? ?#* # ? ? ? ? ? ? ? ? ? ?___/`---'\___ ? ? ? ? ? ? ? ? ?#* # ? ? ? ? ? ? ? ? ?.' \\| ? ? |# '. ? ? ? ? ? ? ? ? #* # ? ? ? ? ? ? ? ? / \\||| ?: ?|||# \ ? ? ? ? ? ? ? ?#* # ? ? ? ? ? ? ? ?/ _||||| -:- |||||- \ ? ? ? ? ? ? ?#* # ? ? ? ? ? ? ? | ? | \\\ ?- ?#/ | ? | ? ? ? ? ? ? ?#* # ? ? ? ? ? ? ? | \_| ?''\---/'' ?|_/ | ? ? ? ? ? ? #* # ? ? ? ? ? ? ? \ ?.-\__ ?'-' ?___/-. / ? ? ? ? ? ? #* # ? ? ? ? ? ? ___'. .' ?/--.--\ ?`. .'___ ? ? ? ? ? #* # ? ? ? ? ?."" '< ?`.___\_<|>_/___.' >' "". ? ? ? ? #* # ? ? ? ? | | : ?`- \`.;`\ _ /`;.`/ - ` : | | ? ? ? #* # ? ? ? ? \ ?\ `_. ? \_ __\ /__ _/ ? .-` / ?/ ? ? ? #* # ? ? =====`-.____`.___ \_____/___.-`___.-'===== ? ?#* # ? ? ? ? ? ? ? ? ? ? ? `=---=' ? ? ? ? ? ? ? ? ? ? #* # ? ? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ? #* # ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #* # ? ? ? ? ? ? ? 佛祖保佑 ? ? ? ? 永無BUG ? ? ? ? ? ? #* # ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #*/import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter;import java.util.ArrayList; import java.util.List;/*** 創 建 者:下一頁5(輕飛揚)* 創建時間:2018/6/27.12:52* 個人小站:http://wap.yhsh.ai(已掛)* 最新小站:http://www.iyhsh.icoc.in* 聯系作者:企鵝 13343401268(請用手機QQ添加)* 博客地址:http://blog.csdn.net/xiayiye5* 空間名稱:SwipeDelMenuLayout* 項目包名:voicenotes.listviewdelete*/ public abstract class CommonAdapter<T> extends BaseAdapter {protected Context mContext;protected List<T> mDatas;protected LayoutInflater mInflater;private int layoutId;public CommonAdapter(Context context, List<T> datas, int layoutId) {this.mContext = context;this.mInflater = LayoutInflater.from(context);this.mDatas = datas;this.layoutId = layoutId;}@Overridepublic int getCount() {return this.mDatas != null?this.mDatas.size():0;}@Overridepublic T getItem(int position) {return this.mDatas.get(position);}@Overridepublic long getItemId(int position) {return (long)position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolder holder = ViewHolder.get(this.mContext, convertView, parent, this.layoutId, position);this.convert(holder, this.getItem(position), position);return holder.getConvertView();}public abstract void convert(ViewHolder var1, T var2, int var3);public void setDatas(List<T> list) {if(this.mDatas != null) {if(null != list) {ArrayList temp = new ArrayList();temp.addAll(list);this.mDatas.clear();this.mDatas.addAll(temp);} else {this.mDatas.clear();}} else {this.mDatas = list;}this.notifyDataSetChanged();}public void remove(int i) {if(null != this.mDatas && this.mDatas.size() > i && i > -1) {this.mDatas.remove(i);this.notifyDataSetChanged();}}public void addDatas(List<T> list) {if(null != list) {ArrayList temp = new ArrayList();temp.addAll(list);if(this.mDatas != null) {this.mDatas.addAll(temp);} else {this.mDatas = temp;}this.notifyDataSetChanged();}}public List<T> getDatas() {return this.mDatas;} }6.我們來看下自己寫的ViewHolder
/*** 創 建 者:下一頁5(輕飛揚)* 創建時間:2018/6/27.12:53* 個人小站:http://wap.yhsh.ai(已掛)* 最新小站:http://www.iyhsh.icoc.in* 聯系作者:企鵝 13343401268(請用手機QQ添加)* 博客地址:http://blog.csdn.net/xiayiye5* 空間名稱:SwipeDelMenuLayout* 項目包名:voicenotes.listviewdelete*/ public class ViewHolder {private SparseArray<View> mViews;private int mPosition;private View mConvertView;private Context mContext;private int mLayoutId;public ViewHolder(Context context, ViewGroup parent, int layoutId, int position) {this.mContext = context;this.mLayoutId = layoutId;this.mPosition = position;this.mViews = new SparseArray();this.mConvertView = LayoutInflater.from(context).inflate(layoutId, parent, false);this.mConvertView.setTag(this);}public static ViewHolder get(Context context, View convertView, ViewGroup parent, int layoutId, int position) {if(convertView == null) {return new ViewHolder(context, parent, layoutId, position);} else {ViewHolder holder = (ViewHolder)convertView.getTag();holder.mPosition = position;return holder;}}public int getPosition() {return this.mPosition;}public int getLayoutId() {return this.mLayoutId;}public <T extends View> T getView(int viewId) {View view = (View)this.mViews.get(viewId);if(view == null) {view = this.mConvertView.findViewById(viewId);this.mViews.put(viewId, view);}return (T) view;}public View getConvertView() {return this.mConvertView;}public ViewHolder setSelected(int viewId, boolean flag) {View v = this.getView(viewId);v.setSelected(flag);return this;}public ViewHolder setText(int viewId, String text) {TextView tv = (TextView)this.getView(viewId);tv.setText(text);return this;}public ViewHolder setImageResource(int viewId, int resId) {ImageView view = (ImageView)this.getView(viewId);view.setImageResource(resId);return this;}public ViewHolder setImageBitmap(int viewId, Bitmap bitmap) {ImageView view = (ImageView)this.getView(viewId);view.setImageBitmap(bitmap);return this;}public ViewHolder setImageDrawable(int viewId, Drawable drawable) {ImageView view = (ImageView)this.getView(viewId);view.setImageDrawable(drawable);return this;}public ViewHolder setBackgroundColor(int viewId, int color) {View view = this.getView(viewId);view.setBackgroundColor(color);return this;}public ViewHolder setBackgroundRes(int viewId, int backgroundRes) {View view = this.getView(viewId);view.setBackgroundResource(backgroundRes);return this;}public ViewHolder setTextColor(int viewId, int textColor) {TextView view = (TextView)this.getView(viewId);view.setTextColor(textColor);return this;}public ViewHolder setTextColorRes(int viewId, int textColorRes) {TextView view = (TextView)this.getView(viewId);view.setTextColor(this.mContext.getResources().getColor(textColorRes));return this;}@SuppressLint({"NewApi"})public ViewHolder setAlpha(int viewId, float value) {if(Build.VERSION.SDK_INT >= 11) {this.getView(viewId).setAlpha(value);} else {AlphaAnimation alpha = new AlphaAnimation(value, value);alpha.setDuration(0L);alpha.setFillAfter(true);this.getView(viewId).startAnimation(alpha);}return this;}public ViewHolder setVisible(int viewId, boolean visible) {View view = this.getView(viewId);view.setVisibility(visible?0:8);return this;}public ViewHolder linkify(int viewId) {TextView view = (TextView)this.getView(viewId);Linkify.addLinks(view, 15);return this;}public ViewHolder setTypeface(Typeface typeface, int... viewIds) {int[] var3 = viewIds;int var4 = viewIds.length;for(int var5 = 0; var5 < var4; ++var5) {int viewId = var3[var5];TextView view = (TextView)this.getView(viewId);view.setTypeface(typeface);view.setPaintFlags(view.getPaintFlags() | 128);}return this;}public ViewHolder setProgress(int viewId, int progress) {ProgressBar view = (ProgressBar)this.getView(viewId);view.setProgress(progress);return this;}public ViewHolder setProgress(int viewId, int progress, int max) {ProgressBar view = (ProgressBar)this.getView(viewId);view.setMax(max);view.setProgress(progress);return this;}public ViewHolder setMax(int viewId, int max) {ProgressBar view = (ProgressBar)this.getView(viewId);view.setMax(max);return this;}public ViewHolder setRating(int viewId, float rating) {RatingBar view = (RatingBar)this.getView(viewId);view.setRating(rating);return this;}public ViewHolder setRating(int viewId, float rating, int max) {RatingBar view = (RatingBar)this.getView(viewId);view.setMax(max);view.setRating(rating);return this;}public ViewHolder setTag(int viewId, Object tag) {View view = this.getView(viewId);view.setTag(tag);return this;}public ViewHolder setTag(int viewId, int key, Object tag) {View view = this.getView(viewId);view.setTag(key, tag);return this;}public ViewHolder setChecked(int viewId, boolean checked) {Checkable view = (Checkable)this.getView(viewId);view.setChecked(checked);return this;}public ViewHolder setOnClickListener(int viewId, View.OnClickListener listener) {View view = this.getView(viewId);view.setOnClickListener(listener);return this;}public ViewHolder setOnTouchListener(int viewId, View.OnTouchListener listener) {View view = this.getView(viewId);view.setOnTouchListener(listener);return this;}public ViewHolder setOnLongClickListener(int viewId, View.OnLongClickListener listener) {View view = this.getView(viewId);view.setOnLongClickListener(listener);return this;} }7.下面是對應的布局xml文件
activity_main.xml
<?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"><ListViewandroid:id="@+id/test"android:layout_width="match_parent"android:layout_height="match_parent"/> </RelativeLayout>item_cst_swipe.xml
<?xml version="1.0" encoding="utf-8"?> <voicenotes.listviewdelete.SwipeMenuLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="44dp"android:background="@android:color/white"android:clickable="true"android:minHeight="44dp"android:paddingBottom="1dp"android:paddingLeft="16dp"app:ios="false"app:leftSwipe="true"app:swipeEnable="true"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"><ImageViewandroid:id="@+id/iv_bindCard_list_item"android:layout_width="20dp"android:layout_height="20dp"android:layout_marginLeft="10dp"android:src="@drawable/yhsh_bank_zs" /><TextViewandroid:id="@+id/content"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_marginLeft="8dp"android:layout_weight="1"android:text="@string/yhsh_bank_type"android:textColor="@color/describe_text"android:textSize="16sp" /></LinearLayout><!-- 以下都是側滑菜單的內容依序排列 --><Buttonandroid:id="@+id/btnTop"android:layout_width="60dp"android:layout_height="match_parent"android:background="#d9dee4"android:text="置頂"android:textColor="@android:color/white" /><Buttonandroid:id="@+id/btnUnRead"android:layout_width="120dp"android:layout_height="match_parent"android:background="#ecd50a"android:clickable="true"android:text="標記未讀"android:textColor="@android:color/white" /><Buttonandroid:id="@+id/btnDelete"android:layout_width="60dp"android:layout_height="match_parent"android:background="@color/red_ff4a57"android:text="刪除"android:textColor="@android:color/white" /><!-- <RelativeLayoutandroid:layout_width="wrap_content"android:layout_height="match_parent"android:background="@color/red_ff4a57"android:clickable="true"><TextViewandroid:id="@+id/tv_delete"android:layout_width="100dp"android:layout_height="wrap_content"android:layout_centerVertical="true"android:drawablePadding="5dp"android:drawableTop="@drawable/point_icon_delete"android:gravity="center"android:text="刪除"android:textColor="@android:color/white"/></RelativeLayout>--></voicenotes.listviewdelete.SwipeMenuLayout>源碼下載
GitHub源碼庫
總結
以上是生活随笔為你收集整理的Android开发之无bug滑动删除源码(非第三方库)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Django基础之创建admin账号
- 下一篇: 前端微服务搭建(Single-Spa +