Android:setOnClickListener之后的setOnTouchListener

时间:2016-04-28 05:44:19

标签: android onclicklistener swipe ontouchlistener

我向上滑动并使用setOnTouchListener向下滑动以查看布局(了解如下图所示)

图片

enter image description here enter image description here

但是我想要向上滑动并在按下按钮时向下滑动布局而不是在OnTouchListener时。为此我在网上尝试了几乎所有的例子,但根据我的要求,我没有得到任何解决方案。所以,请点击按钮

帮我制作OnTouchListener事件

我的代码

活动

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我必须在点击按钮时打开布局并关闭布局(或向上滑动布局并向下滑动布局)

2 个答案:

答案 0 :(得分:0)

创建onTouchListenr的对象。

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

启用按钮单击

rlSwipeHolder.setOnTouchListener(ot);

禁用按钮clik

 rlSwipeHolder.setOnTouchListener(null);

答案 1 :(得分:0)

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

<强>答案

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

中的说明,按照动画进行布局上下

我对代码进行了微小的更改

<强>活动

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>