GridView拖动效果实现
- 1. 重新GridView控件
package com.whbs.drag.widget;
import com.whbs.drag.DragGridActivity.DragGridAdapter;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.PixelFormat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
public class GragGridView extends GridView{
private static final String TAG = "GragGridView";
//定义基本的成员变量
private ImageView dragImageView; //拖动的影像
private int dragSrcPosition;//原始对应位置
private int dragPosition;//拖动到坐标对应的位置
//x,y坐标的计算
private int dragPointX; //按下坐标相对于当前项位置-- 相对
private int dragPointY;
private int dragOffsetX;//当前窗体和屏幕的距离 --绝对
private int dragOffsetY;
private WindowManager windowManager;//窗口控制类
private WindowManager.LayoutParams windowParams; //用于控制拖动项的显示参数
// private int scaledTouchSlop; //判断滑动的距离
private int upScrollBounce; //拖动时候,开始向上滚动的边界
private int downScrollBounce;//拖动时候,开始向下滚动的边界
public GragGridView(Context context) {
super(context);
}
public GragGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public GragGridView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
//触控拦截事件
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if(ev.getAction() == MotionEvent.ACTION_DOWN){
int x = (int)ev.getX();
int y = (int)ev.getY();
//选中数据项位置 ,
dragSrcPosition = dragPosition = pointToPosition(x, y);
if(dragPosition == AdapterView.INVALID_POSITION){//无效位置(超出边蛸,分割线)
return super.onInterceptTouchEvent(ev);
}
Log.i(TAG, "[onInterceptTouchEvent] dragSrcPosition:"+dragSrcPosition+",getFirstVisiblePosition():"+getFirstVisiblePosition());
//getFirstVisiblePosition()返回第一个dispaly在界面的view在adapter的位置 可能是0,也可能是4
ViewGroup itemView = (ViewGroup)getChildAt(dragPosition - getFirstVisiblePosition());
//计算按下的坐标相对当前项的位置
dragPointX = x - itemView.getLeft();//在当前项的X位置
dragPointY = y - itemView.getTop();
//当前窗体和屏幕的距离
dragOffsetX = (int) (ev.getRawX()-x);
dragOffsetY = (int) (ev.getRawY() -y);
Log.i(TAG, "[onInterceptTouchEvent] [x:"+x+",y:"+y+"],[rawX:"+ev.getRawX()+",rawY:"+ev.getRawY()+"]");
Log.i(TAG, "[onInterceptTouchEvent] [dragPointX:"+dragPointX+",dragPointY:"+dragPointY+"],[dragOffsetX:"+dragOffsetX+",dragOffsetY:"+dragOffsetY+"]");
//
// upScrollBounce = Math.min(y-scaledTouchSlop, getHeight()/4);
// downScrollBounce = Math.max(y+scaledTouchSlop, getHeight()*3/4);
upScrollBounce = Math.min(y, getHeight()/4);//向上可以滚动的距离
downScrollBounce = Math.max(y, getHeight()*3/4);//向下可以滚动的距离
itemView.setDrawingCacheEnabled(true);
Bitmap bm = Bitmap.createBitmap(itemView.getDrawingCache());
startDrag(bm,x,y);
}
return super.onInterceptTouchEvent(ev);
}
//开始拖动
public void startDrag(Bitmap bm,int x,int y){
stopDrag();
windowParams = new WindowManager.LayoutParams();
windowParams.gravity = Gravity.TOP|Gravity.LEFT;
windowParams.x = x - dragPointX + dragOffsetX;//计算当前项Left离窗体的距离
windowParams.y = y - dragPointY + dragOffsetY;//计算当前项Top离窗体的距离
windowParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
windowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
windowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
windowParams.format = PixelFormat.TRANSLUCENT;
windowParams.windowAnimations = 0;
ImageView imageView = new ImageView(getContext());
imageView.setImageBitmap(bm);
windowManager = (WindowManager)getContext().getSystemService(getContext().WINDOW_SERVICE);
windowManager.addView(imageView, windowParams);
dragImageView = imageView;
}
//停止拖到
public void stopDrag(){
if(dragImageView != null){
windowManager.removeView(dragImageView);
dragImageView = null;
}
}
//拖动
public void onDrag(int x,int y){
if(dragImageView != null){
windowParams.alpha = 0.9f;
windowParams.x = x - dragPointX + dragOffsetX;
windowParams.y = y - dragPointY + dragOffsetY;
windowManager.updateViewLayout(dragImageView, windowParams);
}
int tempPosition = pointToPosition(x, y);
if(tempPosition != INVALID_POSITION){
dragPosition = tempPosition;
}
if(y < upScrollBounce || y > downScrollBounce){
setSelection(dragPosition);
}
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if(dragImageView != null && dragPosition != INVALID_POSITION){
int action = ev.getAction();
switch(action){
case MotionEvent.ACTION_UP:
int upx = (int)ev.getX();
int upY = (int)ev.getY();
stopDrag();
onDrop(upx,upY);
break;
case MotionEvent.ACTION_MOVE:
int moveX = (int)ev.getX();
int moveY = (int)ev.getY();
onDrag(moveX, moveY);
break;
}
return true;
}
return super.onTouchEvent(ev);
}
//拖到落下
public void onDrop(int x,int y){
int tempPosition = pointToPosition(x,y);
if(tempPosition != INVALID_POSITION){
dragPosition = tempPosition;
}
//超出边界
if(y < getChildAt(0).getTop()){
dragPosition = 0;
}else if(y > getChildAt(getChildCount()-1).getBottom() ||
(y > getChildAt(getChildCount()-1).getTop() &&
x > getChildAt(getChildCount()-1).getRight())){
//超出下边界
dragPosition = getAdapter().getCount() -1;
}
//数据交换当前拖动的于拖到到位置上的图片交换
if(dragPosition != dragSrcPosition && dragPosition > -1 && dragPosition < getAdapter().getCount()){
DragGridAdapter adapter = (DragGridAdapter)getAdapter();
String dragSrcItem = adapter.getItem(dragSrcPosition);
String dragTargetItem = adapter.getItem(dragPosition);
adapter.remove(dragSrcItem);
adapter.insert(dragSrcItem, dragPosition);
adapter.remove(dragTargetItem);
adapter.insert(dragTargetItem, dragSrcPosition);
System.out.println("srcPosition="+dragSrcPosition+" dragPosition="+dragPosition);
//Toast.makeText(getContext(), adapter.getList().toString(), Toast.LENGTH_SHORT).show();
}
}
}
- 2. Activity调用实现
package com.whbs.drag;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import com.whbs.drag.widget.GragGridView;
public class DragGridActivity extends Activity {
private static List<String> list = null;
// private static List<Integer> res = null;
//自定义适配器
private DragGridAdapter adapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.drag_grid_activity);
initData();
//
GragGridView dragView = (GragGridView)findViewById(R.id.drag_grid);
// System.out.println("dragView"+dragView);
adapter = new DragGridAdapter(this, list);
dragView.setAdapter(adapter);
}
public void initData(){
list = new ArrayList<String>();
// res = new ArrayList<Integer>();
for(int i = 0; i < 12; i++){
list.add("grid_"+i%12);
//res.add(0x7f020000+i);
}
}
public class DragGridAdapter extends ArrayAdapter<String>{
public DragGridAdapter(Context context, List<String> objects) {
super(context, 0, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if(view == null){
view = LayoutInflater.from(getContext()).inflate(R.layout.drag_grid_item, null);
}
try {
//跟据文件名获取资源文件中的图片资源
Field f = R.drawable.class.getDeclaredField(getItem(position));
int i = f.getInt(R.drawable.class);
ImageView imageview = (ImageView)view.findViewById(R.id.drag_grid_item_image);
imageview.setImageResource(i);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
// }
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return view;
}
}
}
-
3. Xml文件
- 1. drag_grid_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg"
android:padding="10dip"
>
<com.whbs.drag.widget.GragGridView
android:id="@+id/drag_grid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#4682B4"
android:numColumns="4"
android:stretchMode="columnWidth"
android:verticalSpacing="5dip"
android:horizontalSpacing="5dip">
</com.whbs.drag.widget.GragGridView>
</LinearLayout>
- 2. drag_grid_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="5dip"
android:paddingRight="5dip"
>
<ImageView
android:id="@+id/drag_grid_item_image"
android:padding="5dip"
android:background="@drawable/grid_bg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>
- 4. AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.whbs.drag"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="DragGridActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>