Automatically Playing video in listview/scrollview similar to facebook

此生再无相见时 提交于 2019-12-10 15:19:14

问题


I need Video to play automatically in listview/scrollview, if view contains video. This is ver much similar with facebook. If user scrolls down and visible area contains video that system will play video and if still scroll then it automaticaly stops that video. It should work like one video should play at one time.

Can someone help me on this ?

sources I have gone through:

  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

Thanks..!!


回答1:


Please follow the points

  1. First you need to add a scroll listener into RecyclerView
  2. Then through the listener update your RecyclerView adapter

    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);
        }
    }
    
  3. From the adapter update the current visible view from updateRow() method.

Job Done :)



来源:https://stackoverflow.com/questions/33890738/automatically-playing-video-in-listview-scrollview-similar-to-facebook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!