Android仿新浪微博/QQ空间滑动自动播放视频功能

先来看看效果图


关键代码

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类获取视图在屏幕坐标中的可视区域。

小结

好了,基本核心代码就是以上的这些,是不是很简单呢?感兴趣的大家快快自己动手操作起来吧,希望这篇文章对大家能有所帮助,如果有疑问大家可以留言交流。

以上是 Android仿新浪微博/QQ空间滑动自动播放视频功能 的全部内容, 来源链接: utcz.com/z/337376.html

回到顶部