gpt4 book ai didi

java - 组合 OnTouchListener 和 OnClickListener

转载 作者:行者123 更新时间:2023-12-01 21:26:38 29 4
gpt4 key购买 nike

尽管我检查了一些线程,但在 fragment 内使用 OnTouchListener 和 OnClickListener 时遇到问题。

自定义类:

import android.content.Context;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

public class Gestures implements View.OnTouchListener
{
private GestureDetector gestureDetector = null;
public Gestures()
{
gestureDetector = new GestureDetector(new GestureListener());
}
public boolean onTouch(View view, MotionEvent motionEvent)
{
boolean res = gestureDetector.onTouchEvent(motionEvent);
return res;
}
private final class GestureListener extends GestureDetector.SimpleOnGestureListener
{
private static final int SWIPE_THRESHOLD = 50;
private static final int SWIPE_VELOCITY_THRESHOLD = 50;
@Override
public void onLongPress(MotionEvent motionEvent)
{
onLongClick();
super.onLongPress(motionEvent);
}
@Override
public boolean onDown(MotionEvent motionEvent)
{
return true;
}
@Override
public boolean onFling(MotionEvent motionEvent1, MotionEvent motionEvent2, float velocityX, float velocityY)
{
boolean result = false;
try
{
float differenceX = motionEvent2.getX() - motionEvent1.getX();
float differenceY = motionEvent2.getY() - motionEvent1.getY();

if (Math.abs(differenceX) < Math.abs(differenceY))
{
if (Math.abs(differenceY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD)
{
if (differenceY > 0)
{
result = onSwipeBottom();
}
else
{
result = onSwipeTop();
}
}
else
{
result = nothing();
}
}
}
catch (Exception exception)
{
exception.printStackTrace();
}
return result;
}
}
public boolean nothing()
{
return false;
}
public boolean onSwipeTop()
{
return false;
}
public boolean onSwipeBottom()
{
return false;
}
public boolean onLongClick()
{
return false;
}
}

fragment :

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;

public class Fragment1 extends Fragment implements View.OnClickListener, View.OnTouchListener
{
ConstraintLayout constraintLayoutOne, constraintLayoutTwo;
@Override public void onResume()
{
super.onResume();
constraintLayoutOne = getActivity().findViewById(R.id.constraint_layout_one); // Link variable to ID
constraintLayoutTwo = getActivity().findViewById(R.id.constraint_layout_two); // Link variable to ID
getActivity().findViewById(R.id.constraint_layout_one).setOnClickListener(this);
getActivity().findViewById(R.id.constraint_layout_two).setOnClickListener(this);
getActivity().findViewById(R.id.constraint_layout_one).setOnTouchListener(this);
getActivity().findViewById(R.id.constraint_layout_two).setOnTouchListener(this);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
{
View fragment1 = inflater.inflate(R.layout.fragment1, container, false); // Link view and layout
return fragment1;
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent)
{
switch (view.getId())
{
case R.id.constraint_layout_one:
case R.id.constraint_layout_two:
view.setOnTouchListener(new Gestures()
{
public boolean onSwipeTop()
{
showToast("swiped up");
return false;
}
public boolean onSwipeBottom()
{
showToast("swiped down");
return false;
}
});
}
return false;
}
@Override
public void onClick(View view)
{
if (view.getId() == R.id.constraint_layout_one)
{
showToast("button one clicked");
}
if (view.getId() == R.id.constraint_layout_two)
{
showToast("button two clicked");
}
}
public void showToast(String text)
{
Toast toast = Toast.makeText(getContext(), text, Toast.LENGTH_SHORT); // Generate toast message
toast.setGravity(Gravity.CENTER| Gravity.CENTER, 0, -1); // Change y-Position of toast a bit
toast.show(); // Show toast for 4 seconds
}
}

两个问题:

1)当我启动应用程序时,滑动不起作用。当我滑动时什么也没有发生。我必须执行两次滑动才能使其正常工作。之后,我可以随心所欲地滑动。

2)单击仅有效一次。第一次单击后,不再识别进一步的单击(对于单击的 View )当我滑动两次时也会发生同样的情况 - 不再点击。

任何帮助将不胜感激!

最佳答案

我尝试了完全不同的东西。 -> onInterceptTouchEvent我创建了一个名为 RootView 的帮助器类:

import android.content.Context; 
import android.util.AttributeSet;
import android.view.MotionEvent;
import androidx.constraintlayout.widget.ConstraintLayout;

public class RootLayout extends ConstraintLayout
{
public RootLayout(Context context)
{
super(context);
}
public RootLayout(Context context, AttributeSet attributeSet)
{
super(context, attributeSet);
}
public RootLayout(Context context, AttributeSet attributeSet, int defStyleAttribute)
{
super(context, attributeSet, defStyleAttribute);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent motionEvent)
{
return true;
}
}

在我的 xml 文件中,我将 ConstraintLayout 更改为 RootLayout:

<com.example.testSwipeApp.RootLayout 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"
tools:layout_editor_absoluteY="81dp">

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraint_layout_two"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#E91E63"
android:clickable="true"
android:focusable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.4"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.3"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="1" />

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraint_layout_one"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#3F51B5"
android:clickable="false"
android:focusable="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.5"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="1" />

<TextView
android:id="@+id/text_view_header"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#000000"
android:gravity="center_horizontal"
android:paddingTop="2dp"
android:textColor="#ffffff"
android:textSize="15sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.10"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />

</com.example.testSwipeApp.RootLayout>

fragment1.java 看起来像这样:

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;

public class Fragment1 extends Fragment implements View.OnClickListener
{
ConstraintLayout constraintLayoutOne, constraintLayoutTwo;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
{
View fragment1 = inflater.inflate(R.layout.fragment1, container, false); // Link view and layout
constraintLayoutOne = fragment1.findViewById(R.id.constraint_layout_one); // Link variable to ID
constraintLayoutTwo = fragment1.findViewById(R.id.constraint_layout_two); // Link variable to ID
fragment1.findViewById(R.id.constraint_layout_one).setOnClickListener(this);
fragment1.findViewById(R.id.constraint_layout_two).setOnClickListener(this);

fragment1.setOnTouchListener(new Gestures()
{
public boolean onSwipeTop()
{
Toast.makeText(getContext(), "Swiped up", Toast.LENGTH_SHORT).show();
return false;
}
public boolean onSwipeBottom()
{
Toast.makeText(getContext(), "Swiped down", Toast.LENGTH_SHORT).show();
return false;
}
});
return fragment1;
}
@Override
public void onClick(View view)
{
switch (view.getId())
{
case R.id.constraint_layout_one:
case R.id.constraint_layout_two:
showToast("clicked");
break;
}
}
public void showToast(String text)
{
Toast toast = Toast.makeText(getContext(), text, Toast.LENGTH_SHORT); // Generate toast message
toast.setGravity(Gravity.CENTER| Gravity.CENTER, 0, -1); // Change y-Position of toast a bit
toast.show(); // Show toast for 4 seconds
}
}

问题:不再接受点击。尽管这个版本效果较差,但我更喜欢这种方式而不是旧的。

任何帮助将不胜感激!

关于java - 组合 OnTouchListener 和 OnClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58828758/

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