发布于 2017-09-13 12:46:09 | 119 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要为大家详细介绍了Android仿搜狐视频、微视等列表播放视频功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

列表使用的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(); 
} 

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



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务