さまざまな画像にドラッグ アンド ドロップ機能を追加してターゲットを特定できるチュートリアルがあるかどうか疑問に思っていました。
つまり、4 つのドラッグ可能な画像があるとします。image_drag_1
、、、および4 つimage_drag_2
のdrop_target イメージ。、、および。image_drag_3
image_drag_4
image_drop_1
image_drop_2
image_drop_3
image_drop_4
image_drag_1
image_drop_1 に一致する必要がありimage_drag_1
、画面レイアウト内の他のドロップ画像または場所にドロップしようとするimage_drag_1
と、元の位置にスナップバックされます。
このチュートリアル では、テキストビューのドラッグ ドロップ機能を実装しますが、ターゲットは指定しません。
上記のチュートリアルに従って、テキストビューをドラッグ可能にし、イメージビュー、ドロップターゲットを残して、独自のドラッグアンドドロップを実装しようとしました。エミュレータがクラッシュするだけです。
以下は私の.javaファイルです:
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View.DragShadowBuilder;
import android.view.View.OnDragListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.content.ClipData;
import android.graphics.Typeface;
public class MainActivity extends Activity {
//Imageview to drag the image is dragged into
private ImageView image1, image2;
//textview the image is dragged into
private TextView choice1, choice2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//views to drag
image1 = (ImageView)findViewById(R.id.imageView1);
image2 = (ImageView)findViewById(R.id.imageButton1);
//views to drop onto
choice1 = (TextView)findViewById(R.id.choice_1);
choice2 = (TextView)findViewById(R.id.choice_2);
//set touch listeners
image1.setOnTouchListener(new ChoiceTouchListener());
image2.setOnTouchListener(new ChoiceTouchListener());
//set drag listeners
choice1.setOnDragListener(new ChoiceDragListener());
choice2.setOnDragListener(new ChoiceDragListener());
}
private final class ChoiceTouchListener implements OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
/*
* Drag details: we only need default behavior
* - clip data could be set to pass data as part of drag
* - shadow can be tailored
*/
ClipData data = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
//start dragging the item touched
view.startDrag(data, shadowBuilder, view, 0);
return true;
} else {
return false;
}
}
}
/**
* DragListener will handle dragged views being dropped on the drop area
* - only the drop action will have processing added to it as we are not
* - amending the default behavior for other parts of the drag process
*
*/
private class ChoiceDragListener implements OnDragListener {
@Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
//no action necessary
break;
case DragEvent.ACTION_DRAG_ENTERED:
//no action necessary
break;
case DragEvent.ACTION_DRAG_EXITED:
//no action necessary
break;
case DragEvent.ACTION_DROP:
//handle the dragged view being dropped over a drop view
View view = (View) event.getLocalState();
//stop displaying the view where it was before it was dragged
view.setVisibility(View.INVISIBLE);
//view dragged item is being dropped on
TextView dropTarget = (TextView) v;
//view being dragged and dropped
ImageView dropped = (ImageView) view;
//update the text in the target view to reflect the data being dropped
dropTarget.setText(dropped.getImageAlpha());
//make it bold to highlight the fact that an item has been dropped
dropTarget.setTypeface(Typeface.DEFAULT_BOLD);
//if an item has already been dropped here, there will be a tag
Object tag = dropTarget.getTag();
//if there is already an item here, set it back visible in its original place
if(tag!=null)
{
//the tag is the view id already dropped here
int existingID = (Integer)tag;
//set the original view visible again
findViewById(existingID).setVisibility(View.VISIBLE);
}
//set the tag in the target view being dropped on - to the ID of the view being dropped
dropTarget.setTag(dropped.getId());
break;
case DragEvent.ACTION_DRAG_ENDED:
//no action necessary
break;
default:
break;
}
return true;
}
}
}
私のxmlファイルは以下の通りです:
<RelativeLayout 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"
tools:context=".MainActivity" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="66dp"
android:orientation="vertical"
android:padding="10dp"
android:paddingLeft="50dp"
android:paddingRight="50dp" >
<TextView
android:id="@+id/choice_1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_marginTop="46dp"
android:background="@drawable/choice"
android:gravity="center"
android:text="@string/choice_1" />
<TextView
android:id="@+id/choice_2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_marginTop="57dp"
android:background="@drawable/choice"
android:gravity="center"
android:text="@string/choice_2" />
</LinearLayout>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/linearLayout1"
android:layout_marginBottom="16dp"
android:layout_toRightOf="@+id/imageButton1"
android:contentDescription="@string/image_desc"
android:src="@drawable/firefighter" />
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:contentDescription="@string/image_desc"
android:src="@drawable/clown" />
私が間違っていることについてアドバイスはありますか?前もって感謝します。