實(shí)現(xiàn)輪播效果有2種方法:
專注于為中小企業(yè)提供網(wǎng)站設(shè)計(jì)制作、網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)托克托免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了超過(guò)千家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
(1)使用ViewPager+ImageView,通過(guò)Handler進(jìn)行間隔發(fā)送消息,實(shí)現(xiàn)自動(dòng)輪播效果
xml代碼:
private Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); list= (List) msg.obj; //進(jìn)行下載圖片 for (int i = 0; i < 3; i++) { Picasso.with(getActivity()).load(list.get(i).getImgs()).resize(640,400).into(imgs.get(i)); } //展示圖片 MyAdapter adapter=new MyAdapter(); viewPager.setAdapter(adapter); } }; private Handler handler2=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); //獲取當(dāng)前viewPager的位置 int pos=viewPager.getCurrentItem(); //如果是最后一張圖片就進(jìn)行設(shè)置為第一張,實(shí)現(xiàn)循環(huán) if(pos==imgs.size()-1){ pos=0; //否則進(jìn)行下一張圖片展示 }else{ pos++; } //設(shè)置viewPager的位置 viewPager.setCurrentItem(pos); //定時(shí)發(fā)送消息 handler2.sendEmptyMessageDelayed(1,2000); } }; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment_recreation, container, false); initview(view); load(); //發(fā)送消息給handler2,實(shí)現(xiàn)自動(dòng)輪播 handler2.sendEmptyMessageDelayed(1,2000); initListener(); return view; } private void load() { //下載圖片地址 new Thread(){ @Override public void run() { super.run(); String json= Utils.getJsonFromNet(ContentUrls.headLine_head+0); if(!TextUtils.isEmpty(json)){ List data= ParserBanner.getData(json); Message msg=new Message(); msg.obj=data; handler.sendMessage(msg); } } }.start(); data=new ArrayList<>(); headlineAdapter=new HeadlineAdapter(data,getActivity()); lv.setAdapter(headlineAdapter); new MyAsyncTask(data,headlineAdapter).execute(ContentUrls.headLine_head+num); } private void initview(View view) { 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=new ArrayList<>(); imgs=new ArrayList<>(); for (int i = 0; i < 3; i++) { ImageView iv=new ImageView(getActivity()); imgs.add(iv); } }
adapter:
class MyAdapter extends PagerAdapter{ @Override public int getCount() { return imgs.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view==object; } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(imgs.get(position)); return imgs.get(position); } @Override public void destroyItem(ViewGroup container, int position, Object object) { // super.destroyItem(container, position, object); container.removeView(imgs.get(position)); } }
(2)使用SliderLayout第三方進(jìn)行實(shí)現(xiàn)輪播
添加依賴:
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代碼:
<?xml version="1.0" encoding="utf-8"?>
Java代碼:
private ListView lv; private String[] title={"煙","超標(biāo)","標(biāo)準(zhǔn)","審車合格"}; private int[] imgs={R.mipmap.a,R.mipmap.b,R.mipmap.c,R.mipmap.d}; private SliderLayout slider; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment_home_page, container, false); lv = ((ListView) view.findViewById(R.id.homePage_lv)); View headerView=LayoutInflater.from(getActivity()).inflate(R.layout.header_layout,null,false); slider = ((SliderLayout) headerView.findViewById(R.id.header_slider)); lv.addHeaderView(headerView); initSlider(); initData(); return view; } private void initSlider() { for (int i = 0; i < 4; i++) { TextSliderView textSlider=new TextSliderView(getActivity()); textSlider.image(imgs[i]).description(title[i]); slider.addSlider(textSlider); textSlider.setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() { @Override public void onSliderClick(BaseSliderView slider) { Toast.makeText(getActivity(),slider.getDescription(),Toast.LENGTH_SHORT).show(); } }); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。