欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现轮播效果的两种方法

程序员文章站 2023-12-19 22:50:52
实现轮播效果有2种方法: (1)使用viewpager+imageview,通过handler进行间隔发送消息,实现自动轮播效果 xml代码: &...

实现轮播效果有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" />

 private handler handler=new handler(){
    @override
    public void handlemessage(message msg) {
      super.handlemessage(msg);
      list= (list<bannerbean>) msg.obj;
      //进行下载图片
      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);
      //获取当前viewpager的位置
      int pos=viewpager.getcurrentitem();
      //如果是最后一张图片就进行设置为第一张,实现循环
      if(pos==imgs.size()-1){
        pos=0;
       //否则进行下一张图片展示
      }else{
        pos++;
      }
      //设置viewpager的位置
      viewpager.setcurrentitem(pos);
      //定时发送消息
      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();
    //发送消息给handler2,实现自动轮播
    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<bannerbean>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第三方进行实现轮播

添加依赖:

  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"?>
<linearlayout xmlns: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代码:

private listview lv;
  private string[] title={"烟","超标","标准","审车合格"};
  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();
        }
      });
    }
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: