1

我试图在布局之间拖放 textview 效果很好。但它只会在最后一个位置下降,并且在尝试插入 textview 之间时不会下降。我只是喜欢这个链接。我正在尝试,但我不知道。请帮我。

am 实现 OnTouchListener 和 OnDragListener

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    findViewById(R.id.textView1).setOnTouchListener(this);
    findViewById(R.id.textView2).setOnTouchListener(this);
    findViewById(R.id.textView3).setOnTouchListener(this);
    findViewById(R.id.textView4).setOnTouchListener(this);
    findViewById(R.id.textView5).setOnTouchListener(this);
    findViewById(R.id.textView6).setOnTouchListener(this);
    findViewById(R.id.textView7).setOnTouchListener(this);
    findViewById(R.id.textView8).setOnTouchListener(this);
    findViewById(R.id.textView9).setOnTouchListener(this);
    findViewById(R.id.pinkLayout).setOnDragListener(this);
    findViewById(R.id.yellowLayout).setOnDragListener(this);
    findViewById(R.id.whiteLayout).setOnDragListener(this);
}

public boolean onTouch(View view, MotionEvent motionEvent) {
    if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
        DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
        view.startDrag(null, shadowBuilder, view, 0);
        view.setVisibility(View.INVISIBLE);
        return true;
    } else {
        return false;
    }
}

public boolean onDrag(View layoutview, DragEvent dragevent) {
    int action = dragevent.getAction();
    switch (action) {
    case DragEvent.ACTION_DRAG_STARTED:
        Log.d(LOGCAT, "Drag event started");
        break;
    case DragEvent.ACTION_DRAG_ENTERED:
        Log.d(LOGCAT, "Drag event entered into " + layoutview.toString());
        break;
    case DragEvent.ACTION_DRAG_EXITED:
        Log.d(LOGCAT, "Drag event exited from " + layoutview.toString());
        break;
    case DragEvent.ACTION_DROP:
        Log.d(LOGCAT, "Dropped");

            Log.d("This is ", "Linear Layout");
            View view = (View) dragevent.getLocalState();
            ViewGroup owner = (ViewGroup) view.getParent();
            owner.removeView(view);
            LinearLayout container = (LinearLayout) layoutview;
            container.addView(view);
            view.setVisibility(View.VISIBLE);


        break;
    case DragEvent.ACTION_DRAG_ENDED:
        Log.d(LOGCAT, "Drag ended");
        break;
    default:
        break;
    }
    return true;
 }

xml布局

<LinearLayout
    android:id="@+id/pinkLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="#FF8989"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/dragtext" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Drag me" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Drag soon" />
</LinearLayout>

<LinearLayout
    android:id="@+id/yellowLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="#FFCC00"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Drag upon" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Drag me" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Draged" />
</LinearLayout>

<LinearLayout
    android:id="@+id/whiteLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="#fff"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Drag out"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Drag in"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Draged"
        android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>

4

1 回答 1

-1

嘿试试这个代码...

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="200dp"
        android:layout_height="150dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="38dp"
        android:layout_marginTop="43dp"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="150dp"
        android:layout_alignLeft="@+id/linearLayout1"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="55dp"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

    </LinearLayout>

</RelativeLayout>
于 2013-07-30T06:03:01.843 回答