AdapterViewFlipper简介
AdapterViewFlipper继承了AdapterViewAnimater。每次只能显示一个组件,用showPrevious()和showNext()来显示这些组件上一个和下一个。还有startFlipping()控制它自动播放。
AdapterViewFlipper属性
代码实例
package peng.liu.testview;import android.app.Activity;import android.content.res.TypedArray;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.AdapterViewFlipper;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageView;public class MainActivity extends Activity { private int[] imageIds = new int[]{ R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher }; private AdapterViewFlipper flipper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); flipper = (AdapterViewFlipper) findViewById(R.id.flipper); BaseAdapter adapter = new BaseAdapter() { @Override public int getCount() { return 0; } @Override public Object getItem(int i) { return null; } @Override public long getItemId(int i) { return 0; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ImageView image = new ImageView(MainActivity.this); image.setImageResource(imageIds[i]); image.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); image.setScaleType(ImageView.ScaleType.FIT_XY); return image; } }; }}
<LinearLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:orientation="vertical"> <AdapterViewFlipper android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/flipper" android:flipInterval="5000" tools:ignore="NewApi" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/previous" android:onClick="previous" android:text="previous"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/next" android:onClick="next" android:text="next"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/auto" android:onClick="auto" android:text="auto"/> </LinearLayout></LinearLayout>
布局代码
<LinearLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:orientation="vertical"> <AdapterViewFlipper android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/flipper" android:flipInterval="5000" tools:ignore="NewApi" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/previous" android:onClick="previous" android:text="previous"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/next" android:onClick="next" android:text="next"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/auto" android:onClick="auto" android:text="auto"/> </LinearLayout></LinearLayout>
StackView简介
StackView也是继承于AdapterViewAnimator,它是以重叠在一起的,使用showPrevious()和showNext()分别显示上一张和下一张。属性类似AdapterViewFlipper
实例
package peng.liu.testview;import android.annotation.TargetApi;import android.app.Activity;import android.content.res.TypedArray;import android.os.Build;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.AdapterViewFlipper;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageView;import android.widget.SimpleAdapter;import android.widget.StackView;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;public class MainActivity extends Activity { private int[] imageIds = new int[]{ R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher }; private StackView stack; @TargetApi(Build.VERSION_CODES.HONEYCOMB) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); stack = (StackView) findViewById(R.id.stack); List<Map<String,Object>> listItems = new ArrayList<Map<String,Object>>(); for (int i = 0;i<imageIds.length;i++){ Map<String ,Object> item = new HashMap<String,Object>(); item.put("image",imageIds[i]); listItems.add(item); } SimpleAdapter adapter = new SimpleAdapter(this,listItems,R.layout.simple_item,new String[]{"image"},new int[]{R.id.image01}); stack.setAdapter(adapter); }}
<LinearLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:orientation="vertical"> <StackView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/stack" android:loopViews="true" tools:ignore="NewApi" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/previous" android:text="previous" android:onClick="previous"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/next" android:text="next" android:onClick="next"/> </LinearLayout></LinearLayout>
<LinearLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:orientation="vertical"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/image01"/></LinearLayout>