Android拖放GridView

时间:2013-05-19 21:04:32

标签: android drag-and-drop android-gridview

我正试图在我的gridview上强制安卓[{3}}。我为网格适配器中的每个视图设置了一个dragListener。当网格不够长以便可滚动时,或者只要网格中的第一个视图可见,它就可以滚动,它就能很好地工作。滚动时,它会将动作向下移动一个位置,但是在顶部看不到许多视图而不是在正确的位置。

GridView在适配器内设置DragListener:

ImageView i = new ImageView;
i.setImageResource(ImageList.get(position));
i.setOnDragListener(new MyDragImageListener(position));
return i;

MyDragListener:

public class MyDragImageListener implements OnDragListener{

    int imagePosition;

    public MyDragImageListener(int position) {
        imagePosition = position;
    }

    @Override
    public boolean onDrag(View v, DragEvent event) {
        switch (event.getAction()) {
        case DragEvent.ACTION_DRAG_STARTED:
            Log.i("DRAG Started", "" + imagePosition);
          break;
        case DragEvent.ACTION_DRAG_ENTERED:
            Log.i("DRAG Entered", "" + imagePosition);
            ImageView imageEnter = (ImageView) Grid.getChildAt(imagePosition);
            imageEnter.setBackgroundColor(Color.argb(155, 100, 200, 255));
          break;
        case DragEvent.ACTION_DRAG_EXITED:        
            Log.i("DRAG Exited", "" + imagePosition);
            ImageView imageExit = (ImageView) Grid.getChildAt(imagePosition);
            imageExit.setBackgroundColor(Color.argb(0, 0, 0, 0));
          break;
        case DragEvent.ACTION_DROP:
            Log.i("DRAG Dropped", "" + imagePosition);

          break;
        case DragEvent.ACTION_DRAG_ENDED:
            Log.i("DRAG Ended", "" + imagePosition);
          default:
          break;
        }
        return true;
    }

}

1 个答案:

答案 0 :(得分:0)

你是如何处理触摸事件的?在一个地方提供所有触摸操作之间存在薄弱的平衡:

    @Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (mDragController == null || !mDragging) {
        if (mDragController!=null) mDragController.onInterceptTouchEvent(ev);
        if (mSwipeEnabled) {
            gestureDetector.onTouchEvent(ev);
            return false; //to make sure we receive further messages
        }
        else
            return super.onInterceptTouchEvent(ev); //for scroll, etc
    }
    else
    // controller to handle event
    return mDragController.onInterceptTouchEvent(ev);
}

/**
 * 
 */
@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (mDragController == null || !mDragging) {
        //if (!gestureDetector.onTouchEvent(ev))
            return super.onTouchEvent(ev);
    }
    // controller to handle event
    return mDragController.onTouchEvent(ev);
}

效果很好,提供此演示视频中显示的结果:http://www.youtube.com/watch?v=m4yktX3SWSs(视频说明中的源代码链接)。