美文网首页UIandroid样式主题合集新视频开发
8.Android RecycleView实现抖音翻页上下滑

8.Android RecycleView实现抖音翻页上下滑

作者: 鹏城十八少 | 来源:发表于2021-07-02 08:06 被阅读0次

    今天开始讲RecycleView的系列教程。分割线,分组,局部刷新,动态添加,缓存原理,抖音效果,瀑布流。嵌套,动画等等

    分析:

    1.滑动过半的判断PagerSnapHelper

    2.滑动过半添加在哪,通过自定义layoutManager

    3.得到滑动的view,和position

    4.初始化和销毁的时机  recycleVIewchild销毁

    监听滑动的位置:

    publicvoidonScrollStateChanged(intstate) {

    switch(state) {

    caseRecyclerView.SCROLL_STATE_IDLE:

    ViewviewIdle=mPagerSnapHelper.findSnapView(this);

    intpositionIdle=getPosition(viewIdle);

    if(mOnViewPagerListener!=null&&getChildCount()==1) {

    mOnViewPagerListener.onPageSelected(positionIdle,positionIdle==getItemCount()-1);

                    }

    break;

    caseRecyclerView.SCROLL_STATE_DRAGGING:

    ViewviewDrag=mPagerSnapHelper.findSnapView(this);

    intpositionDrag=getPosition(viewDrag);

    break;

    caseRecyclerView.SCROLL_STATE_SETTLING:

    ViewviewSettling=mPagerSnapHelper.findSnapView(this);

    intpositionSettling=getPosition(viewSettling);

    break;

            }

        }

    监听recycleVIew子child销毁

    privateRecyclerView.OnChildAttachStateChangeListenermChildAttachStateChangeListener=newRecyclerView.OnChildAttachStateChangeListener() {

    @Override

    publicvoidonChildViewAttachedToWindow(Viewview) {

    if(mOnViewPagerListener!=null&&getChildCount()==1) {

    mOnViewPagerListener.onInitComplete();

                }

            }

    @Override

    publicvoidonChildViewDetachedFromWindow(Viewview) {

    if(mDrift>=0){

    if(mOnViewPagerListener!=null)mOnViewPagerListener.onPageRelease(true,getPosition(view));

    }else{

    if(mOnViewPagerListener!=null)mOnViewPagerListener.onPageRelease(false,getPosition(view));

                }

            }

        };

    PagerSnapHelper找到view

    ViewviewSettling=mPagerSnapHelper.findSnapView(this);

    通过view得到Positon

    源码:

    public class DouyinLayoutManagerextends LinearLayoutManager {

    private static final StringTAG ="ViewPagerLayoutManager";

        private PagerSnapHelpermPagerSnapHelper;

        private OnViewPagerListenermOnViewPagerListener;

        private RecyclerViewmRecyclerView;

        private int mDrift;//位移,用来判断移动方向

        public DouyinLayoutManager(Context context, int orientation) {

    super(context, orientation, false);

            init();

        }

    public DouyinLayoutManager(Context context, int orientation, boolean reverseLayout) {

    super(context, orientation, reverseLayout);

            init();

        }

    private void init() {

    mPagerSnapHelper =new PagerSnapHelper();

        }

    @Override

    public void onAttachedToWindow(RecyclerView view) {

    super.onAttachedToWindow(view);

            mPagerSnapHelper.attachToRecyclerView(view);

            this.mRecyclerView = view;

            mRecyclerView.addOnChildAttachStateChangeListener(mChildAttachStateChangeListener);

        }

    @Override

    public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {

    super.onLayoutChildren(recycler, state);

    //

        }

    /**

        * 滑动状态的改变

        * 缓慢拖拽-> SCROLL_STATE_DRAGGING

        * 快速滚动-> SCROLL_STATE_SETTLING

        * 空闲状态-> SCROLL_STATE_IDLE

        * @param state

        */

        @Override

    public void onScrollStateChanged(int state) {

    switch (state) {

    case RecyclerView.SCROLL_STATE_IDLE:

    View viewIdle =mPagerSnapHelper.findSnapView(this);

                    int positionIdle = getPosition(viewIdle);

                    if (mOnViewPagerListener !=null && getChildCount() ==1) {

    mOnViewPagerListener.onPageSelected(positionIdle,positionIdle == getItemCount() -1);

                    }

    break;

                case RecyclerView.SCROLL_STATE_DRAGGING:

    View viewDrag =mPagerSnapHelper.findSnapView(this);

                    int positionDrag = getPosition(viewDrag);

    break;

                case RecyclerView.SCROLL_STATE_SETTLING:

    View viewSettling =mPagerSnapHelper.findSnapView(this);

                    int positionSettling = getPosition(viewSettling);

    break;

            }

    }

    /**

        * 监听竖直方向的相对偏移量

        * @param dy

        * @param recycler

        * @param state

        * @return

        */

        @Override

    public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {

    this.mDrift = dy;

            return super.scrollVerticallyBy(dy, recycler, state);

        }

    /**

        * 监听水平方向的相对偏移量

        * @param dx

        * @param recycler

        * @param state

        * @return

        */

        @Override

    public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {

    this.mDrift = dx;

            return super.scrollHorizontallyBy(dx, recycler, state);

        }

    /**

        * 设置监听

        * @param listener

        */

        public void setOnViewPagerListener(OnViewPagerListener listener){

    this.mOnViewPagerListener = listener;

        }

    private RecyclerView.OnChildAttachStateChangeListenermChildAttachStateChangeListener =new RecyclerView.OnChildAttachStateChangeListener() {

    @Override

    public void onChildViewAttachedToWindow(View view) {

    if (mOnViewPagerListener !=null && getChildCount() ==1) {

    mOnViewPagerListener.onInitComplete();

                }

    }

    @Override

    public void onChildViewDetachedFromWindow(View view) {

    if (mDrift >=0){

    if (mOnViewPagerListener !=null)mOnViewPagerListener.onPageRelease(true,getPosition(view));

                }else {

    if (mOnViewPagerListener !=null)mOnViewPagerListener.onPageRelease(false,getPosition(view));

                }

    }

    };

    }

    public class DouyinActivityextends AppCompatActivity {

    private static final StringTAG ="ViewPagerActivity";

        private RecyclerViewmRecyclerView;

        private MyAdaptermAdapter;

        private DouyinLayoutManagermLayoutManager;

        @Override

        protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_view_pager_layout_manager);

            initView();

            initListener();

        }

    private void initView() {

    mRecyclerView = findViewById(R.id.recycler);

            mLayoutManager =new DouyinLayoutManager(DouyinActivity.this, OrientationHelper.VERTICAL);

            mAdapter =new MyAdapter();

            mRecyclerView.setLayoutManager(mLayoutManager);

            mRecyclerView.setAdapter(mAdapter);

        }

    private void initListener(){

    mLayoutManager.setOnViewPagerListener(new OnViewPagerListener() {

    @Override

                public void onInitComplete() {

    Log.e(TAG,"onInitComplete");

                    playVideo(0);

                }

    @Override

                public void onPageRelease(boolean isNext,int position) {

    Log.e(TAG,"释放位置:"+position +" 下一页:"+isNext);

                    int index =0;

                    if (isNext){

    index =0;

                    }else {

    index =1;

                    }

    releaseVideo(index);

                }

    @Override

                public void onPageSelected(int position,boolean isBottom) {

    Log.e(TAG,"选中位置:"+position+"  是否是滑动到底部:"+isBottom);

                    playVideo(0);

                }

    });

        }

    demo地址:https://github.com/pengcaihua123456/shennandadao

    相关文章

      网友评论

        本文标题:8.Android RecycleView实现抖音翻页上下滑

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