android 侧滑删除功能,200行代码让你在Android中完美实现iOS版侧滑删除效果
使用幾個月的IOS之后,發(fā)現(xiàn)IOS中側(cè)滑刪除俺就
大家好,自己開始學(xué)習(xí)Android已經(jīng)差不多半年了吧,前前后后看了不少的博客獲益匪淺。漸漸的隨著技術(shù)的提升,慢慢感覺網(wǎng)上其它的一些功能的實(shí)現(xiàn)又不是那么完美,今天就給大家?guī)硪黄贏ndroid中完全仿照IOS側(cè)滑刪除的效果。
首先我們來看一下實(shí)現(xiàn)的效果如何:
?? ? ? ? ??
第一張圖片是展示刪除的效果,刪除時會有上縮動畫效果
第二張圖片是展示滑出刪除按鈕時的事件搶占,有刪除按鈕存在時需要搶占掉ListView的滑動事件,而且保證至多有1個刪除按鈕顯示
所以說實(shí)現(xiàn)完美的側(cè)滑刪除效果需要了解Android中的事件分發(fā)機(jī)制,如果有不明白的同學(xué)可以去
郭神:
鴻前輩:
這兩位前輩的博客里看事件分發(fā)章節(jié)的內(nèi)容。
好了我們廢話不多說,進(jìn)入主題。
本例的側(cè)滑刪除用的是HorizontalScrollView來實(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"
/>
是不是很簡單,僅僅是只有一個自定義的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="刪除" />
如果需要復(fù)雜一點(diǎn)兒的item樣式,那就把item_text換成自己想要的布局就好了~
然后看我們的重頭戲,adapter
public class HorizontalSlideAdapter extends ArrayAdapter {
/** 屏幕寬度 */
private int mScreenWidth;
/** 刪除按鈕事件 */
private DeleteButtonOnclickImpl mDelOnclickImpl;
/** HorizontalScrollView左右滑動事件 */
private ScrollViewScrollImpl mScrollImpl;
/** 布局參數(shù),動態(tài)讓HorizontalScrollView中的TextView寬度包裹父容器 */
private LinearLayout.LayoutParams mParams;
/** 記錄滑動出刪除按鈕的itemView */
public HorizontalScrollView mScrollView;
/** touch事件鎖定,如果已經(jī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滑動事件
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的滑動事件 */
private class ScrollViewScrollImpl implements OnTouchListener {
/** 記錄開始時的坐標(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操作,解鎖會在父組件的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;
// 如果滑動了>50個像素,就顯示出刪除按鈕
if (startX > event.getX() + 50) {
startX = 0;// 因?yàn)楣靡粋€事件處理對象,防止錯亂,還原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 + "項(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)兒長,首先是獲取當(dāng)前屏幕寬度,態(tài)的給每一個ItemView讓他填充父容器(因?yàn)閷挾仁瞧聊粚挾?。
再之后就是對onTouch事件的處理,這一點(diǎn)注釋里面寫的比較詳細(xì)就不再贅述了。
看到這兒也許就會有點(diǎn)兒納悶了,唉 既然吧事件鎖上了,,怎么沒見打開啊?
嗯,這也是我當(dāng)時在編寫這個側(cè)滑時遇到的問題,我們不僅僅是只讓刪除按鈕只出現(xiàn)一個就行了,我們還需要在出現(xiàn)刪除按鈕時也讓ListView的滑動失效!
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的android 侧滑删除功能,200行代码让你在Android中完美实现iOS版侧滑删除效果的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux前10ip,检查网口流量与前1
- 下一篇: 固定 顶部_优质的阳光板温室的顶部应该如