3

I have an Activity with one big ImageView in the center that I can drag onto another 4 smaller views. The small views are aligned to the bottom of the screen and they must be centered horizontally, that's why they have to be inside a ViewGroup (in this case a LinearLayout).

The problem is: when I try to drop the big image on any of the small ones, the OnDragListener is never triggered. Instead, if I set a listener to the LinearLayout, it is successfully triggered.

All relevant code:

activity_main.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/container">

        <ImageView
            android:id="@+id/image"
            android:layout_width="254dp"
            android:layout_height="254dp"
            android:layout_centerInParent="true" />

    </RelativeLayout>

    <LinearLayout
        android:id="@+id/container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/goal_1"
            style="@style/Goal"
            android:src="@drawable/goal_1"/>

        <ImageView
            android:id="@+id/goal_2"
            style="@style/Goal"
            android:src="@drawable/goal_2"/>

        <ImageView
            android:id="@+id/goal_3"
            style="@style/Goal"
            android:src="@drawable/hand_03"/>

        <ImageView
            android:id="@+id/goal_4"
            style="@style/Goal"
            android:src="@drawable/goal_4"/>

    </LinearLayout>

</RelativeLayout>

MainActivity.java

public class MainActivity extends BaseActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        View goal1 = findViewById(R.id.goal_1);
        View goal2 = findViewById(R.id.goal_2);
        View goal3 = findViewById(R.id.goal_3);
        View goal4 = findViewById(R.id.goal_4);

        goal1.setOnDragListener(new MyDragListener(1));
        goal2.setOnDragListener(new MyDragListener(2));
        goal3.setOnDragListener(new MyDragListener(3));
        goal4.setOnDragListener(new MyDragListener(4));

        ImageView image = (ImageView) findViewById(R.id.image);

        image.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    ClipData data = ClipData.newPlainText("", "");
                    View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
                    view.startDrag(data, shadowBuilder, null, 0);
                    return true;

                } else {
                    return false;
                }
            }
        });
    }

    protected class MyDragListener implements View.OnDragListener() {

        private final int index;

        public MyDragListener(int index) {
            this.index = index;
        }

        @Override
        public boolean onDrag(View v, DragEvent event) {
            switch (event.getAction()) {
                case DragEvent.ACTION_DRAG_ENTERED:
                    Log.d(TAG, "onDrag: entered " + index);
                    break;

                case DragEvent.ACTION_DRAG_EXITED:
                    Log.d(TAG, "onDrag: exited " + index);
                    break;

                case DragEvent.ACTION_DROP:
                    Log.d(TAG, "onDrag: drop " + index);
                    break;

                default:
                    Log.d(TAG, "onDrag: default " + index);
                    break;
            }
            return true;
        }
    }
}

If I remove the LinearLayout of course the event is triggered.

Is there any way to pass the listener to one of the ViewGroup children views?

josemigallas
  • 3,761
  • 1
  • 29
  • 68

0 Answers0