gpt4 book ai didi

java - 处理 onClick 和双击

转载 作者:行者123 更新时间:2023-12-01 19:32:35 25 4
gpt4 key购买 nike

我有一个类似于 Whatsapp 的消息应用程序,其中聊天 Activity 中有消息视频和图像。现在我想对此实现类似双击的功能,但问题是我还附加了一个onclicklistner到我的图像和视频帧,它打开了显示图像的全屏 Activity 全屏或全屏播放视频。因此,双击时,它不会点赞图片或视频,而是以全屏模式打开视频或图像。因为在消息中我没有附加双击监听器,所以工作正常。关于如何在视频或图像的情况下双击类似工作的任何想法。我对使用 Java 进行 Android 开发相当陌生。因此,我们非常感谢任何帮助。

最佳答案

HI, I think the approach to take depends on the future of your app, using LongClick is kinda good but what if you want to achieve a zoom like onLongClick?

What I did below is that I created a subclass that extends GestureDetector.SimpleOnGestureListener and then in my GestureTest class, I an ontouchListener and return the gesture instance created before, finally I parse the listener to the button that has to perform the clicks, it can be any View object. Finally, I used the TextView I created to display/monitor my actions.

GestureDetector 如下所示:

public class GestureTest extends AppCompatActivity{

private Button btn;
private TextView actions;
private GestureDetector mDetector;

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

btn = findViewById(R.id.btn);
actions = findViewById(R.id.action);

mDetector = new GestureDetector(this, new SampleGestureListener());
View.OnTouchListener touchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return mDetector.onTouchEvent(event);
}
};

btn.setOnTouchListener(touchListener);
}

public class SampleGestureListener extends GestureDetector.SimpleOnGestureListener{

@Override
public boolean onDown(MotionEvent event) {
Log.e("EVENT","BUTTON PRESSED DOWN, NOT YET RELEASED WILL TRIGGER ONLONG-PRESS");

// don't return false here or else none of the other
// gestures will work
return true;
}

@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
Log.e("EVENT", "SINGLE CLICK EVENT");
actions.setText("THIS CLICK IS FOR IMAGE OR VIDEO TO OPEN ?");
return true;
}

@Override
public void onLongPress(MotionEvent e) {
Log.e("EVENT", "LONG PRESSED");
actions.setText("THIS IS ZOOM LIKE NOT DOUBLE CLICK!!!");
}

@Override
public boolean onDoubleTap(MotionEvent e) {
Log.e("EVENT", "DOUBLE CLICKED HAPPENED");
actions.setText("THIS CLICK IS TO LIKE IMAGE OR VIDEO ?");
return true;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
Log.e("EVENT", "SCROLLING");
actions.setText("YOU\" Scrolling on many images...");
return true;
}

@Override
public boolean onFling(MotionEvent event1, MotionEvent event2,
float velocityX, float velocityY) {
Log.e("EVENT", "FLINGED");
actions.setText("THIS IS TINDER FLING LIKE ?");
return true;
}
}
}

AND BELOW IS MY activity_gesture_test.xml FOR TESTING:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/btn"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="match_parent"
android:layout_height="100dp"/>

<TextView
android:id="@+id/action"
android:textSize="20dp"
android:textColor="@color/black"
app:layout_constraintTop_toBottomOf="@+id/btn"
android:layout_width="match_parent"
android:layout_height="100dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

GoodLuck Coding.

关于java - 处理 onClick 和双击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59124209/

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