如何将视图放到ViewGroup中的另一个视图?

时间:2016-08-24 18:54:06

标签: android android-layout android-viewgroup

我有一个Activity,中间有一个大的ImageView我可以拖到另外4个较小的视图上。小视图与屏幕底部对齐,并且它们必须水平居中,这就是为什么它们必须位于ViewGroup内(在这种情况下为LinearLayout)。

问题是:当我尝试将大图像放在任何小图像上时,OnDragListener永远不会被触发。相反,如果我将监听器设置为LinearLayout,则会成功触发。

所有相关代码:

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;
        }
    }
}

如果我删除LinearLayout当然会触发事件。

有没有办法将监听器传递给其中一个ViewGroup子视图?

0 个答案:

没有答案