如何使用Android仿抖音上下滑动布局
这篇文章将为大家详细讲解有关如何使用Android仿抖音上下滑动布局,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
创新互联-专业网站定制、快速模板网站建设、高性价比奉贤网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式奉贤网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖奉贤地区。费用合理售后完善,10多年实体公司更值得信赖。
具体内容如下
使用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仿抖音上下滑动布局”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
分享标题:如何使用Android仿抖音上下滑动布局
文章源于:http://scyanting.com/article/iiighi.html