Android 【实现自动轮询的RecycleView】

Android 【实现自动轮询的RecycleView】

需求:类似医院或者商场,大屏幕无限轮播item (广告词/广告图…)

代码如下

/** * Created by Xia_焱 on 2017/8/20. */

public class AutoPollRecyclerView extends RecyclerView {
   
    private static final long TIME_AUTO_POLL = 32;
    AutoPollTask autoPollTask;
    private boolean running; //标示是否正在自动轮询
    private boolean canRun;//标示是否可以自动轮询,可在不需要的是否置false
    public AutoPollRecyclerView(Context context, @Nullable AttributeSet attrs) {
   
        super(context, attrs);
        autoPollTask = new AutoPollTask(this);
    }
    static class AutoPollTask implements Runnable {
   
        private final WeakReference<AutoPollRecyclerView> mReference;
        //使用弱引用持有外部类引用->防止内存泄漏
        public AutoPollTask(AutoPollRecyclerView reference) {
   
            this.mReference = new WeakReference<AutoPollRecyclerView>(reference);
        }
        @Override
        public void run() {
   
            AutoPollRecyclerView recyclerView = mReference.get();
            if (recyclerView != null &&  recyclerView.running &&recyclerView.canRun) {
   
                recyclerView.scrollBy(2, 2);
                recyclerView.postDelayed(recyclerView.autoPollTask,recyclerView.TIME_AUTO_POLL);
            }
        }
    }
    //开启:如果正在运行,先停止->再开启
    public void start() {
   
        if (running)
            stop();
        canRun = true;
        running = true;
        postDelayed(autoPollTask,TIME_AUTO_POLL);
    }
    public void stop(){
   
        running = false;
        removeCallbacks(autoPollTask);
    }
    @Override
    public boolean onTouchEvent(MotionEvent e) {
   
        switch (e.getAction()){
   
            case MotionEvent.ACTION_DOWN:
                if (running)
                    stop();
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_OUTSIDE:
                if (canRun)
                    start();
                break;
        }
        return super.onTouchEvent(e);
    }
}

开启:如果正在运行,先停止->再开启

  public void start() {
   
        if (running)
            stop();
        canRun = true;
        running = true;
        postDelayed(autoPollTask,TIME_AUTO_POLL);
    }
    public void stop(){
   
        running = false;
        removeCallbacks(autoPollTask);
    }
    @Override
    public boolean onTouchEvent(MotionEvent e) {
   
        switch (e.getAction()){
   
            case MotionEvent.ACTION_DOWN:
                if (running)
                    stop();
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_OUTSIDE:
                if (canRun)
                    start();
                break;
        }
        return super.onTouchEvent(e);
    }
}

Adapter中的代码如下

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
   
        String data = mData.get(position%mData.size());
        holder.setText(R.id.tv_content,data);
    }
    @Override
    public int getItemCount() {
   
        return Integer.MAX_VALUE;
    }

Activity中的代码

  mRecyclerView.setAdapter(adapter);
        if (true) //保证itemCount的总个数宽度超过屏幕宽度->自己处理
            mRecyclerView.start();

[希望这篇文章可以帮到你]

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/2826.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • keyvaluepair_c# KeyValuePair用法「建议收藏」

    keyvaluepair_c# KeyValuePair用法「建议收藏」privateKeyValuePairSetKeyValuePair(){intintKey=1;stringstrValue=”Myvalue”;KeyValuePairkvp=newKeyValuePair(intKey,strValue);returnkvp;}//////获得键/值对///privatevoidGetKeyValuePairDemo(…

    2022年7月26日
    59
  • Git 当前项目设置 用户名、邮箱「建议收藏」

    Git 当前项目设置 用户名、邮箱「建议收藏」Git全局配置和单个仓库的用户名邮箱配置学习git的时候,大家刚开始使用之前都配置了一个全局的用户名和邮箱$gitconfig–globaluser.name“github’sName”$gitconfig–globaluser.email“github@xx.com”$gitconfig–list如果你公司的项目是放在自建的gitlab上面,如…

    2025年9月29日
    3
  • 基于Python 输出字符HelloWorld简单总结

    基于Python 输出字符HelloWorld简单总结根据简明教程:源文件#!/usr/bin/python#Filename:helloworld.pyprint'HelloWorld'运行程序,打开shell键入命

    2022年7月6日
    30
  • linux添加路由网关_linux删除默认网关

    linux添加路由网关_linux删除默认网关1、以前经常使用route命令添加和删除路由添加网关/设置网关:routeadd-net192.100.10.0netmask255.255.255.0deveth0#增加一条到达192.100.10.0的路由。屏蔽一条路由:routeadd-net192.100.10.0netmask255.255.255.0reject#增加一…

    2022年10月5日
    3
  • python 实现 跳一跳游戏 代码解析

    python 实现 跳一跳游戏 代码解析这个代码实现的是手动点击起点和终点,程序自动判断距离、触屏时间完成跳跃原理(摘自项目说明页面):1.将手机点击到“跳一跳”小程序界面;2.用Adb工具获取当前手机截图,并用adb将截

    2022年7月6日
    40
  • 谷歌浏览器油猴插件安装教程(超详细),让你的浏览器更加强大[通俗易懂]

    谷歌浏览器油猴插件安装教程(超详细),让你的浏览器更加强大[通俗易懂]什么是扩展程序首先,你要知道油猴是什么的话,那你就必须知道谷歌浏览器的扩展程序是什么?点解浏览器右上角–>更多工具–>扩展程序,进入我们的扩展程序界面,在这里可以看到我们已经安装的扩展程序。那么扩展程序可以做什么呢?举个例子,像我安装的Infinity新标签页,就可以实现以下功能,将浏览器原本的新标签页样式变成这个样子那么油猴是干什么的呢其实,油猴就是属于我们上面说的扩…

    2022年7月14日
    197

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号