Android拖放问题不显示 [英] Android drag and drop issue not displaying
本文介绍了Android拖放问题不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下是我使用的代码:
ImageView iv1,iv2 ,iv3;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initControls();
}
private void initControls(){
// TODO自动生成的方法存根
iv1 =(ImageView)findViewById R.id.imageView1);
iv2 =(ImageView)findViewById(R.id.imageView2);
iv1.setTag(a);
iv2.setTag(b);
iv3 =(ImageView)findViewById(R.id.imageView3);
iv1.setOnTouchListener(new MyTouchListener());
iv2.setOnTouchListener(new MyTouchListener());
iv3.setOnDragListener(new MyDragListener());
}
私有最终类MyTouchListener实现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);
返回true;
} else {
return false;
}
}
}
class MyDragListener实现OnDragListener {
// Drawable enterShape = getResources()。getDrawable(R.drawable.shape_droptarget);
// Drawable normalShape = getResources()。getDrawable(R.drawable.shape);
@Override
public boolean onDrag(View v,DragEvent event){
switch(event.getAction()){
case DragEvent.ACTION_DRAG_LOCATION:
if(v!= iv3)
v.setVisibility(View.VISIBLE);
break;
case DragEvent.ACTION_DRAG_STARTED:
// do nothing
break;
case DragEvent.ACTION_DRAG_ENTERED:
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DROP:
//丢弃,重新分配视图到ViewGroup
//处理通过放置视图删除的拖动视图
View view =(View )event.getLocalState();
Drawable dropshadow = getResources()。getDrawable(R.drawable.dropshadow);
//查看拖动的项目被删除
ImageView dropTarget =(ImageView)v;
//查看被拖放
ImageView dropped =(ImageView)view;
/ *
//如果一个项目已经被删除,那将会有一个标签
Object tag = dropTarget.getTag();
//如果这里已经有一个项目,将其设置为在原始位置可见
if(tag!= null)
{
//标签是这里的视图ID已经丢失
int existingID =(Integer)标签;
//将原始视图再次设置为
findViewById(existingID).setVisibility(View.VISIBLE);
}
//将目标视图中的标签设置为要删除的视图的ID
dropTarget.setTag(dropped.getId());
* /
String temp =a;
if(temp.equals(view.getTag())){
//dropped.setBackground(dropshadow);
dropTarget.setBackground(dropshadow);
Log.d(temp,Correct Image);
} else {
Log.d(b,Wrong Image);
view.setVisibility(View.VISIBLE);
}
break;
case DragEvent.ACTION_DRAG_ENDED:
默认值:
break;
}
返回true;
}
}
我在这里缺少什么?任何帮助是真正的赞赏。谢谢。
解决方案
最后我自己解决了这个问题。我添加了这个方法:
private boolean dropEventNotHandled(DragEvent dragEvent){
return!dragEvent.getResult();
}
并添加:
case DragEvent.ACTION_DRAG_ENDED:
if(dropEventNotHandled(event))
view.setVisibility(View.VISIBLE);
break;
I'm working on a drag and drop application, everything is working fine but I saw an issue. I have 3 ImageViews, two of them are draggable objects and the other one is the drop target. Every time I drop the object in any location other than the drop target, it's totally disappearing!
Below is the code I used:
ImageView iv1, iv2, iv3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initControls();
}
private void initControls() {
// TODO Auto-generated method stub
iv1 = (ImageView) findViewById (R.id.imageView1);
iv2 = (ImageView) findViewById (R.id.imageView2);
iv1.setTag("a");
iv2.setTag("b");
iv3 = (ImageView) findViewById (R.id.imageView3);
iv1.setOnTouchListener(new MyTouchListener());
iv2.setOnTouchListener(new MyTouchListener());
iv3.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 {
//Drawable enterShape = getResources().getDrawable(R.drawable.shape_droptarget);
//Drawable normalShape = getResources().getDrawable(R.drawable.shape);
@Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_LOCATION:
if(v != iv3)
v.setVisibility(View.VISIBLE);
break;
case DragEvent.ACTION_DRAG_STARTED:
// Do nothing
break;
case DragEvent.ACTION_DRAG_ENTERED:
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DROP:
// Dropped, reassign View to ViewGroup
//handle the dragged view being dropped over a drop view
View view = (View) event.getLocalState();
Drawable dropshadow = getResources().getDrawable(R.drawable.dropshadow);
//view dragged item is being dropped on
ImageView dropTarget = (ImageView) v;
//view being dragged and dropped
ImageView dropped = (ImageView) view;
/*
//if an item has already been dropped here, there will be a tag
Object tag = dropTarget.getTag();
//if there is already an item here, set it back visible in its original place
if(tag!=null)
{
//the tag is the view id already dropped here
int existingID = (Integer)tag;
//set the original view visible again
findViewById(existingID).setVisibility(View.VISIBLE);
}
//set the tag in the target view to the ID of the view being dropped
dropTarget.setTag(dropped.getId());
*/
String temp = "a";
if(temp.equals(view.getTag())){
//dropped.setBackground(dropshadow);
dropTarget.setBackground(dropshadow);
Log.d(temp, "Correct Image");
} else {
Log.d("b", "Wrong Image");
view.setVisibility(View.VISIBLE);
}
break;
case DragEvent.ACTION_DRAG_ENDED:
default:
break;
}
return true;
}
}
What am I missing in here? Any help is truly appreciated. Thanks.
解决方案
Finally I solved this myself. I added this method:
private boolean dropEventNotHandled(DragEvent dragEvent) {
return !dragEvent.getResult();
}
and added this:
case DragEvent.ACTION_DRAG_ENDED:
if (dropEventNotHandled(event))
view.setVisibility(View.VISIBLE);
break;
这篇关于Android拖放问题不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文