- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 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/
抱歉,我不知道如何正确表达标题。但我正在制作一个 Java 游戏,屏幕底部有一个桨,您可以左右移动它以避免小行星坠落。我的桨可以工作,但我不知道如何制作一颗下落的小行星。小行星目前只是一个矩形。我还希
我制作了一个 x=25 & y= 25 的正方形并附加了一个 SKPhysicsBody。我制作了一个标准平台关卡,其中有几个高的下降区域。我希望正方形“破裂”/因从高处坠落而受到损坏。我查了一下维基
我是一名初级游戏程序员,我正在学习如何使用适用于 Android 的 AndEngine。我想让一个 Sprite 从屏幕上掉下来,并在用户触摸时向上跳。关于如何做到这一点的任何想法?如果这样更容易,
我刚刚开始在我的 Ogre 项目中实现 bullet。我按照此处的安装说明进行操作:http://www.ogre3d.org/tikiwiki/OgreBullet+Tutorial+1 剩下的教程
将我的项目升级到 iOS7 后 当我执行 BACK 按钮并且 UINavigationController 返回上一页时,屏幕顶部的 ImageView 会向下移动。 我使用 IB 来做我的布局。这些
我有两个内联 div,其中一个内联 div 有两个彼此下方的 div。 JSFiddle 点击#notLoginStudentBtn 我在做什么我正在切换#notLoginStudentBox 但在切
我有一个移动的对象,它生成由滑动关节连接的 object2 当我按下按钮时,关节被移除,object2 在重力作用下落下 这可行,但只在 x 轴上落下而不会保持其 y 轴速度。 如何让 Object2
如何从购物车中删除商品? 自然地,您希望能够将项目拖放回去。 $(function() { $( "#catalog" ).accordion(); $( "#cata
Swift 标准库中是否有作用于集合、采用谓词并返回从该集合中移除的值的函数? 目前,我必须分两步实现它: guard let idx = allAnnotations.index(where: {$
我真的需要一些关于我的游戏对象的帮助。 我正在开发一款游戏,我想要一个拾取元素来产生物理力爆炸来炸毁敌人。我做了一个简单的炸弹对象来测试这个想法。我添加了一个简单的代码,使用一个循环来收集其半径内的所
平台:iOS (Swift)Mapbox SDK版本:3.6 嗨,我有一个自定义 MGLCalloutView,当我点击 MGLAnnotationView 时,标注会按预期显示在注释上方,但如果我稍
我正在使用 WKWebView 查看自定义 HTML。 无论 HTML 内容如何,在真实设备上进行测试时,我都会在 Could not signal service com.apple.WebKi
我正在使用 WKWebView 查看自定义 HTML。 无论 HTML 内容如何,在真实设备上进行测试时,我都会在 Could not signal service com.apple.WebKi
我是一名优秀的程序员,十分优秀!