拖动监听器多个IF语句 [英] On Drag Listener multiple IF statements

查看:212
本文介绍了拖动监听器多个IF语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个信件拖拽下拉游戏(有点像猜猜品牌),你有一个图像,你必须拖动字母到布局形成正确的答案。

I'm making a Letter Drag n Drop game (kinda like Guess the Brand) where you have an image and you have to drag the letters into layouts to form the correct answer.

在顶部,我有四个布局(layoutAnsw A到D),在底部我有四个按钮
(btnTop A到D)
我有OnTouchListener和OnDragListener工作正常,除了一个单一的东西。

At the top, I have Four layouts (layoutAnsw A to D), and in the bottom I have Four buttons (btnTop A to D) I have the OnTouchListener and OnDragListener working fine, except one single thing.

当我有不止一个相似的字符(字母)时会发生什么?
例如在此图像中:

What happens when I have more than one similar character (letter)? For example in this image:

正如你所看到的,我需要A字母,需要拖动,我想做,无论你先放在哪一个。在我的代码中,我设法得到这样的东西:

As you can see, I have to "A" letters that need to be dragged, and I want to do it regardless of which one you put first. In my code I managed to get something like this:

如果第一个A在第一个空间,那么第二个A进入第二个空间

"If the First A is in the First space, then Second A goes to Second Space"

我正在尝试以其他方式编写包括之前的语句。
我的代码到目前为止让我们在任何空间放置任何A,包括在同一个空格中的2个字母。相当无用。

I'm trying to code the other way around including that previous statement. My code so far let's you put any "A" in any space, including 2 letters in the same space. Pretty useless.

我的代码到目前为止

public class OneQuestionA extends Fragment implements OnTouchListener,
    OnDragListener {

protected static final String LOGCAT = null;
int numDragged = 0;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View rootView = inflater.inflate(R.layout.questions_fragment5,
            container, false);

    btnTopA.setOnTouchListener(this); // Letter A
    btnTopB.setOnTouchListener(this); // Second Letter A
    btnTopC.setOnTouchListener(this); 
    btnTopD.setOnTouchListener(this);

    rootView.findViewById(R.id.layoutAnswA).setOnDragListener(this); // Layout 1
    rootView.findViewById(R.id.layoutAnswB).setOnDragListener(this); // Layout 2
    rootView.findViewById(R.id.layoutAnswC).setOnDragListener(this); // Layout 3
    rootView.findViewById(R.id.layoutAnswD).setOnDragListener(this); // Layout 4
return rootView;
}

我的onTouch实现:

My onTouch implementation:

 @Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    // TODO Auto-generated method stub
    if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
        DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
        view.startDrag(null, shadowBuilder, view, 0);
        view.setVisibility(View.INVISIBLE);
        return true;
    }

    if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
        view.setVisibility(View.VISIBLE);
        return true;
    } else {
        return false;
    }
}

我的onDrag实现:

My onDrag implementation:

@Override
public boolean onDrag(View v, DragEvent e) {

    int action = e.getAction();
    View view = (View) e.getLocalState();

    switch (action) {
    case DragEvent.ACTION_DRAG_STARTED:
        return true;
    case DragEvent.ACTION_DRAG_ENTERED:
        return false;
    case DragEvent.ACTION_DRAG_LOCATION:
        return false;
    case DragEvent.ACTION_DRAG_EXITED:
        return false;
    case DragEvent.ACTION_DROP:


        if (view.getId() == R.id.btnTopA && v.getId() == R.id.layoutAnswA) {
            ViewGroup owner = (ViewGroup) view.getParent();
            owner.removeView(view);
            LinearLayout container = (LinearLayout) v;
            container.addView(view);
            view.setVisibility(View.VISIBLE);
            view.setOnTouchListener(null);
            view.setOnDragListener(null);
            numDragged++;
        } else if (view.getId() == R.id.btnTopB && v.getId() == R.id.layoutAnswB) {
                ViewGroup owner = (ViewGroup) view.getParent();
            owner.removeView(view);
            LinearLayout container = (LinearLayout) v;
            container.addView(view);
            view.setVisibility(View.VISIBLE);
            view.setOnTouchListener(null);
            view.setOnDragListener(null);
            numDragged++;
            }
        } 





        if (numDragged >= 4) {
            numDragged = 0;

            Toast.makeText(getActivity(),
                    "All buttons in the Right place", Toast.LENGTH_SHORT)
                    .show();


        }

    case DragEvent.ACTION_DRAG_ENDED:
        if (dropEventNotHandled(e)) {
            view.setVisibility(View.VISIBLE);
        }
    }
    return false;
}

private boolean dropEventNotHandled(DragEvent e) {
    // TODO Auto-generated method stub
    return !e.getResult();
}


推荐答案

经过很多阅读,我遇到了非常简单的一行:

After a lot of reading, I came across the incredibly simple line:

getChildCount() 

使用它作为条件,任何时候已经有另一个视图,它只会返回false

Use that as a condition, anytime there's already another view in place, it will just return false

int i = container.getChildCount();
if (i < 1) {
// Do Something
} else if (i == 1) {
return false;
}

这篇关于拖动监听器多个IF语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆