简体   繁体   中英

How can i use two buttons in setOnDragListener?

I'm new in Android Studio, i know some basics about it and java. I'm not an expert. I'm doing a drag and drop game and i want to drag and drop two items, the code is working, but i need to check that a button is on a specific linear layout but both buttons are validated.

This is for Android Studio

package com.example.lalo.menuhamburguesa;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.content.ClipData;

import android.view.DragEvent;
import android.view.MotionEvent;



public class Fragment3 extends Fragment {
    Button dragbutton;
    Button dragbutton1;
    LinearLayout dropbutton;
    LinearLayout dropbutton1;
    TextView textView, sucess;
    TextView textView1, sucess1;
    int total, fail = 0;
    int total1, fail1 = 0;

    @Override
    /*public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_fragment3, container, false);
    }*/

    /*View v = inflater.inflate(R.layout.fragment_fragment2, container, false);
    pdfViewer = (PDFView) v.findViewById(R.id.pdfView);
        pdfViewer.fromAsset("Ejercicios.pdf").pages(0).load();*/


    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
/*
setContentView(R.layout.activity_main);

With this:

return new SampleView(R.layout.activity_main);
*/
        View v = inflater.inflate(R.layout.fragment_fragment3, container, false);


        dragbutton = (Button) v.findViewById(R.id.one);
        dropbutton = (LinearLayout) v.findViewById(R.id.drag_it_linear);

        dragbutton1 = (Button) v.findViewById(R.id.one1);
        dropbutton1 = (LinearLayout) v.findViewById(R.id.drag_it_linear1);


        textView = (TextView) v.findViewById(R.id.Total);
        sucess = (TextView) v.findViewById(R.id.Success);

        textView1 = (TextView) v.findViewById(R.id.Total1);
        sucess1 = (TextView) v.findViewById(R.id.Success1);


        //================================================LISTENER========================


        dropbutton.setOnDragListener(new View.OnDragListener() {
            @Override
            public boolean onDrag(View v, DragEvent event) {
// TODO Auto-generated method stub
                final int action = event.getAction();
                switch (action) {
                    case DragEvent.ACTION_DRAG_STARTED:
// Executed after startDrag() is called.
                        break;
                    case DragEvent.ACTION_DRAG_EXITED:
                        break;
                    case DragEvent.ACTION_DRAG_ENTERED:
                        // Executed after the Drag Shadow enters the drop area
                        break;
                    case DragEvent.ACTION_DROP: {
                        //Executed when user drops the data
                        fail = fail + 1;
                        return (true);
                    }
                    case DragEvent.ACTION_DRAG_ENDED: {
                        total = total + 1;
                        int value = total - fail;
                        sucess.setText("Sucessful Drops:" + value);
                        textView.setText("Total Drops: " + total);
                        return (true);
                    }
                    default:
                        break;
                }
                return true;
            }
        });
        dragbutton.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent arg1) {
// TODO Auto-generated method stub
                ClipData data = ClipData.newPlainText("", "");
                View.DragShadowBuilder shadow = new View.DragShadowBuilder(dragbutton);
                v.startDrag(data, shadow, null, 0);
                return false;
            }
        });



        //================================================LISTENER========================

        //================================================LISTENER========================


        dropbutton1.setOnDragListener(new View.OnDragListener() {
            @Override
            public boolean onDrag(View v, DragEvent event) {
// TODO Auto-generated method stub
                final int action = event.getAction();

                //CASE


                switch (action) {
                    case DragEvent.ACTION_DRAG_STARTED:
// Executed after startDrag() is called.
                        break;
                    case DragEvent.ACTION_DRAG_EXITED:
                        break;
                    case DragEvent.ACTION_DRAG_ENTERED:
                        // Executed after the Drag Shadow enters the drop area
                        break;
                    case DragEvent.ACTION_DROP: {
                        //Executed when user drops the data
                        fail1 = fail1 + 1;
                        return (true);
                    }
                    case DragEvent.ACTION_DRAG_ENDED: {
                        total1 = total1 + 1;
                        int value1 = total1 - fail1;
                        sucess1.setText("Sucessful Drops:" + value1);
                        textView1.setText("Total Drops: " + total1);
                        return (true);
                    }
                    default:
                        break;
                }

                //CASE

                return true;
            }
        });
        dragbutton1.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent arg1) {
// TODO Auto-generated method stub
                ClipData data = ClipData.newPlainText("", "");
                View.DragShadowBuilder shadow = new View.DragShadowBuilder(dragbutton1);
                v.startDrag(data, shadow, null, 0);
                return false;
            }
        });



        //================================================LISTENER========================


        return v;
    }
}

I expect only one validation for each setOnDragListener but i get two

both your drag buttons are returning false though they consume the onTouch action. See what happens if you make them return true.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM