本文執行個體為大家分享了Android實作可拖拽GridView的具體代碼,供大家參考,具體内容如下
先看看效果圖
主要思想:
1、監聽觸碰事件
2、用WindowManager添加拖曳的圖檔
3、用Collections.swap()交換List資料
自定義代碼:
public class DragGridVeiw extends GridView {
private final int PRESS_TIME = 1000;//長按時間
private int mDownX;//觸碰時的X坐标
private int mDownY;//觸碰時的Y坐标
private int mMoveX;//移動時的X坐标
private int mMoveY;//移動時的Y坐标
private int mOffset2Top;//DragGridView距離螢幕頂部的偏移量
private int mOffset2Left;//DragGridView距離螢幕左邊的偏移量
private int mPointToItemTop;//觸碰點距離ItemView的上邊距
private int mPointToItemLeft;//觸碰點距離ItemView的左邊距
private int mStatusHeight;//狀态欄高度
private boolean isDraging;//是否正在拖曳
private Bitmap mBitmap;//ItemView的圖檔
private int mTouchPostiion;//觸碰的位置
private View mTouchItemView;//觸碰的ItemView
private Vibrator mVibrator;//震動器
private ImageView mDragImageView;//拖曳的View
private WindowManager mWindowManager;//視窗管理器
private WindowManager.LayoutParams mWindowLayoutParams;//視窗管理器布局
private OnChanageListener onChanageListener;//交換事件監聽器
private Handler mHandler = new Handler();
public DragGridVeiw(Context context) {
this(context, null);
}
public DragGridVeiw(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public DragGridVeiw(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mStatusHeight = getStatusHeight(context);
mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
//使用Handler延遲dragResponseMS執行mLongClickRunnable
mHandler.postDelayed(mLongClickRunnable, PRESS_TIME);
mDownX = (int) ev.getX();
mDownY = (int) ev.getY();
//根據按下的X,Y坐标擷取所點選item的position
mTouchPostiion = pointToPosition(mDownX, mDownY);
if (mTouchPostiion == AdapterView.INVALID_POSITION) {
return super.dispatchTouchEvent(ev);
}
//根據position擷取該item所對應的View
mTouchItemView = getChildAt(mTouchPostiion - getFirstVisiblePosition());
//下面這幾個距離大家可以參考我的部落格上面的圖來了解下
mPointToItemTop = mDownY - mTouchItemView.getTop();
mPointToItemLeft = mDownX - mTouchItemView.getLeft();
mOffset2Top = (int) (ev.getRawY() - mDownY);
mOffset2Left = (int) (ev.getRawX() - mDownX);
//開啟mDragItemView繪圖緩存
mTouchItemView.setDrawingCacheEnabled(true);
//擷取mDragItemView在緩存中的Bitmap對象
mBitmap = Bitmap.createBitmap(mTouchItemView.getDrawingCache());
//這一步很關鍵,釋放繪圖緩存,避免出現重複的鏡像
mTouchItemView.destroyDrawingCache();
break;
case MotionEvent.ACTION_MOVE:
int moveX = (int) ev.getX();
int moveY = (int) ev.getY();
//拖曳點超出GridView區域則取消拖曳事件
if (ev.getY() > getHeight() || ev.getY() < 0) {
onStopDrag();
}
//如果我們在按下的item上面移動,隻要超過item的邊界就移除mRunnable
if (!isTouchInItem(mTouchItemView, moveX, moveY)) {
mHandler.removeCallbacks(mLongClickRunnable);
}
break;
case MotionEvent.ACTION_UP:
mHandler.removeCallbacks(mLongClickRunnable);
break;
}
return super.dispatchTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (isDraging && mDragImageView != null) {
switch (ev.getAction()) {
case MotionEvent.ACTION_MOVE:
mMoveX = (int) ev.getX();
mMoveY = (int) ev.getY();
//拖動item
onDragItem(mMoveX, mMoveY);
break;
case MotionEvent.ACTION_UP:
onStopDrag();
break;
}
return true;
}
return super.onTouchEvent(ev);
}
//處理長按事件的線程
private Runnable mLongClickRunnable = new Runnable() {
@Override
public void run() {
isDraging = true; //設定可以拖拽
mVibrator.vibrate(50); //震動一下
mTouchItemView.setVisibility(View.INVISIBLE);//隐藏該ItemView
//根據我們按下的點顯示ItemView鏡像
createDragView(mBitmap, mDownX, mDownY);
}
};
//添加拖動View
private void createDragView(Bitmap bitmap, int downX, int downY) {
mWindowLayoutParams = new WindowManager.LayoutParams();
mWindowLayoutParams.format = PixelFormat.TRANSLUCENT; //圖檔之外的其他地方透明
mWindowLayoutParams.gravity = Gravity.TOP | Gravity.LEFT;
mWindowLayoutParams.x = downX - mPointToItemTop + mOffset2Left;
mWindowLayoutParams.y = downY - mPointToItemTop + mOffset2Top - mStatusHeight;
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);
}
private void removeDragView() {
if (mDragImageView != null) {
mWindowManager.removeView(mDragImageView);
mDragImageView = null;
}
}
//是否點選在GridView的item上面
private boolean isTouchInItem(View dragView, int x, int y) {
int leftOffset = dragView.getLeft();
int topOffset = dragView.getTop();
if (x < leftOffset || x > leftOffset + dragView.getWidth()) {
return false;
}
if (y < topOffset || y > topOffset + dragView.getHeight()) {
return false;
}
return true;
}
//拖動事件處理
private void onDragItem(int moveX, int moveY) {
mWindowLayoutParams.x = moveX - mPointToItemLeft + mOffset2Left;
mWindowLayoutParams.y = moveY - mPointToItemTop + mOffset2Top - mStatusHeight;
mWindowManager.updateViewLayout(mDragImageView, mWindowLayoutParams); //更新DragView的位置
onSwapItem(moveX, moveY);//Item的互相交換
}
//交換item,并且控制item之間的顯示與隐藏效果
private void onSwapItem(int moveX, int moveY) {
//擷取我們手指移動到的那個item的position
int tempPosition = pointToPosition(moveX, moveY);
//假如tempPosition 改變了并且tempPosition不等于-1,則進行交換
if (tempPosition != mTouchPostiion && tempPosition != AdapterView.INVALID_POSITION) {
getChildAt(tempPosition - getFirstVisiblePosition()).setVisibility(View.INVISIBLE);//拖動到了新的item,新的item隐藏掉
getChildAt(mTouchPostiion - getFirstVisiblePosition()).setVisibility(View.VISIBLE);//之前的item顯示出來
if (onChanageListener != null) {
onChanageListener.onChange(mTouchPostiion, tempPosition);
}
mTouchPostiion = tempPosition;
}
}
//停止拖拽我們将之前隐藏的item顯示出來,并将DragView移除
private void onStopDrag() {
isDraging = false;
getChildAt(mTouchPostiion - getFirstVisiblePosition()).setVisibility(View.VISIBLE);
removeDragView();
}
//Item交換事件監聽
public void setOnChangeListener(OnChanageListener onChanageListener) {
this.onChanageListener = onChanageListener;
}
//擷取狀态欄高度
private int getStatusHeight(Context context) {
int statusHeight = 0;
Rect localRect = new Rect();
((Activity) context).getWindow().getDecorView().getWindowVisibleDisplayFrame(localRect);
statusHeight = localRect.top;
if (0 == statusHeight) {
Class> localClass;
try {
localClass = Class.forName("com.android.internal.R$dimen");
Object localObject = localClass.newInstance();
int i5 = Integer.parseInt(localClass.getField("status_bar_height").get(localObject).toString());
statusHeight = context.getResources().getDimensionPixelSize(i5);
} catch (Exception e) {
e.printStackTrace();
}
}
return statusHeight;
}
//當item交換位置的時候回調的方法,我們隻需要在該方法中實作資料的交換即可
public interface OnChanageListener {
public void onChange(int from, int to);
}
}
使用方法:
List> dataSourceList = new ArrayList<>();
dragVeiw = (DragGridVeiw) findViewById(R.id.view_drag);
for (int i = 0; i < 8; i++) {
HashMap itemHashMap = new HashMap<>();
itemHashMap.put("item_image", R.drawable.sample_1);
itemHashMap.put("item_text", "拖拽 " + Integer.toString(i));
dataSourceList.add(itemHashMap);
}
final SimpleAdapter mSimpleAdapter = new SimpleAdapter(this, dataSourceList,
R.layout.item_drag, new String[]{"item_image", "item_text"},
new int[]{R.id.item_image, R.id.item_text});
dragVeiw.setAdapter(mSimpleAdapter);
dragVeiw.setOnChangeListener(new DragGridVeiw.OnChanageListener() {
@Override
public void onChange(int from, int to) {
HashMap temp = dataSourceList.get(from);
//這裡的處理需要注意下
if (from < to) {
for (int i = from; i < to; i++) {
Collections.swap(dataSourceList, i, i + 1);
}
} else if (from > to) {
for (int i = from; i > to; i--) {
Collections.swap(dataSourceList, i, i - 1);
}
}
dataSourceList.set(to, temp);
mSimpleAdapter.notifyDataSetChanged();
}
});
附錄:
Log.v("-->getWidth", String.valueOf(getWidth()));//DragView的寬度
Log.v("-->getHeight", String.valueOf(getHeight()));//DragView的高度
Log.v("-->getLeft", String.valueOf(getLeft()));//DragView左邊距離螢幕左側的長度
Log.v("-->getTop", String.valueOf(getTop()));///DragView上邊距離螢幕頂部的長度
Log.v("-->getRawX", String.valueOf(ev.getRawX()));//觸碰點相對于螢幕的X坐标
Log.v("-->getRawY", String.valueOf(ev.getRawY()));//觸碰點相對于螢幕的Y坐标
Log.v("-->getX", String.valueOf(ev.getX()));//觸碰點相對于DragView的X坐标
Log.v("-->getY", String.valueOf(ev.getY()));//觸碰點相對于DragView的Y坐标
Log.v("-->getItemWidth", String.valueOf(mTouchItemView.getWidth()));//DragView中ItemView的寬度
Log.v("-->getItemHeight", String.valueOf(mTouchItemView.getHeight()));//DragView中ItemView的高度
Log.v("-->getItemLeft", String.valueOf(mTouchItemView.getLeft()));//DragView中ItemView左邊距離DragView左側的長度
Log.v("-->getItemTop", String.valueOf(mTouchItemView.getTop()));//DragView中ItemView上邊距離DragView頂部的長度
以上就是本文的全部内容,希望對大家的學習有所幫助,也希望大家多多支援腳本之家。