2021

利用RecycleView实现跑马灯和轮播图效果

2018-10-11  本文已影响233人  digtal_

先看下效果图


ezgif-5-9b723830ee12.gif
ezgif-5-1082f96abe37.gif ezgif-5-08381d40188b.gif

利用Recycleview实现这个效果最主要的一个方法就是这个方法

        mSnapHelper = new LinearSnapHelper();
        mSnapHelper.attachToRecyclerView(mRecyclerView);

这个方法的作用是使得RecycelView像ViewPager一样的效果,一次只能滑一页,而且居中显示。
然后利用Handler来实现无限轮播的效果,在Adapter中要注意

@Override
    public int getItemCount() {
        return Integer.MAX_VALUE;
    }
 Glide.with(mContext).load(mDatas.get(position % mDatas.size())).into(mImageView);

getItemCount中返回一个无穷大达到无限录播的效果,在填充数据时取余防止索引越界
然后自定义Layoutmanger

public class MyBannerManger extends LinearLayoutManager {

    private RecyclerView mRecyclerView;
    private final InterValHander mHander;
    private static final int INTERVAL = 3000;
    private boolean mIsLayoutComplete = false;
    private static int position = 1;
    private LinearSnapHelper mSnapHelper;

    public MyBannerManger(Context context, RecyclerView recyclerView) {
        super(context);
        setOrientation(HORIZONTAL);
        mRecyclerView = recyclerView;
        mHander = new InterValHander(this);
        mRecyclerView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction()  == MotionEvent.ACTION_DOWN){
                    mHander.removeCallbacksAndMessages(null);
                }else if(event.getAction()  == MotionEvent.ACTION_UP){
                    mHander.sendEmptyMessageDelayed(position,INTERVAL);
                }
                return false;
            }
        });
    }

    @Override
    public void onAttachedToWindow(RecyclerView view) {
        super.onAttachedToWindow(view);
        mSnapHelper = new LinearSnapHelper();
        mSnapHelper.attachToRecyclerView(mRecyclerView);
    }

    @Override
    public void onLayoutCompleted(RecyclerView.State state) {
        super.onLayoutCompleted(state);
        if (!mIsLayoutComplete) {
            mHander.sendEmptyMessageDelayed(position,INTERVAL);
        }

        mIsLayoutComplete = true;
    }

    private RecyclerView getRecyclerView(){
        return mRecyclerView;
    }

    static class InterValHander extends Handler {

        private WeakReference<MyBannerManger> mWeakReference;
        private MyBannerManger mBannerManger;

        public InterValHander(MyBannerManger myBannerManger) {
            mWeakReference = new WeakReference<>(myBannerManger);
            mBannerManger = myBannerManger;
        }

        @Override
        public void handleMessage(Message msg) {
            LogUtils.LogE(msg.toString());
            mBannerManger.getRecyclerView().smoothScrollToPosition(position);
            sendEmptyMessageDelayed(0,INTERVAL);
            position++;

        }
    }


    @Override
    public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
        LogUtils.LogE("smoothScrollToPosition");
        LinearSmoothScroller smoothScroller =
                new LinearSmoothScroller(recyclerView.getContext()) {
                    // 返回:滑过1px时经历的时间(ms)。
                    @Override
                    protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
                        float f = 150f / displayMetrics.densityDpi;
                        LogUtils.LogE("F = "+f);
                        return 0.5f;
                    }
                };

        smoothScroller.setTargetPosition(position);
        startSmoothScroll(smoothScroller);
    }

    @Override
    public void onScrollStateChanged(int state) {
        if(state == SCROLL_STATE_IDLE){
            position = getPosition(mSnapHelper.findSnapView(this))+1;
        }

    }

public class RActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private String[] mStrings = {"南京南站","龙眠大道","学则路","仙鹤门","仙鹤名苑","紫东国际创意园!"};

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_r);
        mRecyclerView = findViewById(R.id.rv);
        List<String> list = Arrays.asList(Images.imageUrls);
        mRecyclerView.setLayoutManager(new MyBannerManger(this,mRecyclerView));
        RAdapter rAdapter = new RAdapter(this, list);
        mRecyclerView.setAdapter(rAdapter);
    }
}

上一篇下一篇

猜你喜欢

热点阅读