listview侧滑删除
生活随笔
收集整理的這篇文章主要介紹了
listview侧滑删除
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
自定義Listview,向左滑動,右邊剛好顯示刪除按鈕:
public class SlideListView extends ListView {private int mScreenWidth; // 屏幕寬度
private int mDownX; // 按下點的x值
private int mDownY; // 按下點的y值
private int mDeleteBtnWidth;// 刪除按鈕的寬度
private boolean isDeleteShown; // 刪除按鈕是否正在顯示
private ViewGroup mPointChild; // 當(dāng)前處理的item
private LinearLayout.LayoutParams mLayoutParams; // 當(dāng)前處理的item的LayoutParams
public SlideListView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SlideListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// 獲取屏幕寬度
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
mScreenWidth = dm.widthPixels;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
performActionDown(ev);
break;
case MotionEvent.ACTION_MOVE:
return performActionMove(ev);
case MotionEvent.ACTION_UP:
performActionUp();
break;
}
return super.onTouchEvent(ev);
}
// 處理action_down事件
private void performActionDown(MotionEvent ev) {
if (isDeleteShown) {
turnToNormal();
}
mDownX = (int) ev.getX();
mDownY = (int) ev.getY();
// 獲取當(dāng)前點的item
mPointChild = (ViewGroup) getChildAt(pointToPosition(mDownX, mDownY)
- getFirstVisiblePosition());
// 獲取刪除按鈕的寬度
mDeleteBtnWidth = mPointChild.getChildAt(1).getLayoutParams().width;
mLayoutParams = (LinearLayout.LayoutParams) mPointChild.getChildAt(0)
.getLayoutParams();
mLayoutParams.width = mScreenWidth;
mPointChild.getChildAt(0).setLayoutParams(mLayoutParams);
}
// 處理action_move事件
private boolean performActionMove(MotionEvent ev) {
int nowX = (int) ev.getX();
int nowY = (int) ev.getY();
if (Math.abs(nowX - mDownX) > Math.abs(nowY - mDownY)) {
// 如果向左滑動
if (nowX < mDownX) {
// 計算要偏移的距離
int scroll = (nowX - mDownX) / 2;
// 如果大于了刪除按鈕的寬度, 則最大為刪除按鈕的寬度
if (-scroll >= mDeleteBtnWidth) {
scroll = -mDeleteBtnWidth;
}
// 重新設(shè)置leftMargin
mLayoutParams.leftMargin = scroll;
// mLayoutParams.leftMargin = scroll*2;
mPointChild.getChildAt(0).setLayoutParams(mLayoutParams);
}
// return true;
}
return super.onTouchEvent(ev);
}
// 處理action_up事件
private void performActionUp() {
// 偏移量大于button的一半,則顯示button
// 否則恢復(fù)默認(rèn)
if (-mLayoutParams.leftMargin >= mDeleteBtnWidth / 2) {
mLayoutParams.leftMargin = -mDeleteBtnWidth;
// mLayoutParams.leftMargin = -mDeleteBtnWidth * 2;
isDeleteShown = true;
} else {
turnToNormal();
}
mPointChild.getChildAt(0).setLayoutParams(mLayoutParams);
}
/**
* 變?yōu)檎顟B(tài)
*/
public void turnToNormal() {
mLayoutParams.leftMargin = 0;
mPointChild.getChildAt(0).setLayoutParams(mLayoutParams);
isDeleteShown = false;
}
/**
* 當(dāng)前是否可點擊
*
* @return 是否可點擊
*/
public boolean canClick() {
return !isDeleteShown;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {//與scrollview嵌套需要重新計算高度
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
適配器: public class ListViewSlideAdapter extends BaseAdapter {
private ArrayList<String> bulbList;
private Activity context;
private OnClickListenerEditOrDelete onClickListenerEditOrDelete;
public ListViewSlideAdapter(Activity context, ArrayList<String> bulbList) { this.bulbList = bulbList;
this.context = context;
}
public void setmNews(ArrayList<String> mNews) {
this.bulbList = mNews;
}
@Override
public int getCount() {
return bulbList.size();
}
@Override
public Object getItem(int position) {
return bulbList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final String str = bulbList.get(position);
ViewHolder viewHolder;
if (null == convertView) {
convertView = context.getLayoutInflater().inflate(R.layout.lay_list_collection_item, null);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);//store up viewHolder
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.tvDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onClickListenerEditOrDelete != null) {
onClickListenerEditOrDelete.OnClickListenerDelete(position);
}
}
});
if (!TextUtils.isEmpty(str.)) {
viewHolder.mTv.setText("" + str);
}
return convertView;
}
public void setOnClickListenerEditOrDelete(OnClickListenerEditOrDelete onClickListenerEditOrDelete1) {
this.onClickListenerEditOrDelete = onClickListenerEditOrDelete1;
}
public interface OnClickListenerEditOrDelete {
void OnClickListenerDelete(int position);
}
private class ViewHolder {
TextView mTv;
TextView tvDelete;
ViewHolder(View view) {
mTv = (TextView) view.findViewById(R.id.tv_title);
tvDelete = (TextView) view.findViewById(R.id.tv_delete);
}
}
}
Item布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="match_parent"
android:background="@color/white"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_weight="1"
android:maxLength="@integer/maxlength"
android:textColor="@color/black"
android:textSize="@dimen/text_16" />
<TextView
android:id="@+id/tv_delete"
android:layout_width="60dp"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/panding_5"
android:background="@color/color_26"
android:gravity="center"
android:text="刪除"
android:textColor="@color/text_f7"
android:textSize="@dimen/text_16" />
</LinearLayout>
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="match_parent"
android:fitsSystemWindows="true"
android:background="@color/color_f4"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="@dimen/margin_1"
android:background="@color/color_line_ae" />
<...PullToRefreshScrollView
android:id="@+id/pull_refresh_scrollview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
app:ptrAnimationStyle="flip"
app:ptrMode="both">
<...SlideListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/panding_5"
android:layout_marginRight="@dimen/panding_5"
android:divider="@null"
android:dividerHeight="0dp" />
</...PullToRefreshScrollView>
<TextView
android:id="@+id/tv_notic"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_horizontal"
android:paddingTop="20dp"
android:text="暫無收藏"
android:visibility="gone" />
?
例子:
mListCollectionAdapter = new ListViewSlideAdapter(this, mMessageReceive);
mListView.setAdapter(mListCollectionAdapter);
mListView.setOnItemClickListener(this);
mListCollectionAdapter.setOnClickListenerEditOrDelete(new ListViewSlideAdapter.OnClickListenerEditOrDelete() {
@Override
public void OnClickListenerDelete(int position) {
//刪除按鈕點擊事件
}
});
轉(zhuǎn)載于:https://www.cnblogs.com/shenchanghui/p/5788725.html
總結(jié)
以上是生活随笔為你收集整理的listview侧滑删除的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JS语法(二)
- 下一篇: python打怪之路【第二篇】:Impo