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

Android 自定義 DragGridView:長按拖拽與數據交換

編輯:關於 Android 程式設計

在一些需要自定義排序的頁面中,用戶可能希望通過長按拖拽來調整 item 順序。實現這種效果,可以基於 GridView 進行擴展,結合長按監聽、WindowManager 浮層與數據交換完成。

核心思路

  1. 監聽 GridView 的觸摸事件。
  2. 長按某個 item 後,生成一個浮動的拖拽圖像。
  3. 手指移動時,更新浮動圖像位置。
  4. 當拖拽位置變化時,交換數據源中的元素。

自定義 GridView

可以創建一個 DragGridView 類,繼承自 GridView,並在 dispatchTouchEvent 中處理按下、移動與抬起事件。

public class DragGridView extends GridView {
    private static final int PRESS_TIME = 1000;
    private boolean isDraging = false;
    private Bitmap mBitmap;
    private int mTouchPosition;
    private View mTouchItemView;
    private ImageView mDragImageView;
    private WindowManager mWindowManager;
    private WindowManager.LayoutParams mWindowLayoutParams;

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mHandler.postDelayed(mLongClickRunnable, PRESS_TIME);
                mTouchPosition = pointToPosition((int) ev.getX(), (int) ev.getY());
                if (mTouchPosition == AdapterView.INVALID_POSITION) {
                    return super.dispatchTouchEvent(ev);
                }
                mTouchItemView = getChildAt(mTouchPosition - getFirstVisiblePosition());
                mTouchItemView.setDrawingCacheEnabled(true);
                mBitmap = Bitmap.createBitmap(mTouchItemView.getDrawingCache());
                mTouchItemView.destroyDrawingCache();
                break;
            case MotionEvent.ACTION_MOVE:
                if (isDraging) {
                    onDragItem((int) ev.getX(), (int) ev.getY());
                }
                break;
            case MotionEvent.ACTION_UP:
                mHandler.removeCallbacks(mLongClickRunnable);
                onStopDrag();
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    private final Handler mHandler = new Handler();

    private final Runnable mLongClickRunnable = new Runnable() {
        @Override
        public void run() {
            isDraging = true;
            mTouchItemView.setVisibility(View.INVISIBLE);
            createDragView(mBitmap);
        }
    };
}

添加拖拽浮層

拖拽時可以通過 WindowManager 添加一個 ImageView,讓它跟隨手指移動。

private void createDragView(Bitmap bitmap) {
    mWindowLayoutParams = new WindowManager.LayoutParams();
    mWindowLayoutParams.format = PixelFormat.TRANSLUCENT;
    mWindowLayoutParams.gravity = Gravity.TOP | Gravity.LEFT;
    mWindowLayoutParams.alpha = 0.6f;
    mWindowLayoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
    mWindowLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
    mWindowLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
        | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;

    mDragImageView = new ImageView(getContext());
    mDragImageView.setImageBitmap(bitmap);
    mWindowManager.addView(mDragImageView, mWindowLayoutParams);
}

交換數據

當拖拽位置變化時,可以通過接口回調通知外部交換數據。

public interface OnChanageListener {
    void onChange(int from, int to);
}

在 Activity 中,可以對 List 數據進行交換:

dragVeiw.setOnChangeListener((from, to) -> {
    Collections.swap(dataSourceList, from, to);
    mSimpleAdapter.notifyDataSetChanged();
});

如果需要更平滑的拖拽效果,還可以加入動畫、邊界判斷與自動滾動邏輯。這種自定義方式能顯著提升列表的交互體驗。

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