实现视频+图片轮播

Adapter:

package com.example.administrator.gymscreen.adapter;

import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.VideoView;

import com.bumptech.glide.Glide;
import com.example.administrator.gymscreen.beans.BannerResult;
import com.example.administrator.gymscreen.listener.BannerListener;

import java.util.List;

/**
 * 轮播适配器
 * Created by Mr_zeng on 2017/8/30.
 */

public class BannerAdapter extends PagerAdapter {
    private Context mContext;
    private List imgUrls;
    private BannerListener bannerListener;

    public BannerAdapter(Context context) {
        this.mContext = context;
    }

    public void update(List resIds) {
        if (this.imgUrls != null)
            this.imgUrls.clear();
        if (resIds != null)
            this.imgUrls = resIds;
    }

    @Override
    public int getCount() {
        return imgUrls == null ? 0 : Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        BannerResult url = imgUrls.get(position % imgUrls.size());
        if (url.getType().equals("1")) {
            ImageView imageView = new ImageView(mContext);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            Glide.with(mContext).load(url.getUrl()).into(imageView);
            container.addView(imageView);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    bannerListener.bannerClick(position % imgUrls.size());
                }
            });
            return imageView;
        } else {
            final VideoView videoView = new VideoView(mContext);
            Uri uri = Uri.parse(url.getUrl());
            videoView.setVideoURI(uri);
            videoView.start();
            container.addView(videoView);
            videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    bannerListener.playEnd();
                }
            });
            return videoView;
        }
    }

    public void setBannerListener(BannerListener bannerListener) {
        this.bannerListener = bannerListener;
    }

}


Activity

package com.example.administrator.gymscreen.activity;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.example.administrator.gymscreen.R;
import com.example.administrator.gymscreen.adapter.BannerAdapter;
import com.example.administrator.gymscreen.beans.BannerResult;
import com.example.administrator.gymscreen.listener.BannerListener;
import com.example.administrator.gymscreen.utils.DateUtil;
import com.example.administrator.gymscreen.widget.MyViewPager;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 实现图片+视频轮播
 * Created by Mr_zeng on 2017/8/30.
 */

public class BannerActivity extends BaseActivity {
    private TextView tvDate;
    private TextView tvFingerprint;
    private MyViewPager banner;
    private List bannerList = new ArrayList<>();
    private BannerAdapter bannerAdapter;

    int position1 = 0;
    Timer mTimer;
    TimerTask mTimerTask;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                banner.setCurrentItem(position1);
                position1++;
            }
        }
    };

    @Override
    public int initLayout() {
        return R.layout.activity_banner;
    }

    @Override
    public void initView() {
        tvDate = (TextView) findViewById(R.id.tvDate);
        banner = (MyViewPager) findViewById(R.id.banner);
        tvFingerprint = (TextView) findViewById(R.id.tvFingerprint);
    }

    @Override
    public void initData() {
        getBanner();
        tvDate.setText(DateUtil.getNowDate() + "\t\t\t\t" + DateUtil.getWeek1(DateUtil.getNowDate1()));
        tvFingerprint.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(FingerprintActivity.class);
                finish();
            }
        });
    }


    private void getBanner() {
        BannerResult result1 = new BannerResult("2", "*************************************", "中秋视频广告");
        BannerResult result2 = new BannerResult("1", "***************************", "21212");
        BannerResult result3 = new BannerResult("1", "*********************************", "国庆广告");
        bannerList.add(result2);
        bannerList.add(result1);
        bannerList.add(result3);
        bannerAdapter = new BannerAdapter(this);
        bannerAdapter.update(bannerList);
        banner.setAdapter(bannerAdapter);
        start();
        banner.setOnPageChangeListener(new MyViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (bannerList.get(position % 3).getType().equals("2")) {
                    stop();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        bannerAdapter.setBannerListener(new BannerListener() {
            @Override
            public void bannerClick(int position) {

            }

            @Override
            public void playEnd() {
                start();
            }

        });
    }

    public void start() {
        if (mTimer == null) {
            mTimer = new Timer();
        }
        if (mTimerTask == null) {
            mTimerTask = new TimerTask() {
                @Override
                public void run() {
                    Message message = new Message();
                    message.what = 1;
                    handler.sendMessage(message);
                }
            };
        }
        if (mTimer != null && mTimerTask != null) {
            mTimer.schedule(mTimerTask, 0, 3 * 1000);
        }

    }

    public void stop() {
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }
        if (mTimerTask != null) {
            mTimerTask.cancel();
            mTimerTask = null;
        }
    }
}

你可能感兴趣的:(需求实现)