最基本的滑动:ViewFlipper 中包含多个View ,View之间的切换。
配置文件layout_viewflipper.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <ViewFlipper
- android:layout_height="wrap_content"
- android:layout_width="match_parent"
- android:id="@+id/viewFipper01">
- <include android:id="@+id/layout1" layout="@layout/layout_view1"/>
- <include android:id="@+id/layout2" layout="@layout/layout_view2"/>
- </ViewFlipper>
- </LinearLayout>
对应的layout_view1.xml和layout_view2.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <TextView
- android:layout_height="wrap_content"
- android:layout_width="fill_parent"
- android:gravity="center"
- android:text="鸟"
- android:textSize="50pt"
- />
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <TextView
- android:layout_height="wrap_content"
- android:layout_width="fill_parent"
- android:gravity="center"
- android:text="鱼"
- android:textSize="50pt">
- </TextView>
- </LinearLayout>
实现代码:
- public class TestViewFlipper extends Activity
- {
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.layout_viewflipper);
- final ViewFlipper vf = (ViewFlipper)findViewById(R.id.viewFipper01);
- vf.setOnClickListener(new View.OnClickListener()
- {
- @Override
- public void onClick(View v)
- {
- //点击所在区域即可滑动到下一屏幕
- vf.showNext();
- }
- });
- //动画切入,从左边进入
- vf.setInAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.slide_in_left));
- //动画切出,从右边离开
- vf.setOutAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.slide_out_right));
- }
- }