Android 无法进行元素拖放

Android 无法进行元素拖放,android,Android,我想考字母表。我有一个ImageView11,它显示了一个随机的字母表,你必须猜测。imageView_alphabet_image_1和imageView_alphabet_image_2将显示两个选项,我必须猜测这两个选项,我将把ImageView11图像拖到正确的imageView上,这是向下显示的。但我只能在第一次点击refresh时才这么做,因为它总是显示不正确的吐司 <LinearLayout xmlns:android="http://schemas.android.com/

我想考字母表。我有一个ImageView11,它显示了一个随机的字母表,你必须猜测。imageView_alphabet_image_1和imageView_alphabet_image_2将显示两个选项,我必须猜测这两个选项,我将把ImageView11图像拖到正确的imageView上,这是向下显示的。但我只能在第一次点击refresh时才这么做,因为它总是显示不正确的吐司

<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"
android:orientation="vertical" >
 <Button 
    android:id="@+id/btn_refresh"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Refresh"/>

<LinearLayout
    android:id="@+id/dragLinearLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imgView_des"
        android:layout_width="70dp"
        android:layout_height="80dp"
        android:src="@drawable/ic_launcher" />
</LinearLayout>

<LinearLayout
    android:id="@+id/bottomLinearLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="150dp"
    android:orientation="horizontal"
    android:weightSum="1" >

    <ImageView
        android:id="@+id/imgView_alphabetImage_1"
        android:layout_width="70dp"
        android:layout_height="80dp"
        android:layout_weight="0.25"
        android:src="@drawable/a" />
     <ImageView
        android:id="@+id/imgView_alphabetImage_2"
        android:layout_width="70dp"
        android:layout_height="80dp"
        android:layout_weight="0.25"
        android:src="@drawable/a" />
</LinearLayout>
package com.example.clearQuestion; 导入java.util.ArrayList; 导入java.util.Collections; 导入java.util.Random; 导入android.os.Bundle; 导入android.support.v7.app.ActionBarActivity; 导入android.util.Log; 导入android.view.DragEvent; 导入android.view.Menu; 导入android.view.MenuItem; 导入android.view.MotionEvent; 导入android.view.view; 导入android.view.view.DragShadowBuilder; 导入android.view.view.OnClickListener; 导入android.view.view.OnDragListener; 导入android.view.view.OnTouchListener; 导入android.view.ViewGroup; 导入android.widget.Button; 导入android.widget.ImageView; 导入android.widget.Toast; 公共类MainActivity扩展ActionBarActivity实现OnClickListener, OnTouchListener,OnDragListener{ 私有图像视图; 私有图像视图_imgView_字母图像_1; 私有数组列表_字母表_数组列表; 私人按钮_btn_刷新; 私有图像视图_imgView_字母图像2; 私有数组列表_tempArrayList; 私人阵列列表(finaltemparaylist);; @凌驾 受保护的void onCreateBundle savedInstanceState{ super.onCreatesavedInstanceState; initView; setContentViewR.layout.activity_main; _imgView_des=ImageView findViewByIdR.id.imgView_des; _imgView_des.setOnClickListenerthis; _imgView_des.Setontouch ListenerThis; _imgView_des.setOnDragListenerthis; _imgView_Alphamage_1=ImageView findViewByIdR.id.imgView_Alphamage_1; _imgView_字母图像1.setOnClickListenerthis; _imgView_字母图像1.setOnDragListenerthis; _imgView_Alphamage_2=ImageView findViewByIdR.id.imgView_Alphamage_2; _imgView_字母图像2.setOnClickListenerthis; _imgView_字母图像2.setOnDragListenerthis; _btn_刷新=按钮findViewByIdR.id.btn_刷新; _btn_refresh.setOnClickListenerthis; } 私有void initView{ _alphabet_arrayList=新的arrayList; _字母表_arrayList.addR.drawable.a; _字母表_arrayList.addR.drawable.b; _字母表_arrayList.addR.drawable.c; _字母表_arrayList.addR.drawable.d; _字母表_arrayList.addR.drawable.e; _字母表_arrayList.addR.drawable.f; _字母表_arrayList.addR.drawable.g; _tempArrayList=新的ArrayList; _finalTempArrayList=新的ArrayList; } @凌驾 公共布尔onCreateOptions菜单菜单{ //为菜单充气;这会将项目添加到操作栏(如果存在)。 getMenuInflater.inflateR.menu.main,菜单; 返回true; } @凌驾 公共布尔值OnOptions ItemSelectedMenuItem项{ int id=item.getItemId; 如果id==R.id.action\u设置{ 返回true; } 返回super.on选项ItemSelectedItem; } @凌驾 公共视图v{ 开关v.getId{ 案例R.id.imgView_des: 打破 案例R.id.imgView_字母图像_1: 如果"imgView"des .getDrawable getConstantState先生 .equals\u imgView\u字母图像\u 1.getDrawable getConstantState先生{ Toast.makeTextthis,matched,Toast.LENGTH\u SHORT.show; }否则{ Toast.maketext此,不匹配,Toast.LENGTH\u SHORT.show; } 打破 案例R.id.imgView_字母图像_2: 如果"imgView"des .getDrawable getConstantState先生 .equals\u imgView\u字母图像\u 2.getDrawable getConstantState先生{ Toast.makeTextthis,matched,Toast.LENGTH\u SHORT.show; }否则{ Toast.maketext此,不匹配,Toast.LENGTH\u SHORT.show; } 打破 案例R.id.btn\u刷新: 随机=新随机; int index=random.nextInt7; _imgView_des.setImageResource_alphabet_arrayList.getindex; _imgView_des.setVisibilityView.VISIBLE; _tempArrayList=ArrayList\u alphabet\u ArrayList.clone; _tempArrayList.removeindex; Collections.shuffle_tempArrayList,随机; 对于int j=0;j<1;j++{ _finalTempArrayList.add_tempArrayList.getj; } _finalTempArrayList.add_alphabet_arrayList.getindex; Collections.shuffle\u finaltemparaylist; Log.e_finaltemparaylist after suffel,_finaltemparaylist.toString; _imgView_字母图像_1.setImageResource_finalTempArrayList.get0; _imgView_字母图像_2.setImageResource_finalTempArrayList.get1; _finalTempArrayList.clear; 打破 违约: 打破 } } @凌驾 公共视图v,运动事件e{ 如果e.getAction==MotionEvent.ACTION\u向下{ //ClipData ClipData=ClipData.newPlainText; DragShadowBuilder shadowBuilder=新建视图。DragShadowBuilder; v、 startDragnull,阴影生成器,v,0; v、 setVisibilityView.INVISIBLE; 返回true; }否则{ 返回false; } } @凌驾 公共布尔函数 DragView v,DragEvent e{ 开关e.getAction{ 案例DrageEvent.ACTION\u DRAG\u已启动: //如果e.getClipDescription.hasMimeType //clipddescription.MIMETYPE\u TEXT\u PLAIN{ //返回true; //}其他{ //Toast.makeTextthis,无法接受图像, //Toast.LENGTH_SHORT.show; // // } //返回false; 打破 案例DrageEvent.ACTION_DROP: 如果"imgView"des .getDrawable getConstantState先生 .equals\u imgView\u字母图像\u 1.getDrawable getConstantState先生{ ViewGroup ViewGroup=ViewGroup v.getParent; viewGroup.removeView\u imgView\u des; v、 setBackgroundthis.getResources.getDrawableR.drawable.a; 返回true; } 否则,如果您看到 .getDrawable getConstantState先生 .equals\u imgView\u字母图像\u 2.getDrawable getConstantState先生 { ViewGroup ViewGroup=ViewGroup v.getParent; viewGroup.removeView\u imgView\u des; v、 setBackgroundthis.getResources.getDrawableR.drawable.a; 返回true; } //否则{ //返回false; // } 打破 案例DrageEvent.ACTION\u DRAG\u已结束: Log.va,e.getResult+; 如果e.getResult{ _imgView_des.setVisibilityView.INVISIBLE; Log.vasddd,e.getResult+; Toast.makeTextthis,接受图像, Toast.LENGTH_SHORT.show; 返回true; }否则{ _imgView_des.setVisibilityView.VISIBLE; 吐司吐司=新吐司; ImageView视图=新的ImageViewthis; view.setImageResourceR.drawable.unsuccess; toast.setViewview; 吐司.表演; 返回true; } 违约: 打破 } 返回false; }
} 我检查了你的代码,问题出在你的onDrag方法中

case DrageEvent.ACTION_DROP事件未调用,因为您未返回标志case DrageEvent.ACTION_DRAG_STARTED:事件。您必须传递如下代码所示的真标志

case DragEvent.ACTION_DRAG_STARTED:

return true;

在写了true之后,它也接受了错误的字母图像,并且显示了两个吐司times@Ashutoshsingh-您只需检查imageview的可绘制性。未计算拖动的imageview的位置。所以,如果您将图像拖到_imgView_Alphapomage_1上,然后在它之后将检查两个图像视图。所以,您需要检查第一个用户拖动的图像在哪个图像视图上。