在Activity上保持拖动和滚动视图

时间:2013-12-01 07:01:17

标签: android android-layout

我有两个linearlayout,我希望将一个linearlayout子项拖到另一个linearlayout,反之亦然。为此,我使用了以下代码。但是滚动和拖动是不舒服的。建议我解决。

XML:

<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/center"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:background="#FFFFFF" >

    <LinearLayout
        android:id="@+id/toplayout"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/img1"
            android:layout_width="102dp"
            android:layout_height="120dp"
            android:src="@drawable/pic1" />

        <ImageView
            android:id="@+id/img2"
            android:layout_width="102dp"
            android:layout_height="120dp"
            android:src="@drawable/pic2" />


    </LinearLayout>
</ScrollView>

<LinearLayout
    android:id="@+id/bottomlayout"
    android:layout_width="fill_parent"
    android:layout_height="200dp"
    android:layout_alignParentBottom="true"
    android:background="#FFCC00"
    android:orientation="horizontal" >
</LinearLayout>

活动:

public class MainActivity extends Activity implements OnTouchListener,
    OnDragListener {
private static final String LOGCAT = "drag";

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


    findViewById(R.id.img1).setOnTouchListener(this);
    findViewById(R.id.img2).setOnTouchListener(this);
    findViewById(R.id.toplayout).setOnDragListener(this);
    findViewById(R.id.bottomlayout).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");
        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;
}

}

1 个答案:

答案 0 :(得分:0)

我已经使用LongclickListener解决了这个问题。