Combining OnTouchListener and OnClickListener



即使我检查了一些线程,在片段中同时使用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;
}
}

片段:

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(点击只起作用一次。第一次单击后,不再识别其他单击(对于单击的视图(当我刷两次时也会发生同样的情况——不再点击了。

任何帮助都将不胜感激!

我尝试了一些完全不同的东西。->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
}
}

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

任何帮助都将不胜感激!

最新更新