Android中怎么实现抖音列表效果

发布时间:2021-06-28 16:11:08 作者:Leah
来源:亿速云 阅读:326

本篇文章给大家分享的是有关Android中怎么实现抖音列表效果,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

首先我们先说3个和视频播放暂停相关的接口

public interface OnViewPagerListener { /**  * 初始化  */ void onInitComplete(View view); /**  * 释放  */ void onPageRelease(boolean isNext, int position, View view); /**  * 选中  */ void onPageSelected(int position, boolean isBottom, View view);}

然后自定义LinearLayoutManager

public class PagerLayoutManager extends LinearLayoutManager { private PagerSnapHelper mPagerSnapHelper; private OnViewPagerListener mOnViewPagerListener; private RecyclerView mRecyclerView; private int mDrift;//位移,用来判断移动方向 public PagerLayoutManager(Context context, int orientation) {  super(context, orientation, false);  init(); } public PagerLayoutManager(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);    if (viewIdle != null) {     int positionIdle = getPosition(viewIdle);     if (mOnViewPagerListener != null && getChildCount() == 1) {      mOnViewPagerListener.onPageSelected(positionIdle, positionIdle == getItemCount() - 1, viewIdle);     }    }    break;   case RecyclerView.SCROLL_STATE_DRAGGING:    View viewDrag = mPagerSnapHelper.findSnapView(this);    if (viewDrag != null) {     int positionDrag = getPosition(viewDrag);    }    break;   case RecyclerView.SCROLL_STATE_SETTLING:    View viewSettling = mPagerSnapHelper.findSnapView(this);    if (viewSettling != null) {     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.OnChildAttachStateChangeListener mChildAttachStateChangeListener = new RecyclerView.OnChildAttachStateChangeListener() {  /**   * itemView依赖Window   */  @Override  public void onChildViewAttachedToWindow(View view) {   if (mOnViewPagerListener != null && getChildCount() == 1) {    mOnViewPagerListener.onInitComplete(view);   }  }  /**   *itemView脱离Window   */  @Override  public void onChildViewDetachedFromWindow(View view) {   if (mDrift >= 0) {    if (mOnViewPagerListener != null)     mOnViewPagerListener.onPageRelease(true, getPosition(view), view);   } else {    if (mOnViewPagerListener != null)     mOnViewPagerListener.onPageRelease(false, getPosition(view), view);   }  } };}然后大功告成直接使用recyclerView = findViewById(R.id.recycler_view);  PagerLayoutManager mLayoutManager = new PagerLayoutManager(this, OrientationHelper.VERTICAL);  mDatas.addAll(DataUtils.getDatas());  mAdapter = new VideoAdapter(this, mDatas);  recyclerView.setLayoutManager(mLayoutManager);  recyclerView.setAdapter(mAdapter);  mLayoutManager.setOnViewPagerListener(new OnViewPagerListener() {   @Override   public void onInitComplete(View view) {    playVideo(0, view);   }   @Override   public void onPageSelected(int position, boolean isBottom, View view) {    playVideo(position, view);   }   @Override   public void onPageRelease(boolean isNext, int position, View view) {    int index = 0;    if (isNext) {     index = 0;    } else {     index = 1;    }    releaseVideo(view);   }  });/**  * 播放视频  */ private void playVideo(int position, View view) {  if (view != null) {   mVideoView = view.findViewById(R.id.video_view);   mVideoView.start();  } } /**  * 停止播放  */ private void releaseVideo(View view) {  if (view != null) {   IjkVideoView videoView = view.findViewById(R.id.video_view);   videoView.stopPlayback();  } }

以上就是Android中怎么实现抖音列表效果,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

推荐阅读:
  1. vue实现抖音时间转盘
  2. Android如何自定义View实现抖音飘动红心效果

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android

上一篇:android中怎么实现一个五子棋

下一篇:HTML中怎么调用javascript文件

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》