布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/colorGray"
>
<AdapterViewFlipper
android:id="@+id/flipper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:flipInterval="2000"
android:layout_alignParentTop="true"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:onClick="prev"
android:text="@string/prev"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="next"
android:text="@string/next"
/>
<Button
android:id="@+id/autoButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:onClick="auto"
android:text="@string/auto"
/>
</RelativeLayout>
主程序代码:
package com.toby.personal.testlistview;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterViewFlipper;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
final private static String TAG = "Toby_Test";
private AdapterViewFlipper adapterViewFlipper = null;
private Button autoButton = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final int[] images = new int[]{
R.drawable.img01, R.drawable.img02, R.drawable.img03, R.drawable.img04,
R.drawable.img05, R.drawable.img06, R.drawable.img07, R.drawable.img08,
R.drawable.img09, R.drawable.img10, R.drawable.img11, R.drawable.img12,
R.drawable.dog_001, R.drawable.dog_002, R.drawable.dog_003, R.drawable.dog_004,
R.drawable.dog_005,
R.drawable.girl01, R.drawable.girl02, R.drawable.girl03, R.drawable.girl04,
R.drawable.girl05
};
BaseAdapter adapter = new BaseAdapter() {
@Override
public int getCount() {
return images.length;
}
@Override
public Object getItem(int i) {
return i;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(images[i]);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(
new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
return imageView;
}
};
adapterViewFlipper = (AdapterViewFlipper) findViewById(R.id.flipper);
adapterViewFlipper.setAdapter(adapter);
autoButton = (Button) findViewById(R.id.autoButton);
}
public void prev(View view) {
adapterViewFlipper.showPrevious();
}
public void next(View view) {
adapterViewFlipper.showNext();
}
public void auto(View view) {
if (adapterViewFlipper.isFlipping()) {
adapterViewFlipper.stopFlipping();
autoButton.setText(R.string.auto);
} else {
adapterViewFlipper.startFlipping();
autoButton.setText(R.string.stop);
}
}
}
显示效果:
显示效果
参考文献:《疯狂Android讲义(第2版)》
网友评论