即使我检查了一些线程,在片段中同时使用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次滑动才能使其正常工作。
之后,我可以随意滑动。
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
}
}
问题:
点击不再被接受。
即使此版本的效果较差,我还是喜欢这种方式,而不是旧的方式。
任何帮助将不胜感激!