Android ViewFlipper实现多个布局手势切换的效果
生活随笔
收集整理的這篇文章主要介紹了
Android ViewFlipper实现多个布局手势切换的效果
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
這里用到了前面學(xué)過的 手勢效果,如果對手勢還是不很了解的話可以去看一下這篇文章
Android使用GestureDetector實現(xiàn)手勢滑動效果 先看一下布局文件 activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" ><ViewFlipper
android:id="@+id/viewFlipper1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true" ><LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff000000"
android:orientation="vertical" >
</LinearLayout><LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffcccccc"
android:orientation="vertical" >
</LinearLayout><LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffff6600"
android:orientation="vertical" >
</LinearLayout>
</ViewFlipper></RelativeLayout>
布局文件很簡單,在ViewFlipper中包含了三個布局,全部用背景顏色區(qū)分。 再看一下MainActivity.java
package com.example.viewflipper;import android.os.Bundle;
import android.app.Activity;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.Menu;
import android.view.MotionEvent;
import android.widget.Toast;
import android.widget.ViewFlipper;public class MainActivity extends Activity implements OnGestureListener {private GestureDetector mGestureDetector;
private ViewFlipper mViewFlipper = null;@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGestureDetector = new GestureDetector(this, this);
mViewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper1);
}@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
mGestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}@Override
public boolean onDown(MotionEvent arg0) {
// TODO Auto-generated method stub
// Toast.makeText(this, "onDown", Toast.LENGTH_SHORT).show();
return false;
}@Override
public boolean onFling(MotionEvent startEvent, MotionEvent endEvent,
float velocityX, float velocityY) {
// TODO Auto-generated method stub
if (startEvent.getY() - endEvent.getY() > 100) {
Toast.makeText(this, "手勢向上滑動", Toast.LENGTH_SHORT).show();
return true;
} else if (startEvent.getY() - endEvent.getY() < -100) {
Toast.makeText(this, "手勢向下滑動", Toast.LENGTH_SHORT).show();
return true;
} else if (startEvent.getX() - endEvent.getX() > 100) {
Toast.makeText(this, "手勢向左滑動", Toast.LENGTH_SHORT).show();
mViewFlipper.showNext();
return true;
} else if (startEvent.getX() - endEvent.getX() < -100) {
Toast.makeText(this, "手勢向右滑動", Toast.LENGTH_SHORT).show();
mViewFlipper.showPrevious();
return true;
}
return false;
}@Override
public void onLongPress(MotionEvent arg0) {
// TODO Auto-generated method stub
// Toast.makeText(this, "onLongPress ", Toast.LENGTH_SHORT).show();
}@Override
public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
float arg3) {
// TODO Auto-generated method stub
// Toast.makeText(this, "onScroll", Toast.LENGTH_SHORT).show();
return false;
}@Override
public void onShowPress(MotionEvent arg0) {
// TODO Auto-generated method stub
// Toast.makeText(this, "onShowPress", Toast.LENGTH_SHORT).show();
}@Override
public boolean onSingleTapUp(MotionEvent arg0) {
// TODO Auto-generated method stub
// Toast.makeText(this, "onSingleTapUp", Toast.LENGTH_SHORT).show();
return false;
}@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}}
這樣就可以實現(xiàn)最簡單的切換效果了。
總結(jié)
以上是生活随笔為你收集整理的Android ViewFlipper实现多个布局手势切换的效果的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Apache htaccess的简单总结
- 下一篇: android sina oauth2.