自动在listview / scrollview中播放视频,类似于facebook

时间:2015-11-24 10:08:55

标签: android listview video scrollbar playing

如果视图包含视频,我需要在listview / scrollview中自动播放视频。这与facebook非常相似。如果用户向下滚动并且可见区域包含系统将播放视频的视频,并且如果仍然滚动则自动停止该视频。它应该像一个视频应该一次播放一样工作。

有人可以帮我吗?

我经历过的消息来源:

  1. Play video in Android listview
  2. How to automatically play video in listview on android app
  3. How to automatically play video in listview on android app
  4. 谢谢.. !!

1 个答案:

答案 0 :(得分:1)

请按照要点

  1. 首先,您需要在RecyclerView
  2. 中添加滚动侦听器
  3. 然后通过监听器更新您的RecyclerView适配器

    protected void onListViewUpdate(final int position, final Object object) {
        final RecyclerView view = mView;
        LinearLayoutManager layoutManager = ((LinearLayoutManager)view.getLayoutManager());
        final View convertView = layoutManager.findViewByPosition(position);
        int firstVisiblePosition = layoutManager.findFirstCompletelyVisibleItemPosition();
        int lastVisiblePosition = layoutManager.findLastCompletelyVisibleItemPosition();            
    
        if (firstVisiblePosition <= position && position <= lastVisiblePosition) {
            // this is the convertView that you previously returned in getView
            // just fix it (for example:)
    
            Thread thread = new Thread(){
                @Override
                public void run() {
                    super.run();
    
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            adapter.updateRow(adapter.getItem(position), convertView, object);
                        }
                    });
                }
            };
            thread.start();
        } else {
            // just update your data set, UI will be updated automatically in next
            // getView() call
            adapter.updateData(position, object);
        }
    }
    
  4. 从适配器更新updateRow()方法的当前可见视图。

  5. 工作完成:)