Android RecyclerView一个一个自动滚动,无限循环,聊天列表效果,跑马灯效果

实现效果

实现效果

看到这个效果,看到很多人用ViewFlipper实现,但是效果并不理想,于是我想到用RecyclerView试试。

主要是监听recyclerView滑动,开始和暂停来控制,废话不多说,上代码。

自定义RecyclerView

public class AutoPollRecyclerViewextends RecyclerView {

private static final long TIME_AUTO_POLL =16;

    private static final long TIME_AUTO_POLL_1 =2000;

    AutoPollTaskautoPollTask;

    AutoPollTask1autoPollTask1;

    private int index =0;

    private boolean running; //标示是否正在自动轮询

    private boolean canRun;//标示是否可以自动轮询,可在不需要的是否置false

    private final int mTouchSlop;

    public AutoPollRecyclerView(Context context, @Nullable AttributeSet attrs) {

super(context, attrs);

        // autoPollTask = new AutoPollTask(this);

        autoPollTask1 =new AutoPollTask1(this);

        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();

    }

/**

* 持续滑动(走马灯)

*/

    static class AutoPollTaskimplements Runnable {

private final WeakReferencemReference;

        //使用弱引用持有外部类引用->防止内存泄漏

        public AutoPollTask(AutoPollRecyclerView reference) {

this.mReference =new WeakReference(reference);

        }

@Override

        public void run() {

Log.e("AutoPollRecyclerView", System.currentTimeMillis() +"");

            AutoPollRecyclerView recyclerView =mReference.get();

            if (recyclerView !=null && recyclerView.running && recyclerView.canRun) {

recyclerView.scrollBy(2, 2);

                recyclerView.postDelayed(recyclerView.autoPollTask, recyclerView.TIME_AUTO_POLL);

            }

}

}

/***

* 一次只能滑一个item(轮播图)

*/

    static class AutoPollTask1implements Runnable {

private final WeakReferencemReference;

        //使用弱引用持有外部类引用->防止内存泄漏

        public AutoPollTask1(AutoPollRecyclerView reference) {

this.mReference =new WeakReference(ref

  • 5
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值