Android 滑动和滚动一起工作
我正在制作一个应用程序,其中我有一个文本视图(占据了我大部分的活动)。Textview在多个页面上显示大量文本(页面只不过是分配到字符串数组中的大文本以及页面更改触发器上显示的每个数组元素)。我使用了下面线程中的代码来引入右/左滑动以在页面间导航 下面是我在all中使用的确切代码Android 滑动和滚动一起工作,android,Android,我正在制作一个应用程序,其中我有一个文本视图(占据了我大部分的活动)。Textview在多个页面上显示大量文本(页面只不过是分配到字符串数组中的大文本以及页面更改触发器上显示的每个数组元素)。我使用了下面线程中的代码来引入右/左滑动以在页面间导航 下面是我在all中使用的确切代码 public class OnSwipeTouchListener extends Activity implements OnTouchListener{ private final GestureDetecto
public class OnSwipeTouchListener extends Activity implements OnTouchListener{
private final GestureDetector gestureDetector = new GestureDetector(getBaseContext(), new GestureListener());
public boolean onTouch(final View v, final MotionEvent event) {
//super.onTouch(v, event);
return gestureDetector.onTouchEvent(event);
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_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) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
}
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}
public void onSwipeRight() {
}
public void onSwipeLeft() {
}
这就是我在活动中如何使用这门课
myTextView.setOnTouchListener(new OnSwipeTouchListener() {
public void onSwipeRight() {
displayPreviousPage() //code to display previous page
}
public void onSwipeLeft() {
displayNextPage() //code to display next page
}
});
到目前为止一切正常。现在我想为我的文本视图启用垂直滚动,以防我的文本超出屏幕高度。因此,我在我的代码中添加了下面的行来执行同样的操作
myTextView.setMovementMethod(new ScrollingMovementMethod());
但这似乎不起作用。有什么建议吗?是我在OnSwipeTouchListener类中重写了onFling方法,并且setMovementMethod也在使用相同的(onFling方法)实现它吗 您的TextView是动态的吗?不是,它是静态的,预先用xml定义的。但是,如果有帮助的话,我可以将其设置为动态的。尝试不添加setMovementMethod,而是尝试设置XML属性android:scrollbars=“vertical”我需要在代码中动态设置,因为我只在行数超过屏幕高度时才启用滚动。我也尝试了contentTextView.setVerticalScrollBarEnabled,但没有成功。我更改了代码以使用不同的方法。同样的问题,请参阅下面的帖子:(