Android仿新浪微博滑动播放视频
l515818886
8年前
<p><strong>效果图</strong></p> <p style="text-align:center"><img src="https://simg.open-open.com/show/77c13061fb49b7d9d13947bf0cbec924.gif"> <img src="https://simg.open-open.com/show/8bbd6fdbf5c131c21ba350e18915c91f.gif"></p> <h2><strong>关键代码</strong></h2> <p><strong>1.监听滚动事件</strong></p> <p>首先要给listview添加setOnScrollListener监听,注意这个监听在recyclerView上是addOnScrollListener,也就是说下面代码同时支持recyclerView。</p> <pre> <code class="language-java">public int firstVisible=0,visibleCount=0, totalCount=0; videoList.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { switch (scrollState) { case AbsListView.OnScrollListener.SCROLL_STATE_FLING: Log.e("videoTest", "SCROLL_STATE_FLING"); break; case AbsListView.OnScrollListener.SCROLL_STATE_IDLE: Log.e("videoTest", "SCROLL_STATE_IDLE"); autoPlayVideo(view); break; case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL: Log.e("videoTest", "SCROLL_STATE_TOUCH_SCROLL"); break; default: break; } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { // firstVisibleItem 当前第一个可见的item // visibleItemCount 当前可见的item个数 if (firstVisible == firstVisibleItem) { return; } firstVisible = firstVisibleItem; visibleCount = visibleItemCount; totalCount = totalItemCount; } });</code></pre> <p>监听里会有两个方法, 我们用onScroll方法记录 当前第一个可见Item,以及可见Item总数,用onScrollStateChanged来监听手滑动屏幕的整个过程。 当onScrollStateChanged 中的scrollState字段值等于SCROLL_STATE_IDLE 时,代表本次滑动完毕并停止滚动��感兴趣的朋友可以自行百度另外两个参数的意思,没准对你的需求有帮助��。</p> <p><strong>2.处理视频逻辑</strong></p> <pre> <code class="language-java">void autoPlayVideo(AbsListView view){ Log.e("videoTest", "firstVisiblePos = " + firstVisible + "visibleItemCount = " + visibleCount); for (int i = 0; i < visibleCount; i++) { if (view!=null&&view.getChildAt(i)!=null&&view.getChildAt(i).findViewById(R.id.videoplayer) != null) { JCVideoPlayerStandard videoPlayerStandard1 = (JCVideoPlayerStandard) view.getChildAt(i).findViewById(R.id.videoplayer); Rect rect = new Rect(); videoPlayerStandard1.getLocalVisibleRect(rect); int videoheight3 = videoPlayerStandard1.getHeight(); Log.e("videoTest","i="+i+"==="+"videoheight3:"+videoheight3+"==="+"rect.top:"+rect.top+"==="+"rect.bottom:"+rect.bottom); if (rect.top==0&▭.bottom==videoheight3) { if (videoPlayerStandard1.currentState == JCVideoPlayer.CURRENT_STATE_NORMAL || videoPlayerStandard1.currentState == JCVideoPlayer.CURRENT_STATE_ERROR) { Log.e("videoTest", videoPlayerStandard1.currentState + "======================performClick======================"); videoPlayerStandard1.startButton.performClick(); VPApplication.instance.VideoPlaying=videoPlayerStandard1; } return; } } } Log.e("videoTest", "======================releaseAllVideos====================="); JCVideoPlayer.releaseAllVideos(); VPApplication.instance.VideoPlaying=null; }</code></pre> <p>首先是根据总数循环判断 Item 是否有视频,如果有再利用Rect类获取视图在屏幕坐标中的可视区域。基本核心代码就这些是不是非常简单?</p> <h3><strong>视频播放器依赖</strong></h3> <pre> <code class="language-java">compile 'fm.jiecao:jiecaovideoplayer:4.6.3' </code></pre> <p> </p> <p>来自:http://zhangliwei.date/2016/09/05/Android仿新浪微博滑动播放视频/</p> <p> </p>