Android ViewFlipper装载baseAdapter

分享到:
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ViewFlipper;

public class Main extends Activity implements OnClickListener{
/** Called when the activity is first created. */
private ViewFlipper vf;
private Button prevous ;
private Button next;

private int[] dr = {R.drawable.gallery_photo_1,R.drawable.gallery_photo_2,
R.drawable.gallery_photo_3,R.drawable.gallery_photo_4,R.drawable.gallery_photo_5,R.drawable.gallery_photo_6,R.drawable.gallery_photo_7,R.drawable.gallery_photo_8,};


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

vf = (ViewFlipper) findViewById(R.id.ViewFlipper01);
vf.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
vf.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
adapter ad = new adapter(this, dr);
for (int i = 0; i < dr.length; i++) {
vf.addView(ad.getView(i, null, null));
}


prevous = (Button)findViewById(R.id.previous);
next = (Button)findViewById(R.id.next);
prevous.setOnClickListener(this);
next.setOnClickListener(this);

}







@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.previous:
vf.showPrevious();

break;
case R.id.next:
vf.showNext();
break;
default:
break;
}

}



class adapter extends BaseAdapter{

int[] drawables = null;
private Context mcontext;
public adapter(Context context, int[] draw){
mcontext = context;
drawables = draw;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return drawables.length;
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return drawables[position];
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return drawables[position];
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView im = new ImageView(mcontext);
im.setImageDrawable(getResources().getDrawable(drawables[position]));
return im;
}

}
}

viewfilipper 本身不能加载adapter ,但是可以给他传递getview viewflipper.addview(getview()) 这样可以
昵    称:
验证码:

相关文档: