Android仿抖音上下滑动布局
抖音上下滑动,监听播放,自动吸顶,吸底效果,供大家参考,具体内容如下
使用RecyclerView+PagerSnapHelper实现
publicclassDouYinLayoutManagerextendsLinearLayoutManagerimplementsRecyclerView.OnChildAttachStateChangeListener{ //判断是否上滑还是下滑 privateintmDrift; privateOnViewPagerListeneronViewPagerListener; //吸顶,吸底 privatePagerSnapHelperpagerSnapHelper; publicOnViewPagerListenergetOnViewPagerListener(){ returnonViewPagerListener; } publicvoidsetOnViewPagerListener(OnViewPagerListeneronViewPagerListener){ this.onViewPagerListener=onViewPagerListener; } publicDouYinLayoutManager(Contextcontext){ super(context); } publicDouYinLayoutManager(Contextcontext,intorientation,booleanreverseLayout){ super(context,orientation,reverseLayout); pagerSnapHelper=newPagerSnapHelper(); } /** *当manager完全添加到recycleview中是会被调用 *@paramview */ @Override publicvoidonAttachedToWindow(RecyclerViewview){ view.addOnChildAttachStateChangeListener(this); pagerSnapHelper.attachToRecyclerView(view); super.onAttachedToWindow(view); } @Override publicbooleancanScrollVertically(){ returnsuper.canScrollVertically(); } @Override publicintscrollVerticallyBy(intdy,RecyclerView.Recyclerrecycler,RecyclerView.Statestate){ mDrift=dy; returnsuper.scrollVerticallyBy(dy,recycler,state); } @Override publicvoidonChildViewAttachedToWindow(@NonNullViewview){ if(mDrift>0){ //向上滑 if(onViewPagerListener!=null&&Math.abs(mDrift)==view.getHeight()){ onViewPagerListener.onPageSelected(false,view); } }else{ //向下滑 if(onViewPagerListener!=null&&Math.abs(mDrift)==view.getHeight()){ onViewPagerListener.onPageSelected(true,view); } } } @Override publicvoidonChildViewDetachedFromWindow(@NonNullViewview){ if(mDrift>=0){ //向上滑 if(onViewPagerListener!=null){ onViewPagerListener.onPageRelease(true,view); } }else{ //向下滑 if(onViewPagerListener!=null){ onViewPagerListener.onPageRelease(false,view); } } } @Override publicvoidonScrollStateChanged(intstate){ switch(state){ caseRecyclerView.SCROLL_STATE_IDLE: //当前显示的item ViewsnapView=pagerSnapHelper.findSnapView(this); if(onViewPagerListener!=null){ onViewPagerListener.onPageSelected(false,snapView); } break; } super.onScrollStateChanged(state); } }
recyclerView.setLayoutManager()即可
recyclerView=findViewById(R.id.recyclerView_dy); douYinLayoutManager=newDouYinLayoutManager(this,OrientationHelper.VERTICAL,false); recyclerView.setLayoutManager(douYinLayoutManager); recyclerView.setAdapter(newMyAdapter()); douYinLayoutManager.setOnViewPagerListener(newOnViewPagerListener(){ @Override publicvoidonPageRelease(booleanisNest,Viewposition){ releaseVideo(position); } @Override publicvoidonPageSelected(booleanisButten,Viewposition){ playVideo(position); } }); }
代码:MyUiDemo
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。