0

I want to implement a Drag gesture within a LinearLayout view,and there are some components contained in it,such as TextView,button. Now i use APL level 11,3.0 SDK to design it according to Android offical document.the methods included in my codes are below:

onLongClick() --- trigger the Drag movtion with view.startDrag(clipdate, dragShadow, null,0)

onDrag() ---a drag listener, use view.layout(left, top, right, bottom), refresh the component's appreance has been drag

and problem i come across is , the other components disappear when i drag one in LinearLayou view, and when i drop the drag one ,it disappear with others.

so Plz anyone who can help me tell how it going wrong,or how can i implement drag gesture well with your suggestion. Firstly,thanks and appreciate every gus come here.

4

1 回答 1

0

我使用的代码如下:

@Override
public boolean onLongClick(View view) {
    // TODO Auto-generated method stub
    boolean result = true;
    if(DEBUG){
        Log.d(TAG, "onLongClick ()");
    }
    mLongTouch = true;
    ClipData.Item item = new ClipData.Item((CharSequence) view.getTag());
    ClipData dragData = new ClipData((CharSequence) view.getTag(), mTabDragMimeData, item);
    SimeTabDragListener.SimeTabDragShadow myShadow = new SimeTabDragListener.SimeTabDragShadow(view);
    view.startDrag(dragData, myShadow, null, 0);
    return result;
}

@Override
public boolean onDrag(View view, DragEvent event) {
    // TODO Auto-generated method stub
    boolean result = true;
    int action = event.getAction();
    int eventX = (int)event.getX();
    int eventY = (int)event.getY();
    if(DEBUG){
        Log.d(TAG, "onDrag()...event is  "+action + ".... X cor is "+event.getX()+"...Y  cor is"+event.getY());
    }
    view.layout(eventX - mDownOffsetsX, eventY - mDownOffsetsY,
            eventX + mDownViewWidth - mDownOffsetsX, eventY + mDownViewHeight - mDownOffsetsY);
    view.postInvalidate();
    return result;

// 返回 super.onDragEvent(event); }

// drag shadows 
public static class SimeTabDragShadow extends View.DragShadowBuilder {
            private static  Drawable shadow;
            public SimeTabDragShadow(View view) {
                super(view);
                if(DEBUG && view instanceof TextView)
                    Log.d(TAG, "drag view is text");
                shadow = new ColorDrawable(Color.LTGRAY);
            }
            @Override
            public void onProvideShadowMetrics(Point shadowSize,
                    Point shadowTouchPoint) {
                // TODO Auto-generated method stub
                super.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
            }
            @Override
            public void onDrawShadow(Canvas canvas) {
                // TODO Auto-generated method stub
                super.onDrawShadow(canvas);
            }


        }

而LinearLayout中包含的组件(TextView)是静态的,由XML加载,问题是拖动一个TextView时其他TextView会消失,放下它时会与其他TextView一起消失。

有没有人知道它是怎么回事,或者如何在 LayoutView 中实现拖动手势,谢谢大家来到这里。

于 2011-11-07T01:12:37.110 回答