Android手势翻页效果
2010-09-26 01:34:00 来源:WEB开发网3.
4. flipper.addView(addTextView("step 1"));//将View添加到flipper队列中
5. flipper.addView(addTextView("step 2"));
6. flipper.addView(addTextView("step 3"));
7. flipper.addView(addTextView("step 4"));
8. flipper.addView(addTextView("step 5"));
detector = new GestureDetector(this);//初始化触摸探测
flipper = (ViewFlipper) this.findViewById(R.id.ViewFlipper01);//获得ViewFlipper实例
flipper.addView(addTextView("step 1"));//将View添加到flipper队列中
flipper.addView(addTextView("step 2"));
flipper.addView(addTextView("step 3"));
flipper.addView(addTextView("step 4"));
flipper.addView(addTextView("step 5"));
addTextView方法如下:
Java代码
1. private View addTextView(String text) {
2. TextView tv = new TextView(this);
3. tv.setText(text);
4. tv.setGravity(1);
5. return tv;
6. }
private View addTextView(String text) {
TextView tv = new TextView(this);
tv.setText(text);
tv.setGravity(1);
return tv;
}
flipper将按照你的添加顺序排列这些View,并通过flipper.showNext()和flipper.showPrevious()显示.
还需要多重写一个方法:onTouchEvent(MotionEvent event),否则detector检测不到触摸,这个方法非常简单.
Java代码
1. @Override
2. public boolean onTouchEvent(MotionEvent event) {
3. return this.detector.onTouchEvent(event);
4. }
@Override
public boolean onTouchEvent(MotionEvent event) {
return this.detector.onTouchEvent(event);
}
现在开始做动作监听,在onFling方法中加入以下内容:
Java代码
1. @Override
2. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
3. float velocityY) {
更多精彩
赞助商链接