gpt4 book ai didi

android - 在android中拖放,掉落问题

转载 作者:行者123 更新时间:2023-11-30 02:53:53 25 4
gpt4 key购买 nike

我是 android 新手,在 android 中拖放时遇到问题。图像向右拖动,但是当我放下它们时,它们显示出问题。就像 image1 放在它的位置很好但是当 image2 被放下时它也会显示 image1 并且当 image3 被放下时它会显示 image1。

我的主要 Activity 代码是:

    public class Level1 extends Activity {

public boolean checkState = false;
ImageView car1,car2,car3,car4,car5,car6;
private boolean dragging = false;
private boolean dragging1 = false;
private boolean dragging2 = false;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level1);
car1 = (ImageView) findViewById(R.id.car4);
car2 = (ImageView) findViewById(R.id.car5);
car3 = (ImageView) findViewById(R.id.car6);
car4 = (ImageView) findViewById(R.id.car7);
car5 = (ImageView) findViewById(R.id.car8);
car6 = (ImageView) findViewById(R.id.car9);
findViewById(R.id.car4).setOnTouchListener(new MyTouchListener());
findViewById(R.id.car6).setOnTouchListener(new MyTouchListener());
findViewById(R.id.car8).setOnTouchListener(new MyTouchListener());
findViewById(R.id.lcar4).setOnDragListener(new MyDragListener());
findViewById(R.id.lcar5).setOnDragListener(new MyDragListener());
findViewById(R.id.lcar6).setOnDragListener(new MyDragListener());
findViewById(R.id.lcar7).setOnDragListener(new MyDragListener());
findViewById(R.id.lcar8).setOnDragListener(new MyDragListener());
findViewById(R.id.lcar9).setOnDragListener(new MyDragListener());
}

private final class MyTouchListener implements OnTouchListener{

@SuppressLint("NewApi")
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
// TODO Auto-generated method stub
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);
if(view == findViewById(R.id.car4)){
dragging = true;
}
else if(view == findViewById(R.id.car6)){
dragging1 = true;
}
else if(view == findViewById(R.id.car8)){
dragging2 = true;
}
}
return false;
}

}
@SuppressLint("NewApi")
class MyDragListener implements OnDragListener{

@Override
public boolean onDrag(View v, DragEvent event) {
// TODO Auto-generated method stub
//int action = event.getAction();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
// do nothing
break;
case DragEvent.ACTION_DRAG_ENTERED:
//v.setBackgroundDrawable(enterShape);
break;
case DragEvent.ACTION_DRAG_EXITED:
//v.setBackgroundDrawable(normalShape);
break;
case DragEvent.ACTION_DROP:
// Dropped, reassign View to ViewGroup
if(dragging){
if(v == findViewById(R.id.lcar5)){
View view1 = (View) event.getLocalState();
ViewGroup owner1 = (ViewGroup) view1.getParent();
owner1.removeView(view1);
LinearLayout container1 = (LinearLayout) v;
container1.addView(view1);
view1.setVisibility(View.INVISIBLE);
car2.setImageResource(R.id.car4);
dragging = false;
dragging1 = false;
dragging2 = false;
}
}
if(dragging1){
if(v == findViewById(R.id.lcar7)){
View view2 = (View) event.getLocalState();
ViewGroup owner2 = (ViewGroup) view2.getParent();
owner2.removeView(view2);
LinearLayout container2 = (LinearLayout) v;
container2.addView(view2);
view2.setVisibility(View.INVISIBLE);
car4.setImageResource(R.id.car6);
dragging = false;
dragging1 = false;
dragging2 = false;
}
}
if(dragging2){
if(v == findViewById(R.id.lcar9)){
View view3 = (View) event.getLocalState();
ViewGroup owner3 = (ViewGroup) view3.getParent();
owner3.removeView(view3);
LinearLayout container3 = (LinearLayout) v;
container3.addView(view3);
view3.setVisibility(View.INVISIBLE);
car6.setImageResource(R.id.car4);
dragging = false;
dragging1 = false;
dragging2 = false;
}
}
else{
View view = (View) event.getLocalState();
view.setVisibility(View.VISIBLE);
break;
}

break;
case DragEvent.ACTION_DRAG_ENDED:
//v.setBackgroundDrawable(normalShape);
break;
default:
break;
}
return true;
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.level1, menu);
return true;
}

xml文件如下:

    <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"
tools:context=".Level1"
android:orientation="horizontal"
android:weightSum="4" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="3"
android:background="#800080"
android:weightSum="3">
<LinearLayout
android:id="@+id/lcar4"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="5dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/car4"
android:id="@+id/car4"/>
</LinearLayout>
<LinearLayout
android:id="@+id/lcar6"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="5dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/car6"
android:id="@+id/car6"/>
</LinearLayout>
<LinearLayout
android:id="@+id/lcar8"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="5dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/car8"
android:id="@+id/car8"/>
</LinearLayout>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1"
android:background="#4c8bff"
android:weightSum="2">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:id="@+id/lcar5">

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/car5"
android:id="@+id/car5"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"/>

</LinearLayout>
<LinearLayout

android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:weightSum="2">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/lcar7">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/car7"
android:id="@+id/car7"
android:layout_marginTop="5dp"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/lcar9">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/car9"
android:id="@+id/car9"
android:layout_marginTop="5dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>

请帮助我。我深陷其中。

最佳答案

试试这个,我已经更改了你代码中的几行

公共(public)类 Level1 扩展 Activity {

public boolean checkState = false;
ImageView car1, car2, car3, car4, car5, car6;
private boolean dragging = false;
private boolean dragging1 = false;
private boolean dragging2 = false;

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level1);
car1 = (ImageView) findViewById(R.id.car4);
car2 = (ImageView) findViewById(R.id.car5);
car3 = (ImageView) findViewById(R.id.car6);
car4 = (ImageView) findViewById(R.id.car7);
car5 = (ImageView) findViewById(R.id.car8);
car6 = (ImageView) findViewById(R.id.car9);

findViewById(R.id.car4).setOnTouchListener(new MyTouchListener());
findViewById(R.id.car6).setOnTouchListener(new MyTouchListener());
findViewById(R.id.car8).setOnTouchListener(new MyTouchListener());

findViewById(R.id.lcar4).setOnDragListener(new MyDragListener());
findViewById(R.id.lcar5).setOnDragListener(new MyDragListener());
findViewById(R.id.lcar6).setOnDragListener(new MyDragListener());
findViewById(R.id.lcar7).setOnDragListener(new MyDragListener());
findViewById(R.id.lcar8).setOnDragListener(new MyDragListener());
findViewById(R.id.lcar9).setOnDragListener(new MyDragListener());
}

private final class MyTouchListener implements OnTouchListener {

@SuppressLint("NewApi")
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
// TODO Auto-generated method stub
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);
if (view == findViewById(R.id.car4)) {
dragging = true;
dragging1 = false;
dragging2 = false;
} else if (view == findViewById(R.id.car6)) {
dragging1 = true;
dragging = false;
dragging2 = false;
} else if (view == findViewById(R.id.car8)) {
dragging2 = true;
dragging1 = false;
dragging = false;
}
return true;
}
return false;
}

}

@SuppressLint("NewApi")
class MyDragListener implements OnDragListener {

@Override
public boolean onDrag(View v, DragEvent event) {
// TODO Auto-generated method stub
// int action = event.getAction();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
// do nothing
break;
case DragEvent.ACTION_DRAG_ENTERED:
// v.setBackgroundDrawable(enterShape);
break;
case DragEvent.ACTION_DRAG_EXITED:
// v.setBackgroundDrawable(normalShape);
break;
case DragEvent.ACTION_DROP:
// Dropped, reassign View to ViewGroup
if (dragging) {
if (v == findViewById(R.id.lcar5)) {
Toast.makeText(getBaseContext(), "at green ",
Toast.LENGTH_SHORT).show();
View view1 = (View) event.getLocalState();
ViewGroup owner1 = (ViewGroup) view1.getParent();
owner1.removeView(view1);
LinearLayout container1 = (LinearLayout) v;
container1.addView(view1);
view1.setVisibility(View.INVISIBLE);
car2.setImageResource(R.id.car4);
break;
} else {
View view = (View) event.getLocalState();
view.setVisibility(View.VISIBLE);
break;
}
}
if (dragging1) {
if (v == findViewById(R.id.lcar7)) {
Toast.makeText(getBaseContext(), "at blue ",
Toast.LENGTH_SHORT).show();
View view2 = (View) event.getLocalState();
ViewGroup owner2 = (ViewGroup) view2.getParent();
owner2.removeView(view2);
LinearLayout container2 = (LinearLayout) v;
container2.addView(view2);
view2.setVisibility(View.INVISIBLE);
car4.setImageResource(R.id.car6);
break;
} else {
View view = (View) event.getLocalState();
view.setVisibility(View.VISIBLE);
break;
}
}
if (dragging2) {
if (v == findViewById(R.id.lcar9)) {
Toast.makeText(getBaseContext(), "at red ",
Toast.LENGTH_SHORT).show();
View view3 = (View) event.getLocalState();
ViewGroup owner3 = (ViewGroup) view3.getParent();
owner3.removeView(view3);
LinearLayout container3 = (LinearLayout) v;
container3.addView(view3);
view3.setVisibility(View.INVISIBLE);
car6.setImageResource(R.id.car4);
break;
} else {
View view = (View) event.getLocalState();
view.setVisibility(View.VISIBLE);
break;
}

}

break;
case DragEvent.ACTION_DRAG_ENDED:
// v.setBackgroundDrawable(normalShape);
break;
default:
break;
}
return true;
}
}

关于android - 在android中拖放,掉落问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23651771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com