首页 > 代码库 > 安卓学习-界面-ui-ViewFlipper

安卓学习-界面-ui-ViewFlipper

ViewFlipper可以添加多个View并可以上一张、下一张、自动播放等

3个View,内容随便

view1.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="第一页"        android:textAppearance="?android:attr/textAppearanceLarge" />    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Button" /></LinearLayout>
View Code

view2.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="第二页"        android:textAppearance="?android:attr/textAppearanceLarge" />    <ZoomControls        android:id="@+id/zoomControls1"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>
View Code

view3.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="第三页"        android:textAppearance="?android:attr/textAppearanceLarge" />    <AnalogClock        android:id="@+id/analogClock1"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>
View Code

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/linearLayout1"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"     android:background="#999"    >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content" >        <Button            android:id="@+id/button1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="上一张" />        <Button            android:id="@+id/button2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="下一张" />        <Button            android:id="@+id/button3"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="自动播放" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content" >        <ViewFlipper            android:id="@+id/viewFlipper1"            android:layout_width="match_parent"            android:layout_height="match_parent" >        </ViewFlipper>    </LinearLayout></LinearLayout>
View Code

MainActivity.java

public class MainActivity extends Activity{    //用来判断手指是向左滑动还是向右    float x1;    float x2;        ViewFlipper viewFlipper1;        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                viewFlipper1=(ViewFlipper)findViewById(R.id.viewFlipper1);        Button btn1=(Button)findViewById(R.id.button1);        Button btn2=(Button)findViewById(R.id.button2);        Button btn3=(Button)findViewById(R.id.button3);                View view1=LayoutInflater.from(getApplicationContext()).inflate(R.layout.view1, null);        View view2=LayoutInflater.from(getApplicationContext()).inflate(R.layout.view2, null);        View view3=LayoutInflater.from(getApplicationContext()).inflate(R.layout.view3, null);        viewFlipper1.addView(view1, 0);        viewFlipper1.addView(view2, 1);        viewFlipper1.addView(view3, 2);                //上一张        btn1.setOnClickListener(new OnClickListener() {            public void onClick(View v) {                pre();            }        });        // 下一张        btn2.setOnClickListener(new OnClickListener() {            public void onClick(View v) {                next();            }        });                btn3.setOnClickListener(new OnClickListener() {                        @Override            public void onClick(View v) {                viewFlipper1.setFlipInterval(1000);                viewFlipper1.startFlipping();                            }        });        //识别手势        viewFlipper1.setOnTouchListener(new OnTouchListener() {            public boolean onTouch(View v, MotionEvent event) {                //手指按下时的坐标                if(event.getAction()==MotionEvent.ACTION_DOWN){                    x1=event.getX();                }                //手指松开时的坐标                else if(event.getAction()==MotionEvent.ACTION_UP){                    x2=event.getX();                    //向左滑动                    if(x1-x2>0){                        Log.v("", "向左滑动");                        next();                                            }                    //向右滑动                    else if(x2-x1>0){                        Log.v("", "向右滑动");                        pre();                    }                }                return true;            }        });      }            void next(){        // 为ViewSwitcher的组件显示过程设置动画          viewFlipper1.setInAnimation(this, R.anim.slide_in_right);         // 为ViewSwitcher的组件隐藏过程设置动画          viewFlipper1.setOutAnimation(this, R.anim.slide_out_left);          viewFlipper1.showNext();    }       void pre(){        //为ViewSwitcher的组件显示过程设置动画         viewFlipper1.setInAnimation(this, android.R.anim.slide_in_left);          // 为ViewSwitcher的组件隐藏过程设置动画          viewFlipper1.setOutAnimation(this, android.R.anim.slide_out_right);          viewFlipper1.showPrevious();            }    }
View Code

 

安卓学习-界面-ui-ViewFlipper