您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
今天就跟大家聊聊有关怎么在Android中实现抖音上下滑动布局,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
使用RecyclerView+PagerSnapHelper实现
public class DouYinLayoutManager extends LinearLayoutManager implements RecyclerView.OnChildAttachStateChangeListener{ //判断是否上滑还是下滑 private int mDrift; private OnViewPagerListener onViewPagerListener; //吸顶,吸底 private PagerSnapHelper pagerSnapHelper; public OnViewPagerListener getOnViewPagerListener() { return onViewPagerListener; } public void setOnViewPagerListener(OnViewPagerListener onViewPagerListener) { this.onViewPagerListener = onViewPagerListener; } public DouYinLayoutManager(Context context) { super(context); } public DouYinLayoutManager(Context context, int orientation, boolean reverseLayout) { super(context, orientation, reverseLayout); pagerSnapHelper = new PagerSnapHelper(); } /** * 当manager完全添加到recycleview中是会被调用 * @param view */ @Override public void onAttachedToWindow(RecyclerView view) { view.addOnChildAttachStateChangeListener(this); pagerSnapHelper.attachToRecyclerView(view); super.onAttachedToWindow(view); } @Override public boolean canScrollVertically() { return super.canScrollVertically(); } @Override public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) { mDrift = dy; return super.scrollVerticallyBy(dy, recycler, state); } @Override public void onChildViewAttachedToWindow(@NonNull View view) { 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 public void onChildViewDetachedFromWindow(@NonNull View view) { if (mDrift >= 0){ //向上滑 if (onViewPagerListener != null){ onViewPagerListener.onPageRelease(true,view); } }else { //向下滑 if (onViewPagerListener != null){ onViewPagerListener.onPageRelease(false,view); } } } @Override public void onScrollStateChanged(int state) { switch (state){ case RecyclerView.SCROLL_STATE_IDLE: //当前显示的item View snapView = pagerSnapHelper.findSnapView(this); if (onViewPagerListener != null){ onViewPagerListener.onPageSelected(false,snapView); } break; } super.onScrollStateChanged(state); } }
recyclerView.setLayoutManager()即可
recyclerView = findViewById(R.id.recyclerView_dy); douYinLayoutManager = new DouYinLayoutManager(this, OrientationHelper.VERTICAL,false); recyclerView.setLayoutManager(douYinLayoutManager); recyclerView.setAdapter(new MyAdapter()); douYinLayoutManager.setOnViewPagerListener(new OnViewPagerListener() { @Override public void onPageRelease(boolean isNest, View position) { releaseVideo(position); } @Override public void onPageSelected(boolean isButten, View position) { playVideo(position); } }); }
看完上述内容,你们对怎么在Android中实现抖音上下滑动布局有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。