On Drag Listener多个IF语句

时间:2014-02-05 01:38:49

标签: android android-fragments drag-and-drop drag ontouchlistener

我正在制作一个Letter Drag n Drop游戏(有点像Guess the Brand),你有一个图像,你必须将字母拖到布局中以形成正确的答案。

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

当我有多个相似字符(字母)时会发生什么? 例如,在此图像中: enter image description here

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

“如果第一个A位于第一个空间,则第二个A进入第二个空间”

我正在尝试以相反的方式编写代码,包括之前的语句。 到目前为止,我的代码让你在任何空间放置任何“A”,包括同一空间中的2个字母。很没用。

到目前为止我的代码

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实施:

 @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实现:

@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();
}

1 个答案:

答案 0 :(得分:1)

经过大量阅读后,我遇到了非常简单的界限:

getChildCount() 

使用它作为一个条件,只要有另一个视图到位,它就会返回false

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