Android仿抖音列表效果

作者:xiangxiongfly 时间:2022-03-01 16:36:50 

本文实例为大家分享了Android仿抖音列表效果的具体代码,供大家参考,具体内容如下

Android仿抖音列表效果

当下抖音非常火热,是不是也很心动做一个类似的app吗?

那我们就用RecyclerView实现这个功能吧,关于内存的回收利用就交给RecyclerView就好了。

首先我们先说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();
}
}

github:Android仿抖音列表效果

来源:https://blog.csdn.net/qq_14876133/article/details/82790708

标签:Android,抖音列表
0
投稿

猜你喜欢

  • Android本地存储SharedPreferences详解

    2023-06-24 17:01:51
  • SpringBoot实现PPT格式文件上传并在线预览功能

    2023-07-22 14:44:56
  • Unity调取移动端的麦克风进行录音并播放

    2023-06-04 22:18:05
  • 详解Mybatis框架SQL防注入指南

    2023-09-16 02:49:02
  • Unity中C#和Java的相互调用实例代码

    2022-02-28 13:40:53
  • Java多线程事务管理的实现

    2023-07-26 22:58:20
  • Java拷贝数组方法Arrays.copyOf()是地址传递的证明实例

    2023-11-08 11:51:29
  • unity使用socket编程实现聊天室功能

    2023-10-18 05:26:04
  • Java使用iTextPDF生成PDF文件的实现方法

    2023-10-15 10:44:13
  • Java花式解决'分割回文串 ii'问题详解

    2022-07-09 02:01:58
  • C#中控件动态添加事件绑定的时机详解

    2022-05-05 17:40:59
  • c#求范围内素数的示例分享(c#求素数)

    2022-07-09 22:20:10
  • SpringCloud hystrix断路器与局部降级全面介绍

    2023-10-28 17:29:11
  • Android实现登录界面的注册功能

    2021-10-22 01:17:55
  • Java中重定向输出流实现用文件记录程序日志

    2021-12-28 20:57:13
  • java读取ftp中TXT文件的案例

    2022-07-07 20:10:11
  • Spring Boot项目中定制拦截器的方法详解

    2021-07-28 19:21:55
  • Java并发编程之闭锁与栅栏的实现

    2022-01-09 15:00:12
  • Autowired的注入过程源码解析

    2022-04-29 17:53:36
  • java中的SpringBoot框架

    2022-02-10 16:50:05
  • asp之家 软件编程 m.aspxhome.com