package org.hjw.adapterviewflippertest; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.widget.AdapterViewFlipper; import android.widget.BaseAdapter; import android.widget.ImageView; public class MainActivity extends Activity { private AdapterViewFlipper flipper; private int[] images=new int[]{ R.drawable.mogu1,R.drawable.mogu2 }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); flipper=(AdapterViewFlipper)findViewById(R.id.filpper); BaseAdapter base=new BaseAdapter() { @Override public View getView(int arg0, View arg1, ViewGroup arg2) { ImageView image=new ImageView(MainActivity.this); image.setImageResource(images[arg0]); image.setScaleType(ImageView.ScaleType.FIT_XY); image.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); return image; } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return arg0; } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return arg0; } @Override public int getCount() { // TODO Auto-generated method stub return images.length; } }; flipper.setAdapter(base); } public void prev(View v){ flipper.showPrevious(); flipper.stopFlipping(); } public void next(View v){ flipper.showNext(); flipper.stopFlipping(); } public void auto(View v){ flipper.startFlipping(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
ps:1.adpterviewflipper很好的实现了自动播放幻灯片的功能
2.BaseAdapter的使用,flipper只是一个容器