Android - onTouchListener不适用于子LinearLayout

时间:2015-09-06 17:03:10

标签: java android android-layout ontouchlistener

我正试图向上/向下滑动手势。问题不在于如何实现向上/向下滑动捕获。我无法抓住onTouch电话。 在Fragment布局中,有一个已设置onTouchListener的LinearLayout。但不知何故,它无法正常工作 - 无法进行onTouch通话。

这就是LinearLayout:

    <LinearLayout
        android:id="@+id/llContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/btn_back"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/transparent" />

        <Button
            android:id="@+id/btn_forward"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/transparent" />

    </LinearLayout>

这是片段布局的组件树的方式: enter image description here

在Fragment的onCreateView中,我将onTouchListener设置为该LinearLayout:

    LinearLayout llContent = (LinearLayout) view.findViewById(R.id.llContent);
    llContent.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            new MyLog("Touch");
            return true;
        }

    });

我想,这是因为子按钮和clickListeners。所以我禁用了它们:

//        view.findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                mCurrentIndex = (mCurrentIndex > 0) ? mCurrentIndex - 1 : 0;
//                update();
//            }
//        });
//        view.findViewById(R.id.btn_forward).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                mCurrentIndex = (mCurrentIndex < mPagination.size() - 1) ? mCurrentIndex + 1 : mPagination.size() - 1;
//                update();
//            }
//        });

在StackOverflow上,有很多关于此的问题,并且有解决方案。

尝试1: 这个answer表示我应该从onTouch方法返回true。我的onTouch方法已经返回true。

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        new MyLog("Touch");
        return true;
    }

尝试2: 这个答案说我应该覆盖onTouchListener的onTouchEvent方法。但是当我实现onTouchListener接口时,我无法覆盖onTouchEvent。 Android Studio的intellisense无法找到。当我手动编写时,Android Studio将onTouchEvent方法显示为错误。 错误:方法不会覆盖其超类中的方法。同样的答案也是here

问题:如何获取儿童LinearLayout的onTouch调用?

这是整个片段布局的XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FEFFFF">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="41dp"
            android:background="#EFF0F1"
            android:scrollbars="none">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:gravity="center_vertical"
                android:orientation="horizontal"
                android:paddingLeft="57dp"
                android:paddingRight="57dp">

                <TextView
                    android:id="@+id/tvTitle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="#2E2F30"
                    android:textSize="17sp" />

                <TextView
                    android:id="@+id/tvAuthor"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:textColor="#5E5F60"
                    android:textSize="17dp" />

            </LinearLayout>

        </HorizontalScrollView>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:paddingBottom="8dp"
                android:paddingLeft="25dp"
                android:paddingRight="25dp"
                android:paddingTop="8dp">

                <TextView
                    android:id="@+id/tv"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:textColor="#1C1D1D"
                    android:textSize="18sp" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="24dp"
                    android:orientation="vertical">

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="#050505" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="8dp"
                        android:orientation="horizontal">

                        <TextView
                            android:id="@+id/tvPercent"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginRight="10dp"
                            android:textSize="15sp" />

                        <TextView
                            android:id="@+id/tvProgress"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textColor="#444546"
                            android:textSize="15sp" />

                    </LinearLayout>

                </LinearLayout>

            </LinearLayout>

            <LinearLayout
                android:id="@+id/llContent"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <Button
                    android:id="@+id/btn_back"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="@android:color/transparent" />

                <Button
                    android:id="@+id/btn_forward"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="@android:color/transparent" />

            </LinearLayout>


        </RelativeLayout>

    </LinearLayout>

</RelativeLayout>

1 个答案:

答案 0 :(得分:-1)

试试这个:

 LinearLayout llContent = (LinearLayout) view.findViewById(R.id.llContent);
        llContent.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.d(TAG, "onTouch");
                return true;
            }

        });

        Button ff = (Button)view.findViewById(R.id.btn_forward);
        ff.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                Log.i(TAG, "onTouch");
                return false;

            }
        });

        ff.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i(TAG, "onClick");
            }
        });

btn_forward高于llContent,因此您需要在onTouch()btn_forward)中返回false。然后将从底部控件(llContent)调用该事件。

在按钮上设置android:clickable="false"android:focusable="false"