我想要实现的目标:
我想在Android中创建拖放功能.我想使用特定的布局(与拖动的对象本身不同)作为拖动阴影.
我得到了什么结果:
我的方法都没有按预期工作 - 我最终没有可见的拖动阴影(虽然目标确实接收到了掉落).
我尝试了什么:
我试过了
drag_item在活动中展开布局,然后将其作为参数传递给阴影构建器的构造函数和
drag_item在阴影生成器的onDrawShadow方法中膨胀布局,然后在其上绘制canvas布局:
我的活动布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.app.DragDropTestActivity"
tools:ignore="MergeRootFrame">
<TextView
android:id="@+id/tvReceiver"
android:text="Drop here"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btnDragged"
android:layout_height="wrap_content"
android:text="Drag me"
android:layout_width="match_parent"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我想用作拖动阴影的布局:
dragged_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dragged Item"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
源代码:
下面是用两种方法的代码(由下式表示1,BuilderOne并且2,BuilderTwo分别为):
package com.example.app;
import android.graphics.Canvas;
import android.graphics.Point;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button; …Run Code Online (Sandbox Code Playgroud)