Я разрабатываю приложение для Android, в котором пользователь может перетащить ImageView
в другой ImageView
. ImageView
необходимо поменять, чтобы перетащить ImageView
, если он удовлетворяет определенному условию, и если он не должен возвращаться туда, где он был. Я пытался это сделать, но я не могу заставить его работать.Перетащите ImageView на другой ImageView
код за XML-файл
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:columnWidth="320dp"
android:orientation="vertical"
android:rowCount="14"
android:stretchMode="columnWidth" >
<LinearLayout
android:id="@+id/topleft"
android:layout_width="match_parent"
android:layout_height="413dp"
android:layout_row="0"
android:background="@drawable/shape" >
<ImageView
android:id="@+id/test_house"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/test_house" />
</LinearLayout>
<LinearLayout
android:id="@+id/topright"
android:layout_width="match_parent"
android:layout_height="106dp"
android:background="@drawable/shape"
android:layout_row="13"
android:layout_column="0"
android:weightSum="1">
<ImageView
android:id="@+id/house"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/house"
android:layout_weight="0.26" />
<ImageView
android:id="@+id/face"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/face"
android:layout_weight="0.24" />
<ImageView
android:id="@+id/duck"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/duck"
android:layout_weight="0.24" />
</LinearLayout>
</GridLayout>
код позади Java файла
public class cs_game_one extends Activity {
boolean state;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cs_game_one);
findViewById(R.id.test_house).setOnTouchListener(new MyTouchListener());
findViewById(R.id.house).setOnTouchListener(new MyTouchListener());
findViewById(R.id.face).setOnTouchListener(new MyTouchListener());
findViewById(R.id.duck).setOnTouchListener(new MyTouchListener());
findViewById(R.id.topleft).setOnDragListener(new MyDragListener());
findViewById(R.id.topright).setOnDragListener(new MyDragListener());
}
private final class MyTouchListener 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.INVISIBLE);
return true;
} else {
return false;
}
}
}
class MyDragListener implements OnDragListener {
@Override
public boolean onDrag(View v, DragEvent e) {
if (e.getAction() == DragEvent.ACTION_DROP) {
View view = (View) e.getLocalState();
if (view.getId() == R.id.house && v.getId() == R.id.test_house){
ViewGroup from = (ViewGroup) view.getParent();
from.removeView(view);
v.setBackgroundResource(R.drawable.house);
state = true;
} else {
state = false;
}
}
return state;
}
}
}
Вы видите ошибки? Можете ли вы рассказать нам, что работает, а что нет, или вообще ничего не происходит, когда вы касаетесь представления? –
Я не получаю никаких ошибок, но когда я перетаскиваю изображение и отбрасываю его, просто исчезает, не заменяя «ImageView» – np1234