onTouchEvent не вызывается из пользовательской ViewGroup - PullRequest
0 голосов
/ 21 мая 2019

Я разрабатываю приложение компании, в котором у меня есть динамический фрагмент, который добавляется к FrameLayout, который занимает примерно 3/4 экрана. Я хочу удалить этот фрагмент движением вниз.

Я прочитал несколько статей об этом здесь , здесь и здесь :

В этих ссылках есть то, что я хочу. В логах я вижу, что onInterceptTouchEvent возвращает true, поэтому после этого мой OuterLayout должен начать получать onTouchEvent. Этого не происходит.

Вот OuterLayout

package com.pro.app.ui.devicebrowsing;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.pro.app.R;

public class OuterLayout extends RelativeLayout {

    private final static String TAG = OuterLayout.class.getSimpleName();
    private final double AUTO_OPEN_SPEED_LIMIT = 800.0;
    private boolean mIsOpen;
    private int mDraggingState = 0;
    private int mDraggingBorder;
    private int mVerticalRange;
    private ViewDragHelper mDragHelper;

    private View mTopTransparent;
    private LinearLayout mPlayContainer;

    public OuterLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mIsOpen = false;
    }

    public boolean isMoving() {
        return (mDraggingState == ViewDragHelper.STATE_DRAGGING || mDraggingState == ViewDragHelper.STATE_SETTLING);
    }

    public boolean isOpen() {
        return mIsOpen;
    }

    private void onStopDraggingToClosed() {
        // ToDo Notify fragment to close itself
    }

    private void onStartDragging() {

    }

    private boolean isTopPartTarget(MotionEvent event) {
        int[] topTransparentLocation = new int[2];
        mTopTransparent.getLocationOnScreen(topTransparentLocation);

        int[] playContainerLocation = new int[2];
        mPlayContainer.getLocationOnScreen(playContainerLocation);

        int lowerLimit = topTransparentLocation[1];
        int upperLimit = playContainerLocation[1];

        int y = (int) event.getRawY();
        boolean result = (y > lowerLimit && y < upperLimit);
        return result;
    }

    @Override
    protected void onFinishInflate() {
        mTopTransparent = findViewById(R.id.topTransparent);
        mPlayContainer = findViewById(R.id.playContainer);
        mDragHelper = ViewDragHelper.create(this, 1.0f, new DragHelperCallback());
        mIsOpen = false;
        super.onFinishInflate();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        mVerticalRange = h;
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (isTopPartTarget(ev) && mDragHelper.shouldInterceptTouchEvent(ev)) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (isTopPartTarget(event) || isMoving()) {
            mDragHelper.processTouchEvent(event);
            return true;
        } else {
            return super.onTouchEvent(event);
        }
    }

    @Override
    public void computeScroll() {
        if (mDragHelper.continueSettling(true)) {
            ViewCompat.postInvalidateOnAnimation(this);
        }
    }

    public class DragHelperCallback extends ViewDragHelper.Callback {

        @Override
        public void onViewDragStateChanged(int state) {
            if (state == mDraggingState) { // no change
                return;
            }
            if ((mDraggingState == ViewDragHelper.STATE_DRAGGING || mDraggingState == ViewDragHelper.STATE_SETTLING) && state == ViewDragHelper.STATE_IDLE) {
                // the view stopped from moving.

                if (mDraggingBorder == 0) {
                    onStopDraggingToClosed();
                } else if (mDraggingBorder == mVerticalRange) {
                    mIsOpen = true;
                }
            }
            if (state == ViewDragHelper.STATE_DRAGGING) {
                onStartDragging();
            }
            mDraggingState = state;
        }

        @Override
        public void onViewPositionChanged(@NonNull View changedView, int left, int top, int dx, int dy) {
            mDraggingBorder = top;
        }

        @Override
        public void onViewReleased(@NonNull View releasedChild, float xvel, float yvel) {
            final float rangeToCheck = mVerticalRange;
            if (mDraggingBorder == 0) {
                mIsOpen = false;
                return;
            }
            if (mDraggingBorder == rangeToCheck) {
                mIsOpen = true;
                return;
            }
            boolean settleToOpen = false;
            if (yvel > AUTO_OPEN_SPEED_LIMIT) { // speed has priority over position
                settleToOpen = true;
            } else if (yvel < -AUTO_OPEN_SPEED_LIMIT) {
                settleToOpen = false;
            } else if (mDraggingBorder > rangeToCheck / 2) {
                settleToOpen = true;
            } else if (mDraggingBorder < rangeToCheck / 2) {
                settleToOpen = false;
            }

            final int settleDestY = settleToOpen ? mVerticalRange : 0;

            if (mDragHelper.settleCapturedViewAt(0, settleDestY)) {
                ViewCompat.postInvalidateOnAnimation(OuterLayout.this);
            }
        }

        @Override
        public int getViewVerticalDragRange(@NonNull View child) {
            return mVerticalRange;
        }

        @Override public int clampViewPositionVertical (@NonNull View child,int top, int dy){
            final int topBound = getPaddingTop();
            final int bottomBound = mVerticalRange;
            int result = Math.min(Math.max(top, topBound), bottomBound);
            return result;
        }

        @Override public boolean tryCaptureView (View child,int pointerId){
            boolean result = (child.getId() == R.id.innerLayout);
            return result;
        }
    }
}

А вот файл макета:

<?xml version="1.0" encoding="utf-8"?>

<com.pro.app.ui.devicebrowsing.OuterLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@android:color/white"
   android:id="@+id/outerLayout">

    <RelativeLayout
        android:id="@+id/innerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true">

        <View
            android:id="@+id/topTransparent"
            android:layout_width="match_parent"
            android:layout_height="10dp"
            android:layout_alignParentTop="true"
            android:background="@color/transparent">
        </View>

        <ImageView
            android:id="@+id/gradient"
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:layout_below="@id/topTransparent"
            android:src="@drawable/horizontal_gradient"/>

        <View
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/gradient"
            android:layout_alignParentBottom="true"
            android:background="@color/cit_dark">
        </View>

        <RelativeLayout
            android:id="@+id/topBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="20dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="1dp"
                android:layout_centerVertical="true"
                android:layout_alignParentStart="true"
                android:layout_toStartOf="@+id/downArrow"
                android:src="@color/cit_white"/>
            <ImageView
                android:id="@+id/downArrow"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:padding="5dp"
                android:background="@color/transparent"
                android:src="@drawable/cit_down_arrow"/>
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="1dp"
                android:layout_centerVertical="true"
                android:layout_alignParentEnd="true"
                android:layout_toEndOf="@+id/downArrow"
                android:src="@color/cit_white"/>

        </RelativeLayout>

        <LinearLayout
            android:id="@+id/playContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/topBar"
            android:layout_marginStart="10dp"
            android:layout_marginEnd="10dp"
            android:layout_marginBottom="10dp"
            android:animateLayoutChanges="true"
            android:background="@color/transparent"
            android:orientation="vertical">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="4"
                android:layout_marginStart="60dp"
                android:layout_marginEnd="60dp">

                <ImageView
                    android:id="@+id/album_art"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_centerInParent="true"
                    android:adjustViewBounds="true"
                    android:scaleType="fitCenter"
                    android:src="@drawable/ic_album_art" />

                <ProgressBar
                    android:id="@+id/progress_play_view"
                    style="?android:attr/progressBarStyleLarge"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:visibility="visible" />

            </RelativeLayout>

            <LinearLayout
                android:id="@+id/meta_box"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="2"
                android:layout_marginStart="25dp"
                android:layout_marginEnd="25dp"
                android:gravity="center"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/title_info"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:ellipsize="marquee"
                    android:singleLine="true"
                    android:textColor="@color/cit_white"
                    android:textSize="14sp"
                    android:textStyle="bold"
                    android:marqueeRepeatLimit="marquee_forever"/>

                <TextView
                    android:id="@+id/artist_info"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:ellipsize="marquee"
                    android:marqueeRepeatLimit="marquee_forever"
                    android:singleLine="true"
                    android:textColor="@color/cit_white"
                    android:textSize="14sp"/>

                <TextView
                    android:id="@+id/album_info"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:ellipsize="marquee"
                    android:marqueeRepeatLimit="marquee_forever"
                    android:singleLine="true"
                    android:textColor="@color/cit_white"
                    android:textSize="14sp"/>
            </LinearLayout>


            <LinearLayout
                android:id="@+id/text_time_container"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:layout_marginStart="25dp"
                android:layout_marginEnd="25dp"
                android:gravity="center"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/text_time"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/min_sec"
                    android:textSize="12sp"
                    android:textColor="@color/cit_white" />

                <ProgressBar
                    android:id="@id/time_bar"
                    style="?android:attr/progressBarStyleHorizontal"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="3dp"
                    android:layout_marginEnd="3dp"
                    android:layout_marginTop="1dp"
                    android:background="@color/transparent"
                    android:minHeight="8dp"
                    android:visibility="gone" />

                <TextView
                    android:id="@+id/text_time_full"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/min_sec"
                    android:textSize="12sp"
                    android:textColor="@color/cit_white"
                    android:visibility="gone" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/button_container"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:layout_marginStart="90dp"
                android:layout_marginEnd="90dp"
                android:gravity="center"
                android:orientation="horizontal">

                <ImageButton
                    android:id="@+id/previous_rewind_button"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="@null"
                    android:src="@drawable/cit_player_prev" />

                <ImageButton
                    android:id="@+id/play_pause_button"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="@null"
                    android:src="@drawable/cit_selector_play"
                    android:visibility="gone" />

                <ImageButton
                    android:id="@+id/next_forward_button"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="@null"
                    android:src="@drawable/cit_player_next" />

            </LinearLayout>

            <LinearLayout
                android:id="@+id/volume_layout"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="horizontal">

                <ImageButton
                    android:id="@+id/mute_button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@null"
                    android:src="@drawable/selector_mute" />

                <SeekBar
                    android:id="@+id/volume_bar"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="2dp"
                    android:layout_weight="1" />

                <TextView
                    android:id="@+id/volume_level_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="18sp"
                    android:textColor="@color/cit_white"/>
            </LinearLayout>

        </LinearLayout>
    </RelativeLayout>
</com.pro.app.ui.devicebrowsing.OuterLayout>

Что может быть не так? Какой-то ребенок / родитель крал эти события?

Спасибо

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...