fragment+viewpager图片视频混合轮播(videoview+glide)

   最近要做一个需求是图片和视频混合轮播,因为之前的话轮播都是用的banner,实现起来比较简单,就是new出来,把图片集合设置上就好了,之后就是一顿设置,所以想找一个可以能够实现的第三方,肯定有,但我还是没找到,大哭2333333,还是自己写吧,废话不多说,直接上干货

1.首先是页面

2activity里的代码

public class MainActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private List fragments;
    private MyHandler myHandler;
    private FragmenetAdapter fragmenetAdapter;
    private List list;
    private String TAG="每次选中传的值";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        initViews();
    }

    private void initData() {
        myHandler=new MyHandler();
        list = new ArrayList<>();
        list.add(new bean("https://vdse.bdstatic.com//611ab9dc5d336b1de5eae1b560b158ed?authorization=bce-auth-v1/fb297a5cc0fb434c971b8fa103e8dd7b/2017-05-11T09:02:31Z/-1//add0017a6e913612db3ef360898b55f2f9580d67a847c8e9b9780b7d83e440cf.mp4","10","video"));
        list.add(new bean("http://9890.vod.myqcloud.com/9890_4e292f9a3dd011e6b4078980237cc3d3.f20.mp4","5","video"));
        list.add(new bean("http://img2.imgtn.bdimg.com/it/u=3817131034,1038857558&fm=27&gp=0.jpg","5","image"));
        list.add(new bean("http://img1.imgtn.bdimg.com/it/u=4194723123,4160931506&fm=200&gp=0.jpg","5","image"));
        list.add(new bean("http://img5.imgtn.bdimg.com/it/u=1812408136,1922560783&fm=27&gp=0.jpg","5","image"));
    }

    private void initViews() {
        viewPager = findViewById(R.id.viewpager);
        fragments = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getType().equals("video")){
                VideoFragment videoFragment = new VideoFragment();
                fragments.add(videoFragment);
                Bundle bundle = new Bundle();
                bundle.putString("url",list.get(i).getUrl());
                videoFragment.setArguments(bundle);
            }else {
                ImageFragment imageFragment = new ImageFragment();
                fragments.add(imageFragment);
                Bundle bundle = new Bundle();
                bundle.putString("url",list.get(i).getUrl());
                imageFragment.setArguments(bundle);
            }
        }
        fragmenetAdapter = new FragmenetAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(fragmenetAdapter);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                EventBus.getDefault().post(list.get(position).getUrl());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    for (int i = 0; i < list.size(); i++) {
                        if(list.get(i).getUrl().endsWith("mp4")){
                            try {
                                Thread.sleep(10000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            Message msg=new Message();
                            msg.arg1=1;
                            MainActivity.this.myHandler.sendMessage(msg);
                        }else {
                            try {
                                Thread.sleep(3000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            Message msg=new Message();
                            msg.arg1=1;
                            MainActivity.this.myHandler.sendMessage(msg);
                        }
                    }

                }
            }
        }).start();
    }

    class MyHandler extends Handler {


        public MyHandler(){}
        public MyHandler(Looper L){
            super(L);
        }

        @Override
        public void handleMessage(Message msg) {
            if(msg.arg1==1) {
                int count = fragmenetAdapter.getCount();
                if (count > 1) { // 多于1个,才循环
                    int index = viewPager.getCurrentItem();
                    index = (index + 1) % count;
                    viewPager.setCurrentItem(index, true);
                    fragmenetAdapter.notifyDataSetChanged();
                }
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}

3.适配器,FragmentPagerAdapter

public class FragmenetAdapter extends FragmentPagerAdapter {
    private List list;

    public FragmenetAdapter(@NonNull FragmentManager fm, List list) {
        super(fm);
        this.list = list;
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {
        return list.get(position);
    }

    @Override
    public int getCount() {
        return list.size();
    }

4.最后是fragment,因为fragment里的布局分别就一个imageview 和videoview 布局就不放了

  4.1.VideoFragment

public class VideoFragment extends BaseLazyLoadFragment {

    private static final String TAG = "videofragment";
    private VideoView videoFrag;
    private String url;
    private String againUrl;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected boolean setIsRealTimeRefresh() {
        return false;
    }

    @Override
    public void lazyLoadData() {
        super.lazyLoadData();

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    protected void initData() {
        Log.i(TAG, "视频url: "+againUrl);
        if (mIsVisibleToUser){
            videoFrag.setVideoURI(Uri.parse(url));
            videoFrag.start();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    protected void initView() {
        videoFrag = mRootView.findViewById(R.id.frag_vidoe);
        Bundle arguments = this.getArguments();
        if(arguments!=null){
            url = arguments.getString("url");
        }
    }

    @Override
    protected int provideLayoutId() {
        return R.layout.frag_video;
    }

    public void setVolume(float volume,Object object) {
        try {
            Class forName = Class.forName("android.widget.VideoView");
            Field field = forName.getDeclaredField("mMediaPlayer");
            field.setAccessible(true);
            MediaPlayer mMediaPlayer = (MediaPlayer) field.get(object);
            mMediaPlayer.setVolume(volume, volume);
        } catch (Exception e) {
        }
    }

    //接收发送来的String类型数据
    @Subscribe(sticky = true)
    public void setUurl(String Url) {
        videoFrag.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                mediaPlayer.setOnInfoListener(new MediaPlayer.OnInfoListener() {
                    @Override
                    public boolean onInfo(MediaPlayer mp, int what, int extra) {
                        if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START)
                            videoFrag.setBackgroundColor(Color.TRANSPARENT);
                        return true;
                    }
                });
            }
        });
        videoFrag.setVideoURI(Uri.parse(Url));
        videoFrag.start();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}

  4.2,ImageFragment

public class ImageFragment extends BaseLazyLoadFragment {

    private ImageView imageFrag;
    private String url;

    @Override
    protected boolean setIsRealTimeRefresh() {
        return false;
    }

    @Override
    public void lazyLoadData() {
        super.lazyLoadData();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Glide.with(this).load(url).diskCacheStrategy(DiskCacheStrategy.RESOURCE).into(imageFrag);
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void initView() {
        imageFrag = mRootView.findViewById(R.id.frag_image);
        Bundle arguments = this.getArguments();
        if(arguments!=null){
            url = arguments.getString("url");
        }
    }

    @Override
    protected int provideLayoutId() {
        return R.layout.frag_image;
    }

这样就ok了,放个美旅镇一镇,23333333333333333333333333333333

fragment+viewpager图片视频混合轮播(videoview+glide)_第1张图片

你可能感兴趣的:(fragment+viewpager图片视频混合轮播(videoview+glide))