您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關怎么在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中實現一個抖音列表效果了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。