同様の質問が投稿されていることを認識しており、解決策を見つけるためにそれらや他の多くのトピックなどを表示しました.
目標: シンプルなドラッグ アンド ドロップ。ユーザーが画面上で画像を移動し、別の画像の上または画面上の任意の場所にドロップします。
API: >11
完了:
- 画像をドラッグして別の画像の上に配置し、応答を得ることができます (Toast を使用して確認します)。
- 画面上の任意の場所に画像をドラッグして応答を得ることができます (Toast を使用して確認します)。
動作していません:
- 画面上のどこにでも画像をドラッグできず、指を離した場所に画像を配置できません
さまざまな方法を試しましたが、常にコンパイル エラーが発生します。私のコードを見て、画像を ACTION_DRAG_ENDED に配置するためのクリーンでシンプルな方法を誰かが推奨できますか (私は初心者であることを覚えておいてください)
ここに私のJavaコードがあります:
public class MainActivity extends Activity {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
boolean okToDrop;
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.oval).setOnTouchListener(new theTouchListener());
findViewById(R.id.square).setOnTouchListener(new theTouchListener());
findViewById(R.id.robot).setOnTouchListener(new theTouchListener());
findViewById(R.id.oval).setOnDragListener(new theDragListener());
findViewById(R.id.square).setOnDragListener(new theDragListener());
findViewById(R.id.robot).setOnDragListener(new theDragListener());
}
private final class theTouchListener implements OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
ClipData data = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(
view);
view.startDrag(data, shadowBuilder, view, 0);
view.setVisibility(View.VISIBLE);
return true;
} else {
return false;
}
}
}
class theDragListener implements OnDragListener {
@Override
public boolean onDrag(View view, DragEvent dragEvent) {
int dragAction = dragEvent.getAction();
View dragView = (View) dragEvent.getLocalState();
if (dragAction == DragEvent.ACTION_DRAG_EXITED) {
okToDrop = false;
} else if (dragAction == DragEvent.ACTION_DRAG_ENTERED) {
okToDrop = true;
} else if (dragAction == DragEvent.ACTION_DRAG_ENDED) {
if (dropEventNotHandled(dragEvent) == true) {
// Code to generate image goes here ***********************
Context context = getApplicationContext();
CharSequence text = "Action Dropped Not In Box!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
dragView.setVisibility(View.VISIBLE);
}
} else if (dragAction == DragEvent.ACTION_DROP && okToDrop) {
ImageView i = (ImageView) findViewById(R.id.square);
i.setImageResource(R.drawable.oval);
dragView.setVisibility(View.INVISIBLE);
Context context = getApplicationContext();
CharSequence text = "Action Resulted In It Being Dropped In The Box";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
return true;
}
private boolean dropEventNotHandled(DragEvent dragEvent) {
return !dragEvent.getResult();
}
}
そして私の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" >
<ImageView
android:id="@+id/square"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/square_box" />
<ImageView
android:id="@+id/oval"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="58dp"
android:layout_marginTop="58dp"
android:src="@drawable/oval" />
<ImageView
android:id="@+id/robot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/oval"
android:layout_below="@+id/square"
android:layout_marginTop="70dp"
android:src="@drawable/ic_launcher" />
</RelativeLayout>