我有一些关于拖放的问题。该过程运行拖放运行完美。我正在开发一个应用程序,用户可以从罐子中拖放糖果。当ACTION_DROP检测到jar布局时,它将增加值(max为10),当糖果落入机架时,该值将减小。
问题在于,当我将甜点放在同一布局上时,它会不断增加/减少,直到该值达到其最大/最小数量。我该怎么做才能避免这个问题?
class MyDragListener implements OnDragListener {
@Override
public boolean onDrag(View v, DragEvent event) {
View view = (View) event.getLocalState();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
// do nothing
break;
case DragEvent.ACTION_DRAG_ENTERED:
// v.setBackgroundDrawable(enterShape);
view.setVisibility(View.VISIBLE);
break;
case DragEvent.ACTION_DRAG_EXITED:
// v.setBackgroundDrawable(answerShape);
// v.setBackgroundDrawable(normalShape);
view.setVisibility(View.VISIBLE);
break;
case DragEvent.ACTION_DROP:
// // Dropped, reassign View to ViewGroup
ViewGroup owner = (ViewGroup) view.getParent();
Log.i("drop", "Id First :" + view.getId());
Log.i("drop", "Id Second :" + v.getId());
Log.i("value", "Value :" + view.getContentDescription());
owner.removeView(view);
if(v.getId()==R.id.jaroval)
{
RelativeLayout container = (RelativeLayout) v;
container.addView(view);
view.setVisibility(View.VISIBLE);
value++;
sound();
}
else{
GridLayout container = (GridLayout) v;
container.addView(view);
view.setVisibility(View.VISIBLE);
value--;
sound();
}
break;
case DragEvent.ACTION_DRAG_ENDED:
// v.setBackgroundDrawable(answerShape);
// v.setBackgroundDrawable(normalShape);
view.setVisibility(View.VISIBLE);
default:
break;
}
return true;
}
答案 0 :(得分:0)
使用此示例代码 activity_mail.xml
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/center"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/pinkLayout"
android:layout_width="fill_parent"
android:layout_height="210dp"
android:background="#FF8989"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/dragtext" />
</LinearLayout>
<LinearLayout
android:id="@+id/yellowLayout"
android:layout_width="fill_parent"
android:layout_height="250dp"
android:layout_marginTop="210dp"
android:background="#FFCC00"
android:orientation="vertical" >
</LinearLayout>
2 - ; MainActivity.java
@SuppressLint(&#34; NewApi&#34) 公共类MainActivity扩展Activity实现OnTouchListener, OnDragListener { private static final String LOGCAT = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.textView1).setOnTouchListener(this);
findViewById(R.id.pinkLayout).setOnDragListener(this);
findViewById(R.id.yellowLayout).setOnDragListener(this);
}
@SuppressLint("NewApi")
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(null, shadowBuilder, view, 0);
view.setVisibility(View.INVISIBLE);
return true;
} else {
return false;
}
}
public boolean onDrag(View layoutview, DragEvent dragevent) {
int action = dragevent.getAction();
switch (action) {
case DragEvent.ACTION_DRAG_STARTED:
Log.e(LOGCAT, "Drag event started");
break;
case DragEvent.ACTION_DRAG_ENTERED:
Log.e(LOGCAT, "Drag event entered into " + layoutview.toString());
break;
case DragEvent.ACTION_DRAG_EXITED:
Log.e(LOGCAT, "Drag event exited from " + layoutview.toString());
break;
case DragEvent.ACTION_DROP:
Log.e(LOGCAT, "Dropped");
View view = (View) dragevent.getLocalState();
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);
LinearLayout container = (LinearLayout) layoutview;
container.addView(view);
view.setVisibility(View.VISIBLE);
break;
case DragEvent.ACTION_DRAG_ENDED:
Log.e(LOGCAT, "Drag ended");
break;
default:
break;
}
return true;
}
}
在此示例中,首先删除DragEvent.ACTION_DROP条件实际视图,并将新视图添加到父视图,然后再显示该视图。 因此,您将在侧面gridview中复制新视图,并从甜视图布局中删除。
我希望您能理解这个示例并轻松实现您的代码。