您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)Android中怎么實(shí)現(xiàn)抖音列表效果,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
首先我們先說3個(gè)和視頻播放暫停相關(guān)的接口
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;//位移,用來判斷移動(dòng)方向 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); } /** * 滑動(dòng)狀態(tài)的改變 * 緩慢拖拽-> SCROLL_STATE_DRAGGING * 快速滾動(dòng)-> SCROLL_STATE_SETTLING * 空閑狀態(tài)-> 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; } } /** * 監(jiān)聽豎直方向的相對偏移量 * * @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); } /** * 監(jiān)聽水平方向的相對偏移量 * * @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); } /** * 設(shè)置監(jiān)聽 * * @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中怎么實(shí)現(xiàn)抖音列表效果,小編相信有部分知識點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。