美文网首页Android-recyclerview
用recyclerview实现跑马灯文字无限轮播

用recyclerview实现跑马灯文字无限轮播

作者: 莫不如哦 | 来源:发表于2020-11-16 10:41 被阅读0次

    网上找了一些跑马灯实现都是直接重写的textview不停的改变绘制文字的位置来达到跑马灯效果,但是有的时候需求要求来回滚动多条信息,用textview实现的就不尽人意了,于是用recyclerview整理了一个
    思路就是利用recyclerview的scrollBy不停的滚动,来达到效果

    recyclerview代码

    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.MotionEvent;
    
    import androidx.annotation.Nullable;
    import androidx.recyclerview.widget.LinearLayoutManager;
    import androidx.recyclerview.widget.RecyclerView;
    
    import com.cy.utils.LogUtil;
    
    import java.lang.ref.WeakReference;
    
    public class AutoPollRecyclerView extends RecyclerView {
        private static final long TIME_AUTO_POLL = 10;
        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) {
                    LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                    int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
                    if (lastVisibleItemPosition == (Integer.MAX_VALUE - 1))//如果真有人等到一直滚动到最大值那就让他回到头部重新滚
                        recyclerView.scrollToPosition(0);
                    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 false;
        }
    }
    
    

    其中要注意recyclerview的adapter里面的getItemCount方法要写成最大值,

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

    相关文章

      网友评论

        本文标题:用recyclerview实现跑马灯文字无限轮播

        本文链接:https://www.haomeiwen.com/subject/lzyabktx.html