gpt4 book ai didi

Android:setOnClickListener 之后的 setOnTouchListener

转载 作者:太空狗 更新时间:2023-10-29 14:49:06 28 4
gpt4 key购买 nike

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

图片

enter image description here enter image description here

但我想在单击按钮时向上滑动和向下滑动布局,而不是在 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);

关于Android:setOnClickListener 之后的 setOnTouchListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36906191/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com