Android: 在 setOnClickListener 之后设置 OnTouchListener

Android: setOnTouchListener after setOnClickListener

我使用 setOnTouchListener 向上滑动和向下滑动布局(为了理解请参见下图)

图片

但我想在单击按钮时向上滑动和向下滑动布局,而不是在 OnTouchListener 时。为此,我尝试了几乎所有在线示例,但没有根据我的要求获得任何解决方案。所以,请帮我在点击按钮时制作 OnTouchListener 事件

我的代码

Activity

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.RelativeLayout;

public class SwipeUpActivity extends Activity {

    RelativeLayout rlSwipeHolder, rlSwipe1, rlSwipe2;
    private float startY;

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

        rlSwipeHolder = (RelativeLayout) findViewById(R.id.rl_swipe_up_holder);
        rlSwipe1 = (RelativeLayout) findViewById(R.id.rl_swipe_up_1);
        rlSwipe2 = (RelativeLayout) findViewById(R.id.rl_swipe_up_2);
        rlSwipe2.setVisibility(View.GONE);



        rlSwipeHolder.setOnTouchListener(new OnTouchListener() {

            @SuppressLint("ClickableViewAccessibility")
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    startY = event.getY();
                    break;
                case MotionEvent.ACTION_UP: {
                    float endY = event.getY();

                    if (endY < startY) {
                        System.out.println("Move UP");
                        rlSwipeHolder.setVisibility(View.VISIBLE);
                        rlSwipe1.setVisibility(View.VISIBLE);
                        rlSwipe2.setVisibility(View.VISIBLE);
                    } else {
                        rlSwipeHolder.setVisibility(View.VISIBLE);
                        rlSwipe1.setVisibility(View.VISIBLE);
                        rlSwipe2.setVisibility(View.GONE);
                    }
                }

                }
                return true;
            }
        });
    }
}

布局

<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"
    android:background="#FFFFFF"
    tools:context="com.app.swipeup.SwipeUpActivity" >

    <RelativeLayout
        android:id="@+id/rl_swipe_up_holder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#0000FF"
        android:padding="5dp" >

        <RelativeLayout
            android:id="@+id/rl_swipe_up_1"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:background="#585858" >
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/rl_swipe_up_2"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_below="@+id/rl_swipe_up_1"
            android:background="#FE2E2E" >
        </RelativeLayout>
    </RelativeLayout>

</RelativeLayout>

编辑

像这样video单击按钮时我必须打开布局并关闭布局(或向上滑动布局并向下滑动布局)

创建您的 onTouchListenr 对象。

 OnTouchListener ot = new OnTouchListener(){-------- YOUR CODE ---}

点击启用按钮

rlSwipeHolder.setOnTouchListener(ot);

点击禁用按钮

 rlSwipeHolder.setOnTouchListener(null);

这不是解决此问题的确切方法,但我在这里发帖,因为它可能对其他人有所帮助。

回答

我在 setOnClickListener 之后看到了很多针对 setOnTouchListener 的解决方案,但我没有得到。所以,我按照

中讨论的动画上下布局

并且我对代码进行了微小的更改,如

Activity

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.RelativeLayout;


public class SwipeUpActivity extends Activity {

    RelativeLayout mRelativeLayout;
    RelativeLayout mRelativeLayoutHeader;
    ValueAnimator mAnimator;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_swipe_up);

        mRelativeLayout = (RelativeLayout) findViewById(R.id.expandable);
        // mLinearLayout.setVisibility(View.GONE);
        mRelativeLayoutHeader = (RelativeLayout) findViewById(R.id.header);

        // Add onPreDrawListener
        mRelativeLayout.getViewTreeObserver().addOnPreDrawListener(
                new ViewTreeObserver.OnPreDrawListener() {

                    @Override
                    public boolean onPreDraw() {
                        mRelativeLayout.getViewTreeObserver().removeOnPreDrawListener(this);
                        mRelativeLayout.setVisibility(View.GONE);

                        final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                        final int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                        mRelativeLayout.measure(widthSpec, heightSpec);
                        mAnimator = slideAnimator(0, mRelativeLayout.getMeasuredHeight());
                        return true;
                    }
                });

        mRelativeLayoutHeader.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mRelativeLayout.getVisibility() == View.GONE) {
                    expand();
                } else {
                    collapse();
                }
            }
        });
    }

    private void expand() {
        // set Visible
        mRelativeLayout.setVisibility(View.VISIBLE);

        mAnimator.start();
    }

    private void collapse() {
        int finalHeight = mRelativeLayout.getHeight();

        ValueAnimator mAnimator = slideAnimator(finalHeight, 0);

        mAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationEnd(Animator animator) {
                // Height=0, but it set visibility to GONE
                mRelativeLayout.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationStart(Animator animator) {
            }

            @Override
            public void onAnimationCancel(Animator animator) {
            }

            @Override
            public void onAnimationRepeat(Animator animator) {
            }
        });
        mAnimator.start();
    }

    private ValueAnimator slideAnimator(int start, int end) {

        ValueAnimator animator = ValueAnimator.ofInt(start, end);

        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                // Update Height
                int value = (Integer) valueAnimator.getAnimatedValue();

                ViewGroup.LayoutParams layoutParams = mRelativeLayout.getLayoutParams();
                layoutParams.height = value;
                mRelativeLayout.setLayoutParams(layoutParams);
            }
        });
        return animator;
    }
}

布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">

        <RelativeLayout
            android:id="@+id/header"
            android:layout_width="match_parent"
            android:layout_height="64dp"
            android:background="#FCF">
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/expandable"
            android:layout_width="match_parent"
            android:layout_height="64dp"
            android:layout_below="@+id/header"
            android:background="#FFF">


        </RelativeLayout>

    </RelativeLayout>
</RelativeLayout>