http://www.open-open.com/lib/view/open1325602955359.html
怎麼實現Android主頁面的左右拖動效果。其實實現起來很簡單,就是使用ViewFlipper來將您要來回拖動的View裝在一起,然後與GestureDetector手勢識別類來聯動,確定要顯示哪個View,加上一點點動畫效果即可。
01 |
public
class TestFlip extends
Activity implements
OnGestureListener { |
03 |
private
ViewFlipper flipper; |
05 |
private
GestureDetector detector; |
07 |
/** Called when the activity is first created. */ |
09 |
public
void onCreate(Bundle savedInstanceState) {
|
10 |
super .onCreate(savedInstanceState);
|
11 |
setContentView(R.layout.main);
|
13 |
detector = new
GestureDetector( this );
|
14 |
flipper = (ViewFlipper) this .findViewById(R.id.ViewFlipper01);
|
15 |
flipper.addView(addView(R.layout.layout1));
|
16 |
flipper.addView(addView(R.layout.layout2));
|
17 |
flipper.addView(addView(R.layout.layout3));
|
18 |
flipper.addView(addView(R.layout.layout4));
|
21 |
private
View addView( int
layout) { |
22 |
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
23 |
View view = inflater.inflate(layout,
null );
|
28 |
public
boolean onTouchEvent(MotionEvent event) {
|
29 |
return
this .detector.onTouchEvent(event); |
33 |
public
boolean onDown(MotionEvent e) { |
39 |
public
boolean onFling(MotionEvent e1, MotionEvent e2,
float velocityX,
|
41 |
if
(e1.getX() - e2.getX() > 120 ) {
|
42 |
this .flipper.setInAnimation(AnimationUtils.loadAnimation( this , R.anim.push_left_in));
|
43 |
this .flipper.setOutAnimation(AnimationUtils.loadAnimation( this , R.anim.push_left_out));
|
44 |
this .flipper.showNext();
|
46 |
} else
if (e1.getX() - e2.getX() < - 120 ) {
|
47 |
this .flipper.setInAnimation(AnimationUtils.loadAnimation( this , R.anim.push_right_in));
|
48 |
this .flipper.setOutAnimation(AnimationUtils.loadAnimation( this , R.anim.push_right_out));
|
49 |
this .flipper.showPrevious();
|
56 |
public
void onLongPress(MotionEvent e) { |
62 |
public
boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX,
|
69 |
public
void onShowPress(MotionEvent e) { |
75 |
public
boolean onSingleTapUp(MotionEvent e) { |
01 |
<? xml
version = "1.0"
encoding = "utf-8" ?>
|
02 |
< LinearLayout
xmlns:Android = "http://schemas.android.com/apk/res/android" |
03 |
Android:orientation = "vertical" |
04 |
Android:layout_width = "fill_parent" |
05 |
Android:layout_height = "fill_parent" |
07 |
< ViewFlipper
Android:id = "@+id/ViewFlipper01" |
08 |
Android:layout_width = "fill_parent"
android:layout_height = "fill_parent" >
|