我正在制作一个计分板类,该类用于控制布局,使用户上下滑动以增加和减少分数;然后长按屏幕以重置分数。此时,通过创建一个单独的OnSwipeTouchListener类,然后在要触摸的元素上调用其内部的方法,可以使OnTouchListener正常工作。但是,再次运行该程序后,我的LongClickListener已停止响应。我最好的猜测是TouchListener会自动覆盖其他所有内容,但我不确定。我将在下面链接相关的类,这样任何人都可以准确地看到我的实现。任何人都可以协助解决这个问题或推荐替代方案吗?
记分板
package com.example.seth.sidespin;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v4.view.GestureDetectorCompat;
public class Scoreboard extends Fragment{
private int counter_left = 0;
private int counter_right = 0;
private RelativeLayout rl_left, rl_right;
private TextView tv_left, tv_right;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_scoreboard,null);
rl_left = (RelativeLayout) view.findViewById(R.id.layout_left_score);
rl_right = (RelativeLayout) view.findViewById(R.id.layout_right_score);
tv_left =(TextView) view.findViewById(R.id.tv_left_score);
tv_right = (TextView) view.findViewById(R.id.tv_right_score);
rl_left.setOnTouchListener(new OnSwipeTouchListener(getContext()) {
public void onSwipeUp() {
counter_left++;
tv_left.setText(Integer.toString(counter_left));
}
public void onSwipeDown() {
if (counter_left > 0) {
counter_left--;
}
tv_left.setText(Integer.toString(counter_left));
}
});
rl_right.setOnTouchListener(new OnSwipeTouchListener(getContext()) {
public void onSwipeUp() {
counter_right++;
tv_right.setText(Integer.toString(counter_right));
}
public void onSwipeDown() {
if (counter_right > 0) {
counter_right--;
}
tv_right.setText(Integer.toString(counter_right));
}
});
rl_left.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
counter_left = 0;
tv_left.setText(String.valueOf(counter_left));
counter_right = 0;
tv_right.setText(String.valueOf(counter_right));
return true;
}
});
rl_right.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
counter_right = 0;
tv_right.setText(String.valueOf(counter_right));
counter_left = 0;
tv_left.setText(String.valueOf(counter_left));
return true;
}
});
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//you can set the title for your toolbar here for different fragments different titles
getActivity().setTitle("Scoreboard");
}
}
OnSwipeTouchListener.java
package com.example.seth.sidespin;
import android.content.Context;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
/**
* Created by stins on 4/29/2017.
*/
public class OnSwipeTouchListener implements View.OnTouchListener {
private final GestureDetector gestureDetector;
public OnSwipeTouchListener(Context context) {
gestureDetector = new GestureDetector(context, new GestureListener());
}
public void onSwipeUp() {
}
public void onSwipeDown() {
}
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
private final class GestureListener extends GestureDetector.SimpleOnGestureListener {
private static final int SWIPE_DISTANCE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
float distanceX = e2.getX() - e1.getX();
float distanceY = e2.getY() - e1.getY();
if (Math.abs(distanceY) > Math.abs(distanceX) && Math.abs(distanceY) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (distanceY > 0)
onSwipeDown();
else
onSwipeUp();
return true;
}
return false;
}
}
}
最佳答案
我最好的猜测是TouchListener会自动覆盖其他所有内容,但我不确定。
是的,它确实。
尝试覆盖您的长按事件GestureDetector.SimpleOnGestureListener.onLongPress(MotionEvent)
。
关于java - OnLongClickListener无法与onTouchListener一起使用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43697840/