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

Recycleview实现无限自动轮播

程序员文章站 2022-07-04 17:03:40
概述recycleview实现特定数据无限重复滑动在我看来不外乎有两种方法1.修改adpter的复用机制,无限复用数据2.在adpter中返回数据长度返回integer的最大值由于第一种虽然能实现数据...

概述

recycleview实现特定数据无限重复滑动在我看来不外乎有两种方法

1.修改adpter的复用机制,无限复用数据
2.在adpter中返回数据长度返回integer的最大值

由于第一种虽然能实现数据的无限重复但是数据位还是没有任何变化,所以在自动跳转至最后的时候无法在向下一位轮播,所以在这里我使用第二种方式实现自动轮播

简单讲述修改adpter的复用机制

我们拿linearlayoutmanager线性的为例子,我们只需要重新linearlayoutmanager在绘制的时候做一些手手脚就可以实现

package com.li.liproject.recycle;

import android.content.context;
import android.util.attributeset;
import android.util.displaymetrics;
import android.util.log;
import android.view.view;
import android.view.viewgroup;

import androidx.recyclerview.widget.linearlayoutmanager;
import androidx.recyclerview.widget.linearsmoothscroller;
import androidx.recyclerview.widget.recyclerview;

/**
 * @author 版本:1.0
 * 创建日期:2020/4/14 14
 * 描述:
 */
public class scrollspeedlinearlayoutmanger extends linearlayoutmanager {
 public scrollspeedlinearlayoutmanger(context context) {
  super(context);
 }

 public scrollspeedlinearlayoutmanger(context context, int orientation, boolean reverselayout) {
  super(context, orientation, reverselayout);
 }

 public scrollspeedlinearlayoutmanger(context context, attributeset attrs, int defstyleattr, int defstyleres) {
  super(context, attrs, defstyleattr, defstyleres);
 }

 @override
 public recyclerview.layoutparams generatedefaultlayoutparams() {
  return new recyclerview.layoutparams(viewgroup.layoutparams.wrap_content,viewgroup.layoutparams.wrap_content);
 }

 // 1 在recyclerview初始化时,会被调用两次。
// 2 在调用adapter.notifydatasetchanged()时,会被调用。
// 3 在调用setadapter替换adapter时,会被调用。
// 4 在recyclerview执行动画时,它也会被调用。
 @override
 public void onlayoutchildren(recyclerview.recycler recycler, recyclerview.state state) {
  log.d("tag","onlayoutchildren ");
  if (getitemcount() == 0){
   detachandscrapattachedviews(recycler);
   return;
  }
  //state.isprelayout()是支持动画的
  if (getitemcount() == 0 && state.isprelayout()){
   return;
  }
  //将当前recycler中的view全部移除并放到报废缓存里,之后优先重用缓存里的view
  detachandscrapattachedviews(recycler);

  int actualheight = 0;
  for (int i = 0 ;i < getitemcount() ; i++){
   view scrap = recycler.getviewforposition(i);
   addview(scrap);
   measurechildwithmargins(scrap,0,0);
   int width = getdecoratedmeasuredwidth(scrap);
   int height = getdecoratedmeasuredheight(scrap);
   layoutdecorated(scrap,0,actualheight,width,actualheight+height);
   actualheight+=height;
   //超出界面的就不画了,也不add了
   if (actualheight > getheight()){
    break;
   }
  }
 }

 @override
 public boolean canscrollvertically() {
  return true;
 }


 @override
 public int scrollverticallyby(int dy, recyclerview.recycler recycler, recyclerview.state state) {
  log.d("feifeifei","getchildcount() " + getchildcount() + " recycler.getscraplist().size() " + recycler.getscraplist().size());

  //界面向下滚动的时候,dy为正,向上滚动的时候dy为负

  //填充
  fill(dy,recycler,state);
  //滚动
  offsetchildrenvertical(dy*-1);

  //回收已经离开界面的
  recycleout(dy,recycler,state);

  return dy;
 }

 private void fill(int dy, recyclerview.recycler recycler, recyclerview.state state){
  //向下滚动
  if (dy > 0){
   //先在底部填充
   view lastview = getchildat(getchildcount() -1);
   int lastpos = getposition(lastview);
   if (lastview.getbottom() - dy < getheight()){
    view scrap;
    if (lastpos == getitemcount() -1){
     scrap = recycler.getviewforposition(0);
    }else {
     scrap = recycler.getviewforposition(lastpos+1);
    }
    addview(scrap);
    measurechildwithmargins(scrap,0,0);
    int width = getdecoratedmeasuredwidth(scrap);
    int height = getdecoratedmeasuredheight(scrap);
    layoutdecorated(scrap,0,lastview.getbottom(),width,lastview.getbottom()+height);
   }
  }else {
   //向上滚动
   //现在顶部填充
   view firstview = getchildat(0);
   int layoutpostion = getposition(firstview);

   if (firstview.gettop() >= 0 ){
    view scrap ;
    if (layoutpostion == 0){
     scrap = recycler.getviewforposition(getitemcount()-1);
    }else {
     scrap = recycler.getviewforposition(layoutpostion -1);
    }
    addview(scrap,0);
    measurechildwithmargins(scrap,0,0);
    int width = getdecoratedmeasuredwidth(scrap);
    int height = getdecoratedmeasuredheight(scrap);
    layoutdecorated(scrap,0,firstview.gettop() - height,width,firstview.gettop());
   }
  }
 }

 private void recycleout(int dy, recyclerview.recycler recycler, recyclerview.state state){
  for (int i = 0 ; i <getchildcount() ;i++){
   view view = getchildat(i);
   if (dy >0){
    if (view.getbottom()-dy <0){
     log.d("feifeifei","recycleout " + i);
     removeandrecycleview(view,recycler);
    }
   }else {
    if (view.gettop()-dy > getheight()){
     log.d("feifeifei","recycleout " + i);
     removeandrecycleview(view,recycler);
    }
   }
  }
 }

 @override
 public void smoothscrolltoposition(recyclerview recyclerview, recyclerview.state state, int position) {
  recyclerview.smoothscroller smoothscroller = new centersmoothscroller(recyclerview.getcontext());
  smoothscroller.settargetposition(position);
  startsmoothscroll(smoothscroller);
 }

 private class centersmoothscroller extends linearsmoothscroller {
  public centersmoothscroller(context context) {
   super(context);
  }
  protected float calculatespeedperpixel(displaymetrics displaymetrics) {
   return 0.2f;
  }
 }
}

大概就是这么写的网格的需要自己重新写因为计算会有区别,这里简单的讲述一下

正题

adpter适配器的实现

写法没什么区别唯一的getitemcount 和onbindviewholder要做一下处理大概如下

public class adauditoradapter extends recyclerview.adapter<adauditoradapter.myviewholder> {
 private context mcontext;
 private list<string> mdata;

 public adauditoradapter(context mcontext, list<string> mdata) {
  this.mcontext = mcontext;
  this.mdata = mdata;
 }

 @nonnull
 @override
 public myviewholder oncreateviewholder(@nonnull viewgroup viewgroup, int i) {
  myviewholder holder = new myviewholder(layoutinflater.from(mcontext).inflate(r.layout.item_adauditor, viewgroup, false));
  return holder;
 }

 @override
 public void onbindviewholder(@nonnull myviewholder myviewholder, @suppresslint("recyclerview") int i) {
  log.e("hhhhhhhhh", "onbindviewholder: -->"+i );
  //取余否则会出现索引越界
  myviewholder.tv_1.settext(mdata.get(i%mdata.size()));
  myviewholder.itemview.setonclicklistener(new view.onclicklistener() {
   @override
   public void onclick(view v) {
    listener.onclick(v, i%mdata.size(), 3);
   }
  });

 }


 @override
 public int getitemcount() {
 //返回adpter最大值
  return integer.max_value;
 }

 public void update(list<string> list) {
  this.mdata = list;
  notifydatasetchanged();
 }

 class myviewholder extends recyclerview.viewholder {
  textview tv_1;


  public myviewholder(@nonnull view itemview) {
   super(itemview);

   tv_1 = itemview.findviewbyid(r.id.tv_1); //id

  }
 }

 public myclicklistener getlistener() {
  return listener;
 }

 public void setmyclicklistener(myclicklistener listener) {
  this.listener = listener;
 }

 myclicklistener listener;

 public interface myclicklistener {
  void onclick(view view, int position, int type);
 }

 public list<string> getdata() {
  return mdata;
 }
}

activity的实现

1基本实现

1.1 添加假数据写好点击事件
1.2 用handler延迟发消息 mrecyclerview.smoothscrolltoposition(position);移动到指定位置
1.3 点击停止移动

2效果优化

2.1 添加匀速阻尼效果
2.2 实现无限轮播考虑数值超过integer最大值情况
2.3 点击正在轮播时的recycleview会停止轮播,再次点击才会执行点击事件(优化为点击停止并执行点击事件)
阻尼效果就是减少滑动速率

我们这么做

package com.li.liproject.recycle;

import android.content.context;
import android.util.attributeset;
import android.util.displaymetrics;
import android.util.log;
import android.view.view;
import android.view.viewgroup;

import androidx.recyclerview.widget.gridlayoutmanager;
import androidx.recyclerview.widget.linearsmoothscroller;
import androidx.recyclerview.widget.recyclerview;

/**
 * @author 版本:1.0
 * 创建日期:2020/4/14 14
 * 描述:
 */
public class scrollspeedgridlayoutmanager1 extends gridlayoutmanager {


 public scrollspeedgridlayoutmanager1(context context, attributeset attrs, int defstyleattr, int defstyleres) {
  super(context, attrs, defstyleattr, defstyleres);
 }

 public scrollspeedgridlayoutmanager1(context context, int spancount) {
  super(context, spancount);
 }

 public scrollspeedgridlayoutmanager1(context context, int spancount, int orientation, boolean reverselayout) {
  super(context, spancount, orientation, reverselayout);
 }


 @override
 public void smoothscrolltoposition(recyclerview recyclerview, recyclerview.state state, int position) {
  recyclerview.smoothscroller smoothscroller = new centersmoothscroller(recyclerview.getcontext());
  smoothscroller.settargetposition(position);
  startsmoothscroll(smoothscroller);
 }

 private class centersmoothscroller extends linearsmoothscroller {
  public centersmoothscroller(context context) {
   super(context);
  }
  protected float calculatespeedperpixel(displaymetrics displaymetrics) {
   return 10f;//滑动速率问题
  }
 }
}

activity全部代码

package com.li.liproject.recycle;

import android.annotation.suppresslint;
import android.os.bundle;
import android.os.handler;
import android.os.message;
import android.util.log;
import android.view.motionevent;
import android.view.view;
import android.widget.toast;

import androidx.annotation.nonnull;
import androidx.annotation.nullable;
import androidx.appcompat.app.appcompatactivity;
import androidx.recyclerview.widget.gridlayoutmanager;
import androidx.recyclerview.widget.linearlayoutmanager;
import androidx.recyclerview.widget.recyclerview;

import com.li.liproject.mainactivity;
import com.li.liproject.r;

import java.lang.ref.weakreference;
import java.util.arraylist;
import java.util.list;

/**
 * @author 版本:1.0
 * 创建日期:2020/4/14 14
 * 描述:
 */
public class recycleviewactivity extends appcompatactivity {
 static recyclerview rv_1;
 private static int handler_msg = 0x0011;
 private static int handler_long_msg = 0x0021;
 static int position = 0;
 static int addnum = 3;
 @suppresslint("handlerleak")
 private static handler handler = new handler() {
  @override
  public void handlemessage(@nonnull message msg) {
   if (msg.what == handler_msg) {
   // 9宫格效果实现速率相同
    if (addnum==3){
     position = position + addnum;
     addnum = 6;
    }else {
     position = position + addnum;
     addnum = 3;
    }
    log.e("tag", "handlemessage: -->" + position);
    smoothmovetoposition(rv_1, position >= 0 ? position : 0);
    if (position==integer.max_value/2){
     //点击或超过2分之integer.max_valu重置adpter
     longautomove();
    }else {
     automove();
    }

   }else if (msg.what==handler_long_msg){
    position = 0;
    addnum = 3;
    log.e("tag", "handlemessage: -->" + position);
    smoothmovetoposition(rv_1, 0);
    automove();

   }
  }


 };

 private static adauditoradapter adauditoradapter;
 static list<string> strings;
 @override
 protected void oncreate(@nullable bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_recycle);
  rv_1 = findviewbyid(r.id.rv_1);
  strings = new arraylist<>();
  for (int i = 0; i < 50; i++) {
   strings.add(i + "");
  }
  adauditoradapter = new adauditoradapter(this, strings);
  adauditoradapter.setmyclicklistener(new adauditoradapter.myclicklistener() {
   @override
   public void onclick(view view, int position, int type) {
    toast.maketext(recycleviewactivity.this, adauditoradapter.getdata().get(position), toast.length_short).show();
    stopmove();
   }
  });
  gridlayoutmanager layoutmanager = new scrollspeedgridlayoutmanager1(this,3,gridlayoutmanager.horizontal, false);
  rv_1.setlayoutmanager(layoutmanager);
  rv_1.setadapter(adauditoradapter);
  rv_1.addonscrolllistener(new recyclerview.onscrolllistener() {
   @override
   public void onscrollstatechanged(recyclerview recyclerview, int newstate) {
    super.onscrollstatechanged(recyclerview, newstate);
//    if (mshouldscroll && recyclerview.scroll_state_idle == newstate) {
//     mshouldscroll = false;
//     smoothmovetoposition(recyclerview, mtoposition);
//    }
    log.e("tag", "onscrollstatechanged11111111: -->" + newstate);
    if (newstate == 1) {
//     recyclerview.viewholder holder = recyclerview.getchildviewholder(recyclerview.getrootview());
     recyclerview.setonclicklistener(new view.onclicklistener() {
      @override
      public void onclick(view v) {
       toast.maketext(recycleviewactivity.this, adauditoradapter.getdata().get(position)+"........", toast.length_short).show();
      }
     });
     stopmove();
     longautomove();
    }
   }

//   @override
//   public void onscrolled(@nonnull recyclerview recyclerview, int dx, int dy) {
//    super.onscrolled(recyclerview, dx, dy);
//    log.e("tag", "onscrolled: dx=" +dx +" dy="+dy );
//   }
  });
  rv_1.setontouchlistener(new view.ontouchlistener() {
   @override
   public boolean ontouch(view v, motionevent event) {
    if (event.getaction()==motionevent.action_down){
     //监测点击位置找到view实现点击事件
     view childview = rv_1.findchildviewunder(event.getx(), event.gety());
     log.e("ggggggggggggggggg", "ontouch: -->"+rv_1.getchildlayoutposition(childview));
     adauditoradapter.getlistener().onclick(v, rv_1.getchildlayoutposition(childview),1);
    }
    return false;
   }
  });
  automove();
 }


 private static void automove() {
  handler.removemessages(handler_msg);
  handler.sendemptymessagedelayed(handler_msg, 2000);
 }

 private static void longautomove() {
  if (handler.hasmessages(handler_msg)) {
   handler.removemessages(handler_long_msg);
  }
  handler.sendemptymessagedelayed(handler_long_msg, 5000);
 }

 public static void stopmove() {
  if (handler.hasmessages(handler_msg)) {
   handler.removemessages(handler_msg);
  }
 }


 //目标项是否在最后一个可见项之后
 private static boolean mshouldscroll;
 //记录目标项位置
 private static int mtoposition;

 /**
  * 滑动到指定位置
  */
 private static void smoothmovetoposition(recyclerview mrecyclerview, final int position) {
  if (position==0){
   mrecyclerview.setadapter(adauditoradapter);
  }
   mrecyclerview.smoothscrolltoposition(position);
   mtoposition = position;
   mshouldscroll = true;
 }

 @override
 protected void ondestroy() {
  super.ondestroy();
  if (handler!=null){
   handler.removecallbacksandmessages(null);
   handler= null;
  }
  if (adauditoradapter!=null) {
   adauditoradapter= null;
  }
 }
}

自动轮播效果基本实现

这里的demo只写了大概的效果还有很多的东西需要优化一下,才能拿到项目中使用

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

相关标签: Recycleview 轮播