Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

仿 QQ 的 ListView 滑動刪除效果實作要點解析

編輯:關於 Android 程式設計

原本我打算在 ListView 每個 Item 的版面配置上放一個隱藏的 Button,滑動時再顯示。但實作後發現,只要每個 Item 都有一個 Button,它們彼此之間就不好控制。於是決定改成繼承 ListView,再結合 PopupWindow 來實現。

首先是版面配置檔:

delete_btn.xml:這裡只需要一個 Button

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <Button android:id="@+id/id_item_btn" android:layout_width="60dp" android:singleLine="true" android:layout_height="wrap_content" android:text="刪除" android:background="@drawable/d_delete_btn" android:textColor="#ffffff" android:paddingLeft="15dp" android:paddingRight="15dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="15dp" /> </LinearLayout> 

主版面配置檔 activity_main.xml,ListView 每個 Item 的樣式直接沿用系統的 android.R.layout.simple_list_item_1

 <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <com.example.listviewitemslidedeletebtnshow.QQListView android:id="@+id/id_listview" android:layout_width="fill_parent" android:layout_height="wrap_content" > </com.example.listviewitemslidedeletebtnshow.QQListView> </RelativeLayout> 

接下來看看 QQListView 的實作:

 package com.example.listviewitemslidedeletebtnshow; import android.content.Context; import android.util.AttributeSet; import android.view.Gravity; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewConfiguration; import android.widget.Button; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.PopupWindow; public class QQListView extends ListView { private static final String TAG = "QQlistView"; // private static final int VELOCITY_SANP = 200; // private VelocityTracker mVelocityTracker; /** * 用戶滑動的最小距離 */ private int touchSlop; /** * 是否響應滑動 */ private boolean isSliding; /** * 手指按下時的x坐標 */ private int xDown; /** * 手指按下時的y坐標 */ private int yDown; /** * 手指移動時的x坐標 */ private int xMove; /** * 手指移動時的y坐標 */ private int yMove; private LayoutInflater mInflater; private PopupWindow mPopupWindow; private int mPopupWindowHeight; private int mPopupWindowWidth; private Button mDelBtn; /** * 為刪除按鈕提供一個回調接口 */ private DelButtonClickListener mListener; /** * 當前手指觸摸的View */ private View mCurrentView; /** * 當前手指觸摸的位置 */ private int mCurrentViewPos; /** * 必要的一些初始化 * * @param context * @param attrs */ public QQListView(Context context, AttributeSet attrs) { super(context, attrs); mInflater = LayoutInflater.from(context); touchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); View view = mInflater.inflate(R.layout.delete_btn, null); mDelBtn = (Button) view.findViewById(R.id.id_item_btn); mPopupWindow = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); /** * 先調用下measure,否則拿不到寬和高 */ mPopupWindow.getContentView().measure(0, 0); mPopupWindowHeight = mPopupWindow.getContentView().getMeasuredHeight(); mPopupWindowWidth = mPopupWindow.getContentView().getMeasuredWidth(); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { int action = ev.getAction(); int x = (int) ev.getX(); int y = (int) ev.getY(); switch (action) { case MotionEvent.ACTION_DOWN: xDown = x; yDown = y; /** * 如果當前popupWindow顯示,則直接隱藏,然後屏蔽ListView的touch事件的下傳 */ if (mPopupWindow.isShowing()) { dismissPopWindow(); return false; } // 獲得當前手指按下時的item的位置 mCurrentViewPos = pointToPosition(xDown, yDown); // 獲得當前手指按下時的item View view = getChildAt(mCurrentViewPos - getFirstVisiblePosition()); mCurrentView = view; break; case MotionEvent.ACTION_MOVE: xMove = x; yMove = y; int dx = xMove - xDown; int dy = yMove - yDown; /** * 判斷是否是從右到左的滑動 */ if (xMove < xDown && Math.abs(dx) > touchSlop && Math.abs(dy) < touchSlop) { // Log.e(TAG, "touchslop = " + touchSlop + " , dx = " + dx + // " , dy = " + dy); isSliding = true; } break; } return super.dispatchTouchEvent(ev); } @Override public boolean onTouchEvent(MotionEvent ev) { int action = ev.getAction(); /** * 如果是從右到左的滑動才相應 */ if (isSliding) { switch (action) { case MotionEvent.ACTION_MOVE: int[] location = new int[2]; // 獲得當前item的位置x與y mCurrentView.getLocationOnScreen(location); // 設置popupWindow的動畫 mPopupWindow.setAnimationStyle(R.style.popwindow_delete_btn_anim_style); mPopupWindow.update(); mPopupWindow.showAtLocation(mCurrentView, Gravity.LEFT | Gravity.TOP, location[0] + mCurrentView.getWidth(), location[1] + mCurrentView.getHeight() / 2 - mPopupWindowHeight / 2); // 設置刪除按鈕的回調 mDelBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (mListener != null) { mListener.clickHappend(mCurrentViewPos); mPopupWindow.dismiss(); } } }); // Log.e(TAG, "mPopupWindow.getHeight()=" + mPopupWindowHeight); break; case MotionEvent.ACTION_UP: isSliding = false; } // 相應滑動期間屏幕itemClick事件,避免發生沖突 return true; } return super.onTouchEvent(ev); } /** * 隱藏popupWindow */ private void dismissPopWindow() { if (mPopupWindow != null && mPopupWindow.isShowing()) { mPopupWindow.dismiss(); } } public void setDelButtonClickListener(DelButtonClickListener listener) { mListener = listener; } interface DelButtonClickListener { public void clickHappend(int position); } } 

程式碼註解寫得很詳細,簡單說一下:在 dispatchTouchEvent 中設定當前是否響應用戶滑動,然後在 onTouchEvent 中判斷是否響應,如果響應,就讓 popupWindow 以動畫形式顯示出來。當然,當螢幕上存在 PopupWindow 時,ListView 的捲動、Item 的點擊,以及從右到左滑動時的 Item click 事件都要相應屏蔽,以免發生衝突。

接下來是 MainActivity.java,程式碼很簡單就不多說了。

 package com.example.listviewitemslidedeletebtnshow; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.Toast; import com.example.listviewitemslidedeletebtnshow.QQListView.DelButtonClickListener; public class MainActivity extends Activity { private QQListView mListView; private ArrayAdapter<String> mAdapter; private List<String> mDatas; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mListView = (QQListView) findViewById(R.id.id_listview); // 不要直接Arrays.asList mDatas = new ArrayList<String>(Arrays.asList("HelloWorld", "Welcome", "Java", "Android", "Servlet", "Struts", "Hibernate", "Spring", "HTML5", "Javascript", "Lucene")); mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mDatas); mListView.setAdapter(mAdapter); mListView.setDelButtonClickListener(new DelButtonClickListener() { @Override public void clickHappend(final int position) { Toast.makeText(MainActivity.this, position + " : " + mAdapter.getItem(position), 1).show(); mAdapter.remove(mAdapter.getItem(position)); } }); mListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainActivity.this, position + " : " + mAdapter.getItem(position), 1).show(); } }); } } 

效果圖如下:這裡用 asm.jar 搭配 gifcamera 截的 gif,由於 button 的動畫很短,截圖看起來有點卡、不夠流暢,大家如果有更好的截圖工具也歡迎推薦。有興趣的還是下載源碼看看實際效果。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved