本文介绍了使用拖放来确定身份?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid应用程序中,我有过一个单独的LinearLayout一个ImageView的作为重生点。围绕着重生更LinearLayouts。

比方说,ImageView的被称为蓝,还有其他2 LinearLayouts。一个叫做的blueArea和其他redArea。因此,如果蓝色投进的blueArea,行动将开始。如果蓝色投进redArea,另一行动将开始。

我基本上希望我滴知道什么是被丢弃,并在那里被丢弃。我能做些什么标记的ImageViews和LinearLayouts这样我就可以当视图上的布局检测?我怎么能这样做呢?

下面是我已经想出了这么远。

  // R.drawable.blue是ImageView的。
// R.id.blue是的LinearLayout。公共景观蓝色= findViewById(R.drawable.blue);
findViewById(R.id.blue).setOnTouchListener(新TouchListener());findViewById(R.id.blueArea).setOnDragListener(新DragListener());
findViewById(R.id.redArea).setOnDragListener(新DragListener());
findViewById(R.id.spawn).setOnDragListener(新DragListener());ClipData数据= ClipData.newPlainText(,);
DragShadowBuilder shadowBuilder;
私人final类TouchListener实现OnTouchListener {
    公共布尔onTouch(视图V,MotionEvent事件){
        INT行动= event.getAction();
        如果(动作== MotionEvent.ACTION_DOWN){
            shadowBuilder =新View.DragShadowBuilder(ⅴ);
            v.startDrag(数据,shadowBuilder,V,0);
            v.setVisibility(View.INVISIBLE);
            }
        返回true;
        }
    }布尔containsDraggable =真;
类DragListener实现OnDragListener {
公共ondrag当布尔(视图V,事件的dragEvent){
    INT行动= event.getAction();
    查看查看=(查看)event.getLocalState();
    如果(动作== DragEvent.ACTION_DRAG_STARTED){
        }
    如果(动作== DragEvent.ACTION_DRAG_ENTERED){
        containsDraggable =真;
        }
    如果(动作== DragEvent.ACTION_DRAG_EXITED){
        containsDraggable = FALSE;
        }
    如果(动作== DragEvent.ACTION_DRAG_LOCATION){
        // 去做
        }
    如果(动作== DragEvent.ACTION_DRAG_ENDED){
        如果(dropEventNotHandled(事件)){
            view.setVisibility(View.VISIBLE);
            }
        }
    如果(动作== DragEvent.ACTION_DROP&放大器;&安培; containsDraggable){
        view.setVisibility(View.VISIBLE);
        releaseTrue();
        }
    返回true;
    }
}
私人布尔dropEventNotHandled(的dragEvent事件){
    返回event.getResult()!;
    }
    公共无效releaseTrue(){
        得分++;
        textScore.setText(将String.valueOf(分数));
    }
    公共无效releaseFalse(){
        得分了 - ;
        textScore.setText(将String.valueOf(分数));
    }


解决方案

因为你要添加

  .setOnDragListener(新DragListener());

对于所有三个视图

 公共ondrag当布尔(视图V,事件的dragEvent){

将被称为与上述三种观点(即视图V)

所以你可以有switch语句,如果你想表现不同。

 开关(v.getId()){
案例R.id.blueArea:
//蓝色行动..
打破
}

In my Android application, I have an ImageView over a separate LinearLayout as a "spawn" point. Surrounding the "spawn" are more LinearLayouts.

Let's say the ImageView is called "blue", and there are 2 other LinearLayouts. One called "blueArea" and the other "redArea". So if blue is dropped into blueArea, an action would start. If blue is dropped into redArea, another action would start.

I basically want my drops to know what is being dropped and where it is being dropped. What can I do to "tag" the ImageViews and LinearLayouts so I can detect when the view is on the layout? and how can I do this?

Here's what I have came up with so far.

// R.drawable.blue is the ImageView.
// R.id.blue is the LinearLayout.

public View blue = findViewById(R.drawable.blue);
findViewById(R.id.blue).setOnTouchListener(new TouchListener());

findViewById(R.id.blueArea).setOnDragListener(new DragListener());
findViewById(R.id.redArea).setOnDragListener(new DragListener());
findViewById(R.id.spawn).setOnDragListener(new DragListener());

ClipData data = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder;
private final class TouchListener implements OnTouchListener {
    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction();
        if (action == MotionEvent.ACTION_DOWN) {
            shadowBuilder = new View.DragShadowBuilder(v);
            v.startDrag(data, shadowBuilder, v, 0);
            v.setVisibility(View.INVISIBLE);
            }
        return true;
        }
    }

boolean containsDraggable = true;
class DragListener implements OnDragListener {
public boolean onDrag(View v, DragEvent event) {
    int action = event.getAction();
    View view = (View) event.getLocalState();
    if (action == DragEvent.ACTION_DRAG_STARTED) {
        }
    if (action == DragEvent.ACTION_DRAG_ENTERED) {
        containsDraggable = true;
        }
    if (action == DragEvent.ACTION_DRAG_EXITED) {
        containsDraggable = false;
        }
    if (action == DragEvent.ACTION_DRAG_LOCATION) {
        // TODO
        }
    if (action == DragEvent.ACTION_DRAG_ENDED) {
        if (dropEventNotHandled(event)) {
            view.setVisibility(View.VISIBLE);
            }
        }
    if (action == DragEvent.ACTION_DROP && containsDraggable) {
        view.setVisibility(View.VISIBLE);
        releaseTrue();
        }
    return true;
    }
}
private boolean dropEventNotHandled(DragEvent event) {
    return !event.getResult();
    }
    public void releaseTrue() {
        score++;
        textScore.setText(String.valueOf(score));
    }
    public void releaseFalse() {
        score--;
        textScore.setText(String.valueOf(score));
    }
解决方案

since you are adding

.setOnDragListener(new DragListener());

for all three views

public boolean onDrag(View v, DragEvent event) {

will be called with above three views (ie. View v)

so you can have switch statement if you want to behave differently.

switch(v.getId()){
case R.id.blueArea:
//action for blue..
break
}

这篇关于使用拖放来确定身份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 15:05
查看更多