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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > Android >内容正文

Android

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

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

使用幾個(gè)月的IOS之后,發(fā)現(xiàn)IOS中側(cè)滑刪除俺就

大家好,自己開始學(xué)習(xí)Android已經(jīng)差不多半年了吧,前前后后看了不少的博客獲益匪淺。漸漸的隨著技術(shù)的提升,慢慢感覺網(wǎng)上其它的一些功能的實(shí)現(xiàn)又不是那么完美,今天就給大家?guī)?lái)一篇在Android中完全仿照IOS側(cè)滑刪除的效果。

首先我們來(lái)看一下實(shí)現(xiàn)的效果如何:

?? ? ? ? ??

第一張圖片是展示刪除的效果,刪除時(shí)會(huì)有上縮動(dòng)畫效果

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

所以說(shuō)實(shí)現(xiàn)完美的側(cè)滑刪除效果需要了解Android中的事件分發(fā)機(jī)制,如果有不明白的同學(xué)可以去

郭神:

鴻前輩:

這兩位前輩的博客里看事件分發(fā)章節(jié)的內(nèi)容。

好了我們廢話不多說(shuō),進(jìn)入主題。

本例的側(cè)滑刪除用的是HorizontalScrollView來(lái)實(shí)現(xiàn)的。

首先我們先創(chuàng)建我們的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"

/>

是不是很簡(jiǎn)單,僅僅是只有一個(gè)自定義的ListView,不過我們先放過這個(gè)自定義的ListVIew,再來(lái)看看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="刪除" />

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

然后看我們的重頭戲,adapter

public class HorizontalSlideAdapter extends ArrayAdapter {

/** 屏幕寬度 */

private int mScreenWidth;

/** 刪除按鈕事件 */

private DeleteButtonOnclickImpl mDelOnclickImpl;

/** HorizontalScrollView左右滑動(dòng)事件 */

private ScrollViewScrollImpl mScrollImpl;

/** 布局參數(shù),動(dòng)態(tài)讓HorizontalScrollView中的TextView寬度包裹父容器 */

private LinearLayout.LayoutParams mParams;

/** 記錄滑動(dòng)出刪除按鈕的itemView */

public HorizontalScrollView mScrollView;

/** touch事件鎖定,如果已經(jīng)有滑動(dòng)出刪除按鈕的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滑動(dòng)事件

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);

// 設(shè)置item內(nèi)容為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的滑動(dòng)事件 */

private class ScrollViewScrollImpl implements OnTouchListener {

/** 記錄開始時(shí)的坐標(biāo) */

private float startX = 0;

@SuppressLint("ClickableViewAccessibility")

@Override

public boolean onTouch(View v, MotionEvent event) {

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:

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

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;

// 如果滑動(dòng)了>50個(gè)像素,就顯示出刪除按鈕

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

startX = 0;// 因?yàn)楣靡粋€(gè)事件處理對(duì)象,防止錯(cuò)亂,還原startX值

scrollView(view, HorizontalScrollView.FOCUS_RIGHT);

mScrollView = view;

} else {

scrollView(view, HorizontalScrollView.FOCUS_LEFT);

}

break;

}

return false;

}

}

/** HorizontalScrollView左右滑動(dòng) */

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 + "項(xiàng)",

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));

}

});

}

}

}有點(diǎn)兒長(zhǎng),首先是獲取當(dāng)前屏幕寬度,態(tài)的給每一個(gè)ItemView讓他填充父容器(因?yàn)閷挾仁瞧聊粚挾?。

再之后就是對(duì)onTouch事件的處理,這一點(diǎn)注釋里面寫的比較詳細(xì)就不再贅述了。

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

嗯,這也是我當(dāng)時(shí)在編寫這個(gè)側(cè)滑時(shí)遇到的問題,我們不僅僅是只讓刪除按鈕只出現(xiàn)一個(gè)就行了,我們還需要在出現(xiàn)刪除按鈕時(shí)也讓ListView的滑動(dòng)失效!

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。