Android开发之无bug滑动删除源码(非第三方库)
程序员文章站
2022-05-10 20:26:02
...
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;
@Override
protected 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) {
@Override
public 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() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "position:" + position, Toast.LENGTH_SHORT).show();
}
});
holder.setOnClickListener(R.id.btnDelete, new View.OnClickListener() {
@Override
public 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, aaa@qq.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;
}
@Override
public int getCount() {
return this.mDatas != null?this.mDatas.size():0;
}
@Override
public T getItem(int position) {
return this.mDatas.get(position);
}
@Override
public long getItemId(int position) {
return (long)position;
}
@Override
public 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">
<ListView
android: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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageView
android:id="@+id/iv_bindCard_list_item"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="10dp"
android:src="@drawable/reapal_bank_zs" />
<TextView
android:id="@+id/content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_weight="1"
android:text="@string/reapal_bank_type"
android:textColor="@color/describe_text"
android:textSize="16sp" />
</LinearLayout>
<!-- 以下都是侧滑菜单的内容依序排列 -->
<Button
android:id="@+id/btnTop"
android:layout_width="60dp"
android:layout_height="match_parent"
android:background="#d9dee4"
android:text="置顶"
android:textColor="@android:color/white" />
<Button
android: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" />
<Button
android:id="@+id/btnDelete"
android:layout_width="60dp"
android:layout_height="match_parent"
android:background="@color/red_ff4a57"
android:text="删除"
android:textColor="@android:color/white" />
<!-- <RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@color/red_ff4a57"
android:clickable="true">
<TextView
android: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>
上一篇: linux常用操作命令行