Android|Android(ViewPager制作幻灯片)



最近在项目中用到图片轮播,试了Gallery,ViewFlipper,ViewPager,感觉Gallery最符合需求,但是Gallery的系统边框很难看,项目中要求用自己的背景图片。
接下来用viewpager来做幻灯片效果。


Android|Android(ViewPager制作幻灯片)
文章图片
程序:
Android|Android(ViewPager制作幻灯片)
文章图片
public class MainActivity extends Activity { //ViewPager private ViewPager viewpager; //幻灯片图片资源 private int [] imgArray={R.drawable.view1,R.drawable.view2,R.drawable.view3}; //图片数组 private ArrayList viewlist; private ImageView imageView; //点点 private ViewGroup pointgroup; private ImageView[] pointarr; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_viewpage); //初始化 viewpager =(ViewPager) findViewById(R.id.viewpager); pointgroup =(ViewGroup) findViewById(R.id.pointgroup); viewlist = new ArrayList(); //将图片装载到数组 for(int i=0; iAndroid|Android(ViewPager制作幻灯片)
文章图片
【Android|Android(ViewPager制作幻灯片)】 自定义适配器:
Android|Android(ViewPager制作幻灯片)
文章图片
public class MyPageAdapter extends PagerAdapter { private List viewlist; private Context context; public MyPageAdapter(List viewlist,Context context){ this.viewlist =viewlist; this.context=context; }@Override public int getCount() { // TODO Auto-generated method stub return viewlist.size(); }@Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0==arg1; }//以下也是必要方法,不过要自己添加 //实例化@Override public Object instantiateItem(View container, int position) { // TODO Auto-generated method stub ((ViewPager)container).addView(viewlist.get(position)); View view =viewlist.get(position); view.setOnClickListener(new OnClickListener(){@Override public void onClick(View v) { // TODO Auto-generated method stub int i =v.getId(); String t="你点击了图片的图片id为:"+i; Toast.makeText(context, t, Toast.LENGTH_LONG).show(); /*跳转页面 *Intent intent =new Intent(context,NavigationActivity.class); *context.startActivity(intent); */ }}); return viewlist.get(position); }//销毁 @Override public void destroyItem(ViewGroup container, int position, Object object) { // TODO Auto-generated method stub container.removeView(viewlist.get(position)); }}

    推荐阅读