如何在recyclerview中的不同视图中显示视频和图像?

时间:2017-10-09 16:34:55

标签: java android android-recyclerview

enter image description here我想在只有一个循环视图适配器的单独水平视图中显示图像和视频。我有两个按钮(图像和视频)。当我拍摄照片时,我必须在一个回收视图中显示,例如视频。录制视频后,我想存储在另一个ScrollView中,但我想使用相同的循环适配器。

if (caseSheetDao.getDataTypesheet().equals("camera")) {

    holder.mCamera.setVisibility(View.VISIBLE);
    holder.mVideoView.setVisibility(View.GONE);
    holder.mVideoplay.setVisibility(View.GONE);
    holder.mFramecase.setVisibility(View.GONE);
    holder.mAudiolayout.setVisibility(View.GONE);

    holder.mCamera.setImageURI(Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + caseSheetDao.getUrisheet()));
    Log.i(TAG,"Image Path : "+Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + caseSheetDao.getUrisheet());
}

if (caseSheetDao.getDataTypesheet().equals("video")) {

    holder.mCamera.setVisibility(View.GONE);
    holder.mAudiolayout.setVisibility(View.GONE);
    holder.mFramecase.setVisibility(View.VISIBLE);
    holder.mVideoView.setVisibility(View.VISIBLE);
    holder.mVideoplay.setVisibility(View.VISIBLE);

//  Glide.with(context)
//      .load(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + caseSheetDao.getUrisheet()) // or URI/path
//      .into(holder.mVideoView);

    Log.i(TAG," Video Path : "+Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + caseSheetDao.getUrisheet());
    holder.mVideoplay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent();
            intent.setAction(android.content.Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + caseSheetDao.getUrisheet()), "video/*");
            context.startActivity(intent);
        }
    });

}

1 个答案:

答案 0 :(得分:0)

像这样:

public class Content {
    boolean isVideo;

    public Content(boolean isVideo) {
        this.isVideo = isVideo;
    }

    public boolean isVideo() {
        return isVideo;
    }

    public void setVideo(boolean video) {
        isVideo = video;
    }
}



public class CustomAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private static final int IS_VIDEO = 0;
    private static final int IS_IMAGE = 1;
    private List<Content> items;

    public CustomAdapter(List<Content> items) {
        this.items = items;
    }

    public void refresh(List<Content> items) {
        this.items = items;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if(items.get(position).isVideo){
            return IS_VIDEO;
        }else{
            return IS_IMAGE;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == IS_VIDEO){
            return new VideoViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.viewholder_video, parent, false));
        }
        else {
            return new ImageViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.viewholder_image, parent, false));
        }
    }



    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof ImageViewHolder){
            onBindImageViewHolder((ImageViewHolder) holder, position);
        }
        else{
            onBindVideoViewHolder((VideoViewHolder) holder, position);
        }
    }

    private void onBindImageViewHolder(ImageViewHolder holder, int position) {

    }

    private void onBindVideoViewHolder(VideoViewHolder holder, int position) {

    }

    @Override
    public int getItemCount() {
        return items.size();
    }


    public class ImageViewHolder extends RecyclerView.ViewHolder {

        public ImageViewHolder(View itemView) {
            super(itemView);
        }

    }

    public class VideoViewHolder extends RecyclerView.ViewHolder {

        public VideoViewHolder(View itemView) {
            super(itemView);
        }

    }
}