Android实现轮播效果的两种方法
实现轮播效果有2种方法:
(1)使用ViewPager+ImageView,通过Handler进行间隔发送消息,实现自动轮播效果
xml代码:
<android.support.v4.view.ViewPager android:id="@+id/fragment_viewPager" android:layout_width="match_parent" android:layout_height="200dp"/>
privateHandlerhandler=newHandler(){ @Override publicvoidhandleMessage(Messagemsg){ super.handleMessage(msg); list=(List<BannerBean>)msg.obj; //进行下载图片 for(inti=0;i<3;i++){ Picasso.with(getActivity()).load(list.get(i).getImgs()).resize(640,400).into(imgs.get(i)); } //展示图片 MyAdapteradapter=newMyAdapter(); viewPager.setAdapter(adapter); } }; privateHandlerhandler2=newHandler(){ @Override publicvoidhandleMessage(Messagemsg){ super.handleMessage(msg); //获取当前viewPager的位置 intpos=viewPager.getCurrentItem(); //如果是最后一张图片就进行设置为第一张,实现循环 if(pos==imgs.size()-1){ pos=0; //否则进行下一张图片展示 }else{ pos++; } //设置viewPager的位置 viewPager.setCurrentItem(pos); //定时发送消息 handler2.sendEmptyMessageDelayed(1,2000); } }; @Override publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer, BundlesavedInstanceState){ Viewview=inflater.inflate(R.layout.fragment_recreation,container,false); initview(view); load(); //发送消息给handler2,实现自动轮播 handler2.sendEmptyMessageDelayed(1,2000); initListener(); returnview; } privatevoidload(){ //下载图片地址 newThread(){ @Override publicvoidrun(){ super.run(); Stringjson=Utils.getJsonFromNet(ContentUrls.headLine_head+0); if(!TextUtils.isEmpty(json)){ List<BannerBean>data=ParserBanner.getData(json); Messagemsg=newMessage(); msg.obj=data; handler.sendMessage(msg); } } }.start(); data=newArrayList<>(); headlineAdapter=newHeadlineAdapter(data,getActivity()); lv.setAdapter(headlineAdapter); newMyAsyncTask(data,headlineAdapter).execute(ContentUrls.headLine_head+num); } privatevoidinitview(Viewview){ lv=((ListView)view.findViewById(R.id.lv)); headerView=LayoutInflater.from(getActivity()).inflate(R.layout.header_layout,null,false); viewPager=((ViewPager)headerView.findViewById(R.id.fragment_viewPager)); lv.addHeaderView(headerView); list=newArrayList<>(); imgs=newArrayList<>(); for(inti=0;i<3;i++){ ImageViewiv=newImageView(getActivity()); imgs.add(iv); } }
adapter:
classMyAdapterextendsPagerAdapter{ @Override publicintgetCount(){ returnimgs.size(); } @Override publicbooleanisViewFromObject(Viewview,Objectobject){ returnview==object; } @Override publicObjectinstantiateItem(ViewGroupcontainer,intposition){ container.addView(imgs.get(position)); returnimgs.get(position); } @Override publicvoiddestroyItem(ViewGroupcontainer,intposition,Objectobject){ //super.destroyItem(container,position,object); container.removeView(imgs.get(position)); } }
(2)使用SliderLayout第三方进行实现轮播
添加依赖:
compile'com.squareup.picasso:picasso:2.5.2' compile'com.daimajia.slider:library:1.1.5@aar' compile'com.nineoldandroids:library:2.4.0'
xml代码:
<?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <com.daimajia.slider.library.SliderLayout android:id="@+id/header_slider" android:layout_width="match_parent" android:layout_height="160dp"></com.daimajia.slider.library.SliderLayout> </LinearLayout>
Java代码:
privateListViewlv; privateString[]title={"烟","超标","标准","审车合格"}; privateint[]imgs={R.mipmap.a,R.mipmap.b,R.mipmap.c,R.mipmap.d}; privateSliderLayoutslider; @Override publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer, BundlesavedInstanceState){ Viewview=inflater.inflate(R.layout.fragment_home_page,container,false); lv=((ListView)view.findViewById(R.id.homePage_lv)); ViewheaderView=LayoutInflater.from(getActivity()).inflate(R.layout.header_layout,null,false); slider=((SliderLayout)headerView.findViewById(R.id.header_slider)); lv.addHeaderView(headerView); initSlider(); initData(); returnview; } privatevoidinitSlider(){ for(inti=0;i<4;i++){ TextSliderViewtextSlider=newTextSliderView(getActivity()); textSlider.image(imgs[i]).description(title[i]); slider.addSlider(textSlider); textSlider.setOnSliderClickListener(newBaseSliderView.OnSliderClickListener(){ @Override publicvoidonSliderClick(BaseSliderViewslider){ Toast.makeText(getActivity(),slider.getDescription(),Toast.LENGTH_SHORT).show(); } }); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。