我正在寻找一种方法来更改TextViews
用户滑动包含这些视图的布局时的内容。我的目标是这样的:
我正在尝试使用SimpleOnGestureListener
(代码如下)来做到这一点。问题:当我从左向右滑动时,i 的值变为-1,并且后续滑动没有反应。从右向左滑动会导致 i 变为 1(到底是零?)并且视图再次停止对滑动做出反应。我究竟做错了什么?
public class MainActivity extends Activity {
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private GestureDetector gestureDetector;
public View mainview;
public TextView lol;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gestureDetector = new GestureDetector(new MyGestureDetector());
mainview = (View) findViewById(R.id.mainView);
lol = (TextView) findViewById(R.id.textView2);
mainview.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
});
}
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
int i = 0;
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
return false;
}
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
i++;
lol.setText("Text" + " " + Integer.toString(i));
return true;
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
i--;
lol.setText("Text" + " " + Integer.toString(i));
return true;
}
return false;
}
@Override
public boolean onDown(MotionEvent e) {
return true;
}
}
}