Android仿新浪微博/QQ空间滑动自动播放视频功能
程序员文章站
2024-03-06 10:15:19
先来看看效果图
关键代码
1.监听滚动事件
首先要给listview添加setonscrolllistener监听,注意这个监听在recyclerview...
先来看看效果图
关键代码
1.监听滚动事件
首先要给listview
添加setonscrolllistener
监听,注意这个监听在recyclerview
上是addonscrolllistener
,也就是说下面代码同时支持recyclerview
。
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; } });
监听里会有两个方法,我们用onscroll
方法记录 当前第一个可见item
,以及可见item
总数,用onscrollstatechanged
来监听手滑动屏幕的整个过程。 当onscrollstatechanged
中的scrollstate
字段值等于scroll_state_idle
时,代表本次滑动完毕并停止滚动????感兴趣的朋友可以自行百度另外两个参数的意思,没准对你的需求有帮助????。
2.处理视频逻辑
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&&rect.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; }
首先是根据总数循环判断 item
是否有视频,如果有再利用rect
类获取视图在屏幕坐标中的可视区域。
小结
好了,基本核心代码就是以上的这些,是不是很简单呢?感兴趣的大家快快自己动手操作起来吧,希望这篇文章对大家能有所帮助,如果有疑问大家可以留言交流。