简体   繁体   English

在RecyclerView中混合的图像和视频

[英]Images + Videos mixed in a RecyclerView

Am trying to check both images and Videos availability in the RecyclerView , but the challenge is the VideoView is the only one Visible in all the items List , the ImageView is not visible at all, what i want is to check if the Image is present in the ImageView the Videoview GOES , and when the Video is available in the VideoView the ImageView Goes . 我正在尝试在RecyclerView检查图像和视频的可用性,但是挑战是VideoView是所有项List中唯一可见的, ImageView根本不可见,我要检查的是是否存在Image该ImageView的的Videoview GOES,当视频可以在VideoViewImageView 推移

Am developing an App which posts both Videos and Images and retrieves them in the same RecyclerView , so please help me how can i achieve this 我正在开发同时发布视频和图像并在同一RecyclerView检索它们的应用程序,所以请帮助我如何实现这一目标

Below is my code but it doesn't work : 下面是我的代码,但它不起作用:

boolean hasDrawable = (viewHolder.imagePost.getDrawable()!= null);

String hasVideo_string =(String)viewHolder.videoLayout.getTag();
boolean hasVideo = Boolean.parseBoolean(hasVideo_string);

if(hasDrawable){
   viewHolder.setTitle(model.getEventTitle());
   viewHolder.setDesc(model.getEventDescription());
   viewHolder.setImage(c, model.getEventImage());
   viewHolder.videoLayout.setVisibility(View.INVISIBLE);
   }

else if(hasVideo) {
  viewHolder.setTitle(model.getEventTitle());
  viewHolder.setDesc(model.getEventDescription());
  viewHolder.setVideo(c, model.getEventVideo());
  viewHolder.imagePost.setVisibility(View.GONE);
  }

This is how i set my Image and Video in a ViewHolder Class 这是我在ViewHolder类中设置图像和视频的ViewHolder

public void setImage(final Context c,final String imageUrl){
    //
    Picasso.with(c).load(imageUrl).error(R.mipmap.add_btn).fit().centerInside().placeholder(R.mipmap.add_btn)
                    .networkPolicy(NetworkPolicy.OFFLINE).into(imagePost, new Callback() {
                @Override
                public void onSuccess() {

                }

                @Override
                public void onError() {

                    //Reloading an image again ...
                    Picasso.with(c).load(imageUrl).error(R.mipmap.add_btn).placeholder(R.mipmap.add_btn)
                            .into(imagePost);
                }
            });

        }

and the video (I used a Library called FullscreenVideoLayout , its like a VideoView but much customized) 和视频(我使用了一个名为FullscreenVideoLayout的库,它像VideoView一样,但自定义程度很高)

public void setVideo(final Context c, final String videoUrl){

   // videoLayout.setActivity(this);
   // videoLayout.setActivity(get);

    Uri videoUri = Uri.parse(videoUrl);
    try {
        videoLayout.setVideoURI(videoUri);
        videoLayout.setTag(videoUrl);

    } catch (IOException e) {
        e.printStackTrace();
    }
}

This is the Full ViewHolder class 这是Full ViewHolder

public static class BlogViewHolder extends RecyclerView.ViewHolder{


        View mView;
        private ImageView imagePost;
        private FullscreenVideoLayout videoLayout;
          private VideoView videoView;

        public BlogViewHolder(View itemView) {
            super(itemView);
            mView=itemView;
          imagePost =(ImageView)mView.findViewById(R.id.post_image);
           videoLayout = (FullscreenVideoLayout) mView.findViewById(R.id.post_video);

        }
        public void setTitle(String title){

            TextView post_title = (TextView)mView.findViewById(R.id.post_title);
            post_title.setText(title);

        }

        public void setDesc(String desc){

            TextView post_desc = (TextView)mView.findViewById(R.id.post_desc);
            post_desc.setText(desc);
        }


        public void setImage(final Context c,final String imageUrl){

       //

            Picasso.with(c).load(imageUrl).error(R.mipmap.add_btn).fit().centerInside().placeholder(R.mipmap.add_btn)
                    .networkPolicy(NetworkPolicy.OFFLINE).into(imagePost, new Callback() {
                @Override
                public void onSuccess() {

                }

                @Override
                public void onError() {

                    //Reloading an image again ...
                    Picasso.with(c).load(imageUrl).error(R.mipmap.add_btn).placeholder(R.mipmap.add_btn)
                            .into(imagePost);
                }
            });

        }

        public void setVideo(final Context c, final String videoUrl){


           // videoLayout.setActivity(this);
           // videoLayout.setActivity(get);

            Uri videoUri = Uri.parse(videoUrl);
            try {
                videoLayout.setVideoURI(videoUri);
                videoLayout.setTag(videoUrl);

            } catch (IOException e) {
                e.printStackTrace();
            }


        }

            }

I see you are using Firebase , so since you are getting the Urls of both Videos and Images getEventImage() and getEventVideo() , just check the Urls if they are Empty and Remove the Visibility of the ImageView and VideoView . 我看你是使用Firebase ,这样以来你得到Urls两个视频和图片的getEventImage()getEventVideo()只是检查Urls ,如果他们是空的,删除的可见性ImageViewVideoView

Below is the sample code , Try this out: 以下是示例代码,请尝试一下:

 public void setImage(final Context c,final String imageUrl){

            try {
                if (imageUrl!=null) {
                    //

                    Picasso.with(c).load(imageUrl).error(R.mipmap.add_btn).fit().centerInside().placeholder(R.mipmap.add_btn)
                            .networkPolicy(NetworkPolicy.OFFLINE).into(imagePost, new Callback() {
                        @Override
                        public void onSuccess() {

                        }

                        @Override
                        public void onError() {

                            //Reloading an image again ...
                            Picasso.with(c).load(imageUrl).error(R.mipmap.add_btn).placeholder(R.mipmap.add_btn)
                                    .into(imagePost);
                        }
                    });





                } else {

                    imagePost.setVisibility(View.GONE);
                }
            }
            catch (Exception e){

            }

        }

        public void setVideo(final Context c, final String videoUrl){
            try {
                if (videoUrl!=null) {
                    try {
                        Uri videoUri = Uri.parse(videoUrl);
                        try {
                            videoLayout.setVideoURI(videoUri);
                            videoLayout.setTag(videoUrl);
                            String hasVideo_string = (String) videoLayout.getTag();
                            boolean hasVideo = Boolean.parseBoolean(hasVideo_string);


                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    } catch (Exception e) {
                        System.out.println("Error :" + e);
                    }

                } else {
                    videoLayout.setVisibility(View.GONE);
                }
            }
            catch (Exception e){

            }


        }

Hope it works for you . 希望对你有效 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM