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

安卓学习-界面-ui-AdapterViewFlipper

类似Gallery,只不过这个是一张图片页

属性方法说明
 android:animateFirstView setAnimateFirstView(boolean animate) 显示第一个View时是否使用动画
 android:inAnimation setInAnimation(ObjectAnimator inAnimation)View显示动画,不用使用,直接报错
 android:outAnimation setOutAnimation(ObjectAnimator outAnimation)View隐藏动画 ,不用使用,直接报错
 android:loopViews  是否循环显示,最后一个来过就是第一个
 android:autoStart startFlipping() 是否自动播放
 android:flipInterval setFlipInterval(int flipInterval)  自动播放的时间间隔

例子

item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent" >    <ImageView        android:id="@+id/imageView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Large Text"                android:textAppearance="?android:attr/textAppearanceLarge"                 /></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:padding="10dp" >    <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>    <AdapterViewFlipper        android:id="@+id/adapterViewFlipper1"        android:layout_width="match_parent"        android:layout_height="wrap_content"   >    </AdapterViewFlipper></LinearLayout>
View Code

MainActivity.java

package com.example.ddd;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.widget.AdapterViewFlipper;import android.widget.Button;import android.widget.SimpleAdapter;public class MainActivity extends Activity implements OnClickListener{    AdapterViewFlipper adapterViewFlipper1;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                List<Map<String,Object>> items=new ArrayList<Map<String,Object>>();                Map<String,Object> map1=new HashMap<String,Object> ();        map1.put("name", "华为");        map1.put("pic", R.drawable.pic1);        items.add(map1);        Map<String,Object> map2=new HashMap<String,Object> ();        map2.put("name", "苹果");        map2.put("pic", R.drawable.pic2);        items.add(map2);        Map<String,Object> map3=new HashMap<String,Object> ();        map3.put("name", "三星");        map3.put("pic", R.drawable.pic3);        items.add(map3);           SimpleAdapter sa=new SimpleAdapter(MainActivity.this,items,R.layout.item,new String[]{"name","pic"},new int[]{R.id.textView1,R.id.imageView1});        adapterViewFlipper1=(AdapterViewFlipper)findViewById(R.id.adapterViewFlipper1);                adapterViewFlipper1.setAdapter(sa);                Button btn1=(Button)findViewById(R.id.button1);        Button btn2=(Button)findViewById(R.id.button2);        Button btn3=(Button)findViewById(R.id.button3);        btn1.setOnClickListener(this);        btn2.setOnClickListener(this);        btn3.setOnClickListener(this);    }    @Override    public void onClick(View v) {        if(v.getId()==R.id.button1){            //上一张            adapterViewFlipper1.showPrevious();        }        else if(v.getId()==R.id.button2){            //下一张                adapterViewFlipper1.showNext();        }        else if(v.getId()==R.id.button3){                        adapterViewFlipper1.setFlipInterval(5000);            adapterViewFlipper1.startFlipping();    }    }}
View Code

 

安卓学习-界面-ui-AdapterViewFlipper