关闭

Android ViewFlipper装载baseAdapter

标签: Android ViewFlipper装
461人阅读 评论(0) 收藏 举报
分类:

转载地址http://www.cnblogs.com/xiao0/archive/2011/09/13/2174361.html


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()) 这样可以
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:267236次
    • 积分:3775
    • 等级:
    • 排名:第8893名
    • 原创:101篇
    • 转载:63篇
    • 译文:4篇
    • 评论:41条
    博客专栏
    最新评论