日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android 侧滑删除功能,200行代码让你在Android中完美实现iOS版侧滑删除效果

發布時間:2025/3/12 Android 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 侧滑删除功能,200行代码让你在Android中完美实现iOS版侧滑删除效果 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用幾個月的IOS之后,發現IOS中側滑刪除俺就

大家好,自己開始學習Android已經差不多半年了吧,前前后后看了不少的博客獲益匪淺。漸漸的隨著技術的提升,慢慢感覺網上其它的一些功能的實現又不是那么完美,今天就給大家帶來一篇在Android中完全仿照IOS側滑刪除的效果。

首先我們來看一下實現的效果如何:

?? ? ? ? ??

第一張圖片是展示刪除的效果,刪除時會有上縮動畫效果

第二張圖片是展示滑出刪除按鈕時的事件搶占,有刪除按鈕存在時需要搶占掉ListView的滑動事件,而且保證至多有1個刪除按鈕顯示

所以說實現完美的側滑刪除效果需要了解Android中的事件分發機制,如果有不明白的同學可以去

郭神:

鴻前輩:

這兩位前輩的博客里看事件分發章節的內容。

好了我們廢話不多說,進入主題。

本例的側滑刪除用的是HorizontalScrollView來實現的。

首先我們先創建我們的activity_main.xml,主布局文件

xmlns:tools=""

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context="com.example.horizontalslidelistview.MainActivity" >

android:id="@+id/listview"

android:layout_width="match_parent"

android:layout_height="wrap_content"

/>

是不是很簡單,僅僅是只有一個自定義的ListView,不過我們先放過這個自定義的ListVIew,再來看看ListView中item的布局文件

item_horizontal_slide_listview.xml

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:scrollbars="none" >

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="horizontal" >

android:id="@+id/item_text"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:gravity="center_vertical"

android:background="#EEEEEE"

android:paddingLeft="20dp"

android:textColor="#FF0000"

android:textSize="20sp" />

android:id="@+id/item_delete"

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:background="#FF0000"

android:text="刪除" />

如果需要復雜一點兒的item樣式,那就把item_text換成自己想要的布局就好了~

然后看我們的重頭戲,adapter

public class HorizontalSlideAdapter extends ArrayAdapter {

/** 屏幕寬度 */

private int mScreenWidth;

/** 刪除按鈕事件 */

private DeleteButtonOnclickImpl mDelOnclickImpl;

/** HorizontalScrollView左右滑動事件 */

private ScrollViewScrollImpl mScrollImpl;

/** 布局參數,動態讓HorizontalScrollView中的TextView寬度包裹父容器 */

private LinearLayout.LayoutParams mParams;

/** 記錄滑動出刪除按鈕的itemView */

public HorizontalScrollView mScrollView;

/** touch事件鎖定,如果已經有滑動出刪除按鈕的itemView,就屏蔽下一整次(down,move,up)的onTouch操作 */

public boolean mLockOnTouch = false;

public HorizontalSlideAdapter(Context context, List objects) {

super(context, 0, objects);

// 搞到屏幕寬度

Display defaultDisplay = ((Activity) context).getWindowManager()

.getDefaultDisplay();

DisplayMetrics metrics = new DisplayMetrics();

defaultDisplay.getMetrics(metrics);

mScreenWidth = metrics.widthPixels;

mParams = new LinearLayout.LayoutParams(mScreenWidth,

LinearLayout.LayoutParams.MATCH_PARENT);

// 初始化刪除按鈕事件與item滑動事件

mDelOnclickImpl = new DeleteButtonOnclickImpl();

mScrollImpl = new ScrollViewScrollImpl();

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder = null;

if (convertView == null) {

holder = new ViewHolder();

convertView = View.inflate(getContext(),

R.layout.item_horizontal_slide_listview, null);

holder.scrollView = (HorizontalScrollView) convertView;

holder.scrollView.setOnTouchListener(mScrollImpl);

holder.infoTextView = (TextView) convertView

.findViewById(R.id.item_text);

// 設置item內容為fill_parent的

holder.infoTextView.setLayoutParams(mParams);

holder.deleteButton = (Button) convertView

.findViewById(R.id.item_delete);

holder.deleteButton.setOnClickListener(mDelOnclickImpl);

convertView.setTag(holder);

} else {

holder = (ViewHolder) convertView.getTag();

}

holder.position = position;

holder.deleteButton.setTag(holder);

holder.infoTextView.setText(getItem(position));

holder.scrollView.scrollTo(0, 0);

return convertView;

}

static class ViewHolder {

private HorizontalScrollView scrollView;

private TextView infoTextView;

private Button deleteButton;

private int position;

}

/** HorizontalScrollView的滑動事件 */

private class ScrollViewScrollImpl implements OnTouchListener {

/** 記錄開始時的坐標 */

private float startX = 0;

@SuppressLint("ClickableViewAccessibility")

@Override

public boolean onTouch(View v, MotionEvent event) {

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:

// 如果有劃出刪除按鈕的itemView,就讓他滑回去并且鎖定本次touch操作,解鎖會在父組件的dispatchTouchEvent中進行

if (mScrollView != null) {

scrollView(mScrollView, HorizontalScrollView.FOCUS_LEFT);

mScrollView = null;

mLockOnTouch = true;

return true;

}

startX = event.getX();

break;

case MotionEvent.ACTION_UP:

HorizontalScrollView view = (HorizontalScrollView) v;

// 如果滑動了>50個像素,就顯示出刪除按鈕

if (startX > event.getX() + 50) {

startX = 0;// 因為公用一個事件處理對象,防止錯亂,還原startX值

scrollView(view, HorizontalScrollView.FOCUS_RIGHT);

mScrollView = view;

} else {

scrollView(view, HorizontalScrollView.FOCUS_LEFT);

}

break;

}

return false;

}

}

/** HorizontalScrollView左右滑動 */

public void scrollView(final HorizontalScrollView view, final int parameter) {

view.post(new Runnable() {

@Override

public void run() {

view.pageScroll(parameter);

}

});

}

/** 刪除事件 */

private class DeleteButtonOnclickImpl implements OnClickListener {

@Override

public void onClick(View v) {

final ViewHolder holder = (ViewHolder) v.getTag();

Toast.makeText(getContext(), "刪除第" + holder.position + "項",

Toast.LENGTH_SHORT).show();

Animation animation = AnimationUtils.loadAnimation(getContext(),

R.anim.anim_item_delete);

holder.scrollView.startAnimation(animation);

animation.setAnimationListener(new AnimationListener() {

@Override

public void onAnimationStart(Animation animation) {

}

@Override

public void onAnimationRepeat(Animation animation) {

}

@Override

public void onAnimationEnd(Animation animation) {

remove(getItem(holder.position));

}

});

}

}

}有點兒長,首先是獲取當前屏幕寬度,態的給每一個ItemView讓他填充父容器(因為寬度是屏幕寬度)。

再之后就是對onTouch事件的處理,這一點注釋里面寫的比較詳細就不再贅述了。

看到這兒也許就會有點兒納悶了,唉 既然吧事件鎖上了,,怎么沒見打開啊?

嗯,這也是我當時在編寫這個側滑時遇到的問題,我們不僅僅是只讓刪除按鈕只出現一個就行了,我們還需要在出現刪除按鈕時也讓ListView的滑動失效!

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的android 侧滑删除功能,200行代码让你在Android中完美实现iOS版侧滑删除效果的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。