当前位置: 移动技术网 > IT编程>移动开发>Android > Android仿搜狐视频、微视等列表播放视频功能

Android仿搜狐视频、微视等列表播放视频功能

2019年07月24日  | 移动技术网IT编程  | 我要评论

黄浩然电影,东莞公交车路线查询,赵涛ycl

最近项目中需要是实现在列表中自动播放视频,中间遇到了些问题,终于解决,特来跟大家分享一下:

列表使用的recyclerview 播放视频使用mediaplayer+textureview。

主要思路:

1、监听recyclerview的滑动,开始滑动时停止正在播放的item。

2、通过linearlayoutmanager 获取当前显示的第一个item及最后一个item

3、recyclerview停止滑动后,选择item进行播放。如果当前界面只有一个item,播放当前。如果item数量大于2个,播放第二个。如当前界面有两个item则判定哪一个显示的区域比较大。播放item并记录当前position。

附上主要实现逻辑:

try { 
      int fristpos = layoutmanager.findfirstvisibleitemposition(); 
      int lastpos = layoutmanager.findlastvisibleitemposition(); 
      viewholder holder = null; 
      if (recyclerview.getchildcount() == 2) { 
        view fristview = recyclerview.getchildat(0); 
        if (fristview != null) { 
          int[] location = new int[2]; 
          fristview.getlocationinwindow(location); 
          if (location[1] > 0) { 
            holder = (viewholder) recyclerview.findviewholderforposition(fristpos); 
            lastplayposition = fristpos; 
          } 
        } 
        if (holder == null) { 
          view lastview = recyclerview.getchildat(1); 
          if (lastview != null) { 
            int[] lastviewlocation = new int[2]; 
            lastview.getlocationinwindow(lastviewlocation); 
            if ((lastviewlocation[1] + videoheight) < screenheight) { 
              holder = (viewholder) recyclerview.findviewholderforposition(lastpos); 
              lastplayposition = lastpos; 
            } 
          } 
 
 
        } 
      } else if (recyclerview.getchildcount() == 1) { 
        holder = (viewholder) recyclerview.findviewholderforposition(fristpos); 
        lastplayposition = fristpos; 
      } else { 
        holder = (viewholder) recyclerview.findviewholderforposition(fristpos + 1); 
        lastplayposition = fristpos + 1; 
      } 
 
 
      if (holder != null) { 
        holder.play(); 
      } 
       
    } catch (exception e) { 
      e.printstacktrace(); 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网