Android仿搜狐视频、微视等列表播放视频功能
最近项目中需要是实现在列表中自动播放视频,中间遇到了些问题,终于解决,特来跟大家分享一下:
列表使用的RecyclerView播放视频使用MediaPlayer+TextureView。
主要思路:
1、监听RecyclerView的滑动,开始滑动时停止正在播放的item。
2、通过LinearLayoutManager获取当前显示的第一个item及最后一个item
3、RecyclerView停止滑动后,选择item进行播放。如果当前界面只有一个item,播放当前。如果item数量大于2个,播放第二个。如当前界面有两个item则判定哪一个显示的区域比较大。播放item并记录当前position。
附上主要实现逻辑:
try{
intfristPos=layoutManager.findFirstVisibleItemPosition();
intlastPos=layoutManager.findLastVisibleItemPosition();
ViewHolderholder=null;
if(recyclerView.getChildCount()==2){
ViewfristView=recyclerView.getChildAt(0);
if(fristView!=null){
int[]location=newint[2];
fristView.getLocationInWindow(location);
if(location[1]>0){
holder=(ViewHolder)recyclerView.findViewHolderForPosition(fristPos);
lastPlayPosition=fristPos;
}
}
if(holder==null){
ViewlastView=recyclerView.getChildAt(1);
if(lastView!=null){
int[]lastViewLocation=newint[2];
lastView.getLocationInWindow(lastViewLocation);
if((lastViewLocation[1]+videoHeight)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。