Android拖放功能

时间:2016-06-23 03:37:06

标签: android android-layout

每次我在右侧视图中拖动它都会消失我是Android应用程序的初学者任何类型的帮助都会提前谢谢

这是我的MainActivity.java源代码

package com.condorpos.demoapp2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity implements View.OnTouchListener, View.OnDragListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //set ontouch listener for box view
    findViewById(R.id.box_view1).setOnTouchListener(this);
    findViewById(R.id.box_view2).setOnTouchListener(this);
    findViewById(R.id.box_view3).setOnTouchListener(this);
    findViewById(R.id.box_view4).setOnTouchListener(this);

    //set ondrag listener for right and left parent views
    findViewById(R.id.left_view).setOnDragListener(this);
    findViewById(R.id.right_view).setOnDragListener(this);
}

@Override
public boolean onDrag(View v, DragEvent event) {
    //TODO Auto-generated method stub
    if(event.getAction()==DragEvent.ACTION_DROP){
        //we want to make sure it is dropped only to left and right parent view
        View view = (View)event.getLocalState();

        if(v.getId() == R.id.left_view || v.getId() == R.id.right_view){

            ViewGroup source = (ViewGroup) view.getParent();
            source.removeView(view);

            LinearLayout target = (LinearLayout)v;
            target.addView(view);

        }
        // make view visible as we set visibility to invisible while starting drag
        view.setVisibility(View.INVISIBLE);
    }
    return true;
}

@Override
public boolean onTouch(View view, MotionEvent event) {
    // TODO Auto-generated method stub
    if(event.getAction() == MotionEvent.ACTION_DOWN){
        View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
        view.startDrag(null, shadowBuilder, view, 0);
        view.setVisibility(View.INVISIBLE);
        return true;
    }
    return false;
}

}

对于activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.condorpos.demoapp2.MainActivity">

<LinearLayout
    android:id="@+id/left_view"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:layout_weight="1"
    android:background="@android:color/darker_gray"
    android:gravity="center_vertical"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/box_view1"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_gravity="center_vertical|center_horizontal"
        android:layout_margin="10dp"
        android:background="@drawable/box_one"/>

    <ImageView
        android:id="@+id/box_view2"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_gravity="center_horizontal|center_vertical"
        android:layout_margin="10dp"
        android:background="@drawable/box_two"/>

    <ImageView
        android:id="@+id/box_view3"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_gravity="center_horizontal|center_vertical"
        android:layout_margin="10dp"
        android:background="@drawable/box_three"/>

    <ImageView
        android:id="@+id/box_view4"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_gravity="center_horizontal|center_vertical"
        android:layout_margin="10dp"
        android:background="@drawable/box_four"/>

</LinearLayout>

<LinearLayout
    android:id="@+id/right_view"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:layout_weight="1"
    android:background="@android:color/darker_gray"
    android:gravity="center_vertical"
    android:orientation="vertical">

</LinearLayout>

</LinearLayout>

谢谢你们。

1 个答案:

答案 0 :(得分:3)

在检测到操作时,或者在 addView 之后,您应该将可见性设置为 View.VISIBLE ,其中注释表示设置可见性(在操作中)。< / p>