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

Android 使用ViewPager实现轮播图效果

程序员文章站 2023-11-17 08:36:58
写这篇文章只是对今天所学的知识进行加深印象,对viewpager的一些处理,比如适配器和各个方法的作用等。 先看效果图 这里我是在xml中写的圆点 drawabl...

写这篇文章只是对今天所学的知识进行加深印象,对viewpager的一些处理,比如适配器和各个方法的作用等。

先看效果图

Android 使用ViewPager实现轮播图效果

这里我是在xml中写的圆点

drawable文件夹下的xml代码:

shape_yes.xml:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
  <solid android:color="#ff6c67"></solid> 
  <corners android:radius="1000dp"></corners> 
</shape>

shape_no.xml:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
<solid android:color="#f0f0f0"></solid> 
  <corners android:radius="1000dp"></corners> 
</shape>

选择器

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:drawable="@drawable/shape_no" 
  android:state_selected="false" 
  /> 
  <item android:drawable="@drawable/shape_yes" 
   android:state_selected="true" 
    /> 
</selector> 

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent"> 
  <relativelayout 
    android:layout_width="match_parent" 
    android:layout_height="160dp"> 
    <android.support.v4.view.viewpager 
      android:id="@+id/vp" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
    </android.support.v4.view.viewpager> 
    <linearlayout 
      android:layout_width="match_parent" 
      android:layout_height="40dp" 
      android:layout_alignparentbottom="true" 
      android:gravity="center_horizontal" 
      android:padding="5dp" 
      android:orientation="vertical" 
      android:background="#66000000"> 
      <linearlayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_gravity="bottom" 
        android:gravity="center" 
        android:orientation="horizontal" 
        android:paddingbottom="10dp"> 
        <imageview 
          android:id="@+id/iv_one" 
          android:layout_width="20dp" 
          android:layout_height="20dp" 
          android:layout_marginright="20dp" 
          android:background="@drawable/selector_show" /> 
        <imageview 
          android:id="@+id/iv_two" 
          android:layout_width="20dp" 
          android:layout_height="20dp" 
          android:layout_marginright="20dp" 
          android:background="@drawable/selector_show" /> 
        <imageview 
          android:id="@+id/iv_three" 
          android:layout_width="20dp" 
          android:layout_height="20dp" 
          android:background="@drawable/selector_show" /> 
      </linearlayout> 
    </linearlayout> 
  </relativelayout> 
</relativelayout> 

写一个类viewpageradapter_circleextends pageradapter

public class viewpageradapter_circle extends pageradapter { 
  arraylist<view> arraylist; 
  context context; 
  public viewpageradapter_circle(arraylist<view> arraylist, context context) { 
    this.arraylist = arraylist; 
    this.context = context; 
  } 
  /***数据源的大小(条目)*/ 
  @override 
  public int getcount() { 
    return arraylist.size(); 
  } 
  /** 
   * 页面视图是否关联到特定的对象 
   */ 
  @override 
  public boolean isviewfromobject(view view, object object) { 
    return view == object;//判断当前要显示的页面 
  } 
  /** 
   * 初始化页面 
   * 1.页面添加到container 
   * 2.将页面返回 
   */ 
  @override 
  public object instantiateitem(viewgroup container, int position) { 
    container.addview(arraylist.get(position));//页面添加到container,添加位置 
    return arraylist.get(position); 
  } 
  /** 
   * 销毁当前页面 
   */ 
  @override 
  public void destroyitem(viewgroup container, int position, object object) { 
    container.removeview(arraylist.get(position)); 
  } 
} 

mainactivity中代码(含注解)

public class mainactivity extends appcompatactivity { 
  viewpager viewpager; 
  int imageresids[]; 
  arraylist<view> imageviewlist;//存图片的集合 
  arraylist<imageview> imageviews;//存白点的集合 
  imageview iv_one,iv_two,iv_three; 
  int oldpostion;//记录原来的位置 
  private boolean flag; 
  int count=0; 
  handler handler = new handler() { 
    @override 
    public void handlemessage(message msg) { 
      super.handlemessage(msg); 
      if (msg.what == 0) { 
        viewpager.setcurrentitem(count); 
      } 
    } 
  }; 
  @override 
  protected void oncreate(bundle savedinstancestate) { 
    super.oncreate(savedinstancestate); 
    setcontentview(r.layout.activity_main); 
    //初始化布局 
    initview(); 
    //初始化数据 
    initdata(); 
    //设置适配器 
    initadapter(); 
    //设置圆点 
    initpoint(); 
    flag = true; 
//循环设置播放 
    new thread() { 
      @override 
      public void run() { 
        while (flag) { 
          try { 
            thread.sleep(3000); 
            count++; 
            if (count == 3) { 
              count = 0; 
            } 
          } catch (interruptedexception e) { 
            e.printstacktrace(); 
          } 
          /**传递信息*/ 
          message msg = new message(); 
          msg.what = 0; 
          msg.obj = count; 
          handler.sendmessage(msg); 
        } 
      } 
    }.start(); 
  } 
  private void initview() { 
    viewpager = (viewpager) findviewbyid(r.id.vp); 
  } 
  //初始化要显示的数据 
  private void initdata() { 
    //图片资源数组 
    imageresids = new int[]{r.mipmap.a, r.mipmap.b, r.mipmap.c}; 
    imageviewlist = new arraylist<>();//存图片的集合 
    imageview imageview; 
    for (int i = 0; i < imageresids.length; i++) { 
      imageview = new imageview(this); 
      imageview.setbackgroundresource(imageresids[i]); 
      imageviewlist.add(imageview); 
    } 
  } 
  //设置适配器 
  private void initadapter() { 
    viewpageradapter_circle myviewpageradapter = new viewpageradapter_circle(imageviewlist, this); 
    viewpager.setadapter(myviewpageradapter); 
  } 
  /** 
   * 设置圆点的 
   */ 
  private void initpoint() { 
    iv_one = (imageview) findviewbyid(r.id.iv_one); 
    iv_two = (imageview) findviewbyid(r.id.iv_two); 
    iv_three = (imageview) findviewbyid(r.id.iv_three); 
    imageviews = new arraylist<>(); 
    imageviews.add(iv_one); 
    imageviews.add(iv_two); 
    imageviews.add(iv_three); 
    iv_one.setselected(true);//设置第一个圆点为true 
    /**viewpager监听事件*/ 
    viewpager.addonpagechangelistener(new viewpager.onpagechangelistener() { 
      /**滑动时调用*/ 
      @override 
      public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels) { 
      } 
      /**停止时调用*/ 
      @override 
      public void onpageselected(int position) { 
        imageviews.get(position).setselected(true);//滑动后颜色改变 
        imageviews.get(oldpostion).setselected(false);//变为初始颜色 
        oldpostion = position;//把滑动后的圆点的position给原来的oldpostion 
      } 
        /**滑动状态改变时调用*/ 
      @override 
      public void onpagescrollstatechanged(int state) { 
      } 
    }); 
  } 
} 

以上所述是小编给大家介绍的android 使用viewpager实现轮播图效果,希望对大家有所帮助