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

Android使用Recyclerview实现图片水平自动循环滚动效果

程序员文章站 2023-12-01 17:26:40
简介: 本篇博客主要介绍的是如何使用recyclerview实现图片水平方向自动循环(跑马灯效果) 效果图:    思路: 1.准备m张图片...

简介:

本篇博客主要介绍的是如何使用recyclerview实现图片水平方向自动循环(跑马灯效果)

效果图:   

Android使用Recyclerview实现图片水平自动循环滚动效果

思路:

1.准备m张图片

1.使用recyclerview实现,返回无数个(实际interge.maxvalue)item,第n个item显示第n%m张图片

3.使用recyclerview.scrollby  每个一段时间水平滚动一段距离

4.通过layoutmanager.findfirstvisibleitemposition()获取当前显示的第一个view是第几个item,上面的imageview显示对应de图片

实现代码:

xml文件

1.activity布局文件activity_recy.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"
  android:orientation="vertical">
  <imageview
    android:layout_width="300dp"
    android:layout_height="350dp"
    android:layout_gravity="center_horizontal"
    android:id="@+id/img"
    android:src="@drawable/p5"
    android:scaletype="fitxy"
    />

    <android.support.v7.widget.recyclerview
      android:layout_width="match_parent"
      android:layout_height="150dp"
      android:layout_margintop="10dp"
      android:id="@+id/recyclerview"
      ></android.support.v7.widget.recyclerview>



</linearlayout>

2.适配器布局item_horizon.xml  

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="match_parent"
  android:orientation="vertical"

  >
  <imageview
    android:layout_width="100dp"
    android:layout_height="match_parent"
    android:id="@+id/img"
    android:layout_marginleft="5dp"
    android:layout_marginright="5dp"
    android:scaletype="fitxy"
    />

</linearlayout>

activity  

public class horizontalactivity extends appcompatactivity implements recyadapter.onitemclicklistener {
  private string tag="horizontalactivity";

  @bindview(r.id.img)
  imageview img;
  @bindview(r.id.recyclerview)
  recyclerview recyclerview;


  private integer[] mimgids = {r.drawable.p1, r.drawable.p2, r.drawable.p3, r.drawable.p4, r.drawable.p5,
      r.drawable.pic1, r.drawable.pic5, r.drawable.pic6};
  private list<integer> datas;
  private recyadapter recyadapter;
  private handler mhandler=new handler();
  private linearlayoutmanager layoutmanager;

  private int olditem=0;


  @override
  protected void oncreate(@nullable bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_recy);
    butterknife.bind(this);
    initdata();
    initrecy();
    img.setimageresource(datas.get(0));
    recyadapter.setonitemclicklistener(this);
  }


  runnable scrollrunnable=new runnable() {
    @override
    public void run() {
      recyclerview.scrollby(3,0);

//      int firstitem = layoutmanager.findfirstvisibleitemposition();
      int firstitem=layoutmanager.findfirstvisibleitemposition();
      if(firstitem!=olditem&&firstitem>0){
        olditem=firstitem;
        img.setimageresource(datas.get(olditem%datas.size()));
      }

      log.e(tag, "run: firstitem:"+firstitem );


      mhandler.postdelayed(scrollrunnable,10);
    }
  };

  private void initrecy() {
    recyadapter=new recyadapter(this,datas);

    layoutmanager = new linearlayoutmanager(this);
    layoutmanager.setorientation(linearlayoutmanager.horizontal);
    recyclerview.setlayoutmanager(layoutmanager);
    recyclerview.setadapter(recyadapter);
  }

  private void initdata() {
    datas=new arraylist<>();
    for (int i = 0; i <mimgids.length ; i++) {
      datas.add(mimgids[i]);
    }
  }

  @override
  protected void onresume() {
    super.onresume();
    mhandler.postdelayed(scrollrunnable,10);
  }

  @override
  protected void onstop() {
    super.onstop();
    mhandler.removecallbacks(scrollrunnable);
  }

  @override
  public void onitemclick(view view, int tag) {
    toast.maketext(this,"第"+tag+"张图片被点击了",toast.length_short).show();
  }
}

适配器recyadapter

public class recyadapter extends recyclerview.adapter<recyadapter.viewholder> implements view.onclicklistener {

  private context context;
  private list<integer> datas;
  private onitemclicklistener onitemclicklistener;

  public recyadapter(context context, list<integer> datas) {
    this.context = context;
    this.datas = datas;
  }

  @override
  public viewholder oncreateviewholder(viewgroup parent, int viewtype) {
    view view = layoutinflater.from(context).inflate(r.layout.item_horizontal, parent, false);
    viewholder vh=new viewholder(view);
    view.setonclicklistener(this);
    return vh;
  }


  @override
  public void onbindviewholder(viewholder holder, int position) {
    int newpos=position%datas.size();

    holder.img.setimageresource(datas.get(newpos));

    holder.itemview.settag(position);


  }

  @override
  public int getitemcount() {
    return integer.max_value;
  }

  @override
  public void onclick(view view) {
    if(onitemclicklistener!=null){
      onitemclicklistener.onitemclick(view, (integer) view.gettag());
    }
  }


  class viewholder extends recyclerview.viewholder {
     imageview img;
    public viewholder(view itemview) {
      super(itemview);
      img=itemview.findviewbyid(r.id.img);
    }
  }

  public void setonitemclicklistener(onitemclicklistener listener){
    this.onitemclicklistener=listener;

  }
  interface onitemclicklistener{
    void onitemclick(view view,int tag);
  }

}

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