Alexandru Nitu
Alexandru Nitu

Reputation: 3

How to set on click listener on video view inside recyclerview

I have a Recyclerview containing images, gifs and videos. All this elements must have on click listeners to open them full screen in another fragment. In the case of images and gifs, everything is working, but with videoview, onClick is fired after I click more than 3 times. If I use on touch listener it is fired even if i scroll the recyclerview. I also have a mediacontroller attached because in the recyclerview i have to mute the videos.

public void setVideo(String url, final int position){
            MediaController mc = new MediaController(mContext);
            mc.setVisibility(View.GONE);
            mc.setAnchorView(videoContent);
            mc.setMediaPlayer(videoContent);
            videoContent.setMediaController(mc);
            String _path = url;
            Uri videoUri = Uri.parse(url);

            videoContent.setVideoURI(videoUri);
            videoContent.setOnPreparedListener(PreparedListener);

            videoContent.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {

                    if(event.getAction() == MotionEvent.ACTION_DOWN){
                        Log.e("@@@", "Video Touched");
                        //listener.onMediaPressed(position);
                    }
                    return true;
                }
            });
        }

    MediaPlayer.OnPreparedListener PreparedListener = new MediaPlayer.OnPreparedListener(){

        @Override
        public void onPrepared(MediaPlayer m) {
            try {
                if (m.isPlaying()) {
                    m.stop();
                    m.release();
                    m = new MediaPlayer();
                }
                m.setVolume(0f, 0f);
                m.setLooping(true);
                m.start();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

Upvotes: 0

Views: 546

Answers (1)

LinuxMasterRace
LinuxMasterRace

Reputation: 107

You would set the onClickListener in your adapter

public void onBindViewHolder(@NonNull final MyViewHolder holder, int i) {
 holder.video.setOnClickListener(v -> {
       //Do stuff
        });
}

Upvotes: 0

Related Questions