带有Recyclerview的VideoView

时间:2018-01-29 12:03:27

标签: android android-recyclerview android-videoview

我有视频视图预览和视频视图的列表视频在底部,你可以在下面看到我的模型。当我从视频列表中选择一个视频时,我应该播放预览视频。现在我很困惑如何从回收视图到视频视图?

enter image description here

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_premium_video_preview);
    setDefaultToolbar(true);


    Nama = findViewById(R.id.txtNamaPreview);
    Judul = findViewById(R.id.txtJudulPreview);
    Harga = findViewById(R.id.txtHargaPreview);
    judulRelated = findViewById(R.id.judulRelated);
    judulRelated.setText(R.string.others_latest_video);
    btnSubscribed = findViewById(R.id.btnBuy);
    progressBar = new ProgressBar(this);
    videoview = findViewById(R.id.vv_preview_latest);
    rvRelatedLatest = findViewById(R.id.rvRelatedVideo);


    final LatestVideo vid = getIntent().getParcelableExtra("video");
    String url_video = vid.source_content;

    mediaController = new MediaController(this);
    mediaController.setAnchorView(mediaController);

    Uri uri = Uri.parse(url_video);
    videoview.setVideoURI(uri);
    videoview.setMediaController(mediaController);

    videoview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mediaPlayer) {
            videoview.start();
        }
    });
    videoview.setOnErrorListener(new MediaPlayer.OnErrorListener() {
        @Override
        public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
            return false;
        }
    });

    Nama.setText(vid.name);
    Judul.setText(vid.judul);
    Harga.setText("Rp." + vid.price);

    initRelatedLatest();
    loadPremiumVideo();
}

private void initRelatedLatest() {
    LinearLayoutManager layout = new LinearLayoutManager(this);
    layout.setOrientation(LinearLayoutManager.HORIZONTAL);
    rvRelatedLatest.setLayoutManager(layout);
    RelatedLatestAdapter ar = new RelatedLatestAdapter(latestVideoList);
    rvRelatedLatest.setAdapter(ar);

}

private void loadPremiumVideo() {
    Call<APIResponse<List<LatestVideo>>> call = ServicesFactory.getService().getLatestVideo();
    call.enqueue(new Callback<APIResponse<List<LatestVideo>>>() {
        @Override
        public void onResponse(Call<APIResponse<List<LatestVideo>>> call, Response<APIResponse<List<LatestVideo>>> response) {
            if (response.isSuccessful() && response.body().isSuccessful()) {
                List<LatestVideo> data = response.body().data;
                if (data != null) {
                    latestVideoList.clear();
                    latestVideoList.addAll(data.subList(0, (data.size() < 10) ? data.size() - 1 : 10));
                    Collections.shuffle(latestVideoList);
                    rvRelatedLatest.getAdapter().notifyDataSetChanged();
                }
            }
            else {
                Toast.makeText(PreviewLatestVideo.this, response.errorBody().toString(), Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void onFailure(Call<APIResponse<List<LatestVideo>>> call, Throwable t) {
            Toast.makeText(PreviewLatestVideo.this, t.getLocalizedMessage(), Toast.LENGTH_LONG).show();
        }
    });
}

以下是从此适配器开始播放视频列表的视频适配器:

public class RelatedFunnyVideoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
List<VideoLucu> data;

public RelatedFunnyVideoAdapter(List<VideoLucu> data) {
    this.data = data;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.item_premium_video, parent,false);
    return new VH(v);
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    VideoLucu vidLucu = data.get(position);
    VH vh = (VH)holder;
    vh.judul.setText(vidLucu.judul);
    vh.nama.setText(vidLucu.alias);
    vh.harga.setText(vidLucu.price);
    RequestOptions requestOptions = new RequestOptions();
    requestOptions.placeholder(R.drawable.background);
    Glide.with(vh.imgVideo.getContext())
            .load(vidLucu.source_content)
            .into(vh.imgVideo);
    ((VH)holder) .itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            // action to play video

        }
    });
}

@Override
public int getItemCount() {
    return (data == null) ? 0 : data.size();
}

public class VH extends RecyclerView.ViewHolder{
    TextView judul;
    TextView nama;
    TextView harga;
    ImageView imgVideo;

    public VH (View itemView){
        super(itemView);
        judul = itemView.findViewById(R.id.tv_title);
        nama = itemView.findViewById(R.id.tv_artist);
        harga = itemView.findViewById(R.id.tv_harga);
        imgVideo = itemView.findViewById(R.id.iv_video);

    }

  }
}

0 个答案:

没有答案