zoukankan      html  css  js  c++  java
  • 自动轮询的recycleView

    import android.content.Context;
    import android.support.v7.widget.RecyclerView;
    import android.util.AttributeSet;
    import android.view.MotionEvent;
    import com.shiwen.oil.util.LoggerUtil;
    import java.lang.ref.WeakReference;
    
    
    public class AutoRecyclerView extends RecyclerView {
        private static final long TIME_AUTO_POLL = 16;
        AutoPollTask autoPollTask;
        private boolean running; //标示是否正在自动轮询
        private boolean canRun;//标示是否可以自动轮询,可在不需要的是否置false
        public AutoRecyclerView(Context context,  AttributeSet attrs) {
            super(context, attrs);
            autoPollTask = new AutoPollTask(this);
        }
        static class AutoPollTask implements Runnable {
            private final WeakReference<AutoRecyclerView> mReference;
            //使用弱引用持有外部类引用->防止内存泄漏
            public AutoPollTask(AutoRecyclerView reference) {
                this.mReference = new WeakReference<AutoRecyclerView>(reference);
            }
            @Override
            public void run() {
                AutoRecyclerView 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_MOVE:
                    if (running){
                        stop();
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    if (canRun)
                        start();
                    break;
                case MotionEvent.ACTION_CANCEL:
                case MotionEvent.ACTION_OUTSIDE:
                    if (canRun)
                        start();
                    break;
            }
            return super.onTouchEvent(e);
        }
    }
  • 相关阅读:
    CodeForces 173B Chamber of Secrets spfa
    CodeForces 173A Rock-Paper-Scissors 数学
    Codeforces Gym 100803G Flipping Parentheses 线段树+二分
    Codeforces Gym 100803D Space Golf 物理题
    Codeforces Gym 100803F There is No Alternative 暴力Kruskal
    Codeforces Gym 100803C Shopping 贪心
    《白日梦想家》观后感
    select sum也会返回null值
    mysql update更新带子查询的实现方式
    mysql 添加索引 mysql 如何创建索引
  • 原文地址:https://www.cnblogs.com/loaderman/p/9679827.html
Copyright © 2011-2022 走看看