如何在将onItemClickListener设置为null后将其重置回true?

huangapple go评论83阅读模式
英文:

How to reset back onItemClickListener to true after been set to null?

问题

private void playVideo() {

    listOfMessages.setOnItemClickListener(null);

    videoText.setVideoURI(Uri.parse(chattVideo));
    frameText.setVisibility(View.VISIBLE);
    imageVideo.setVisibility(View.INVISIBLE);
    videoText.setVisibility(View.VISIBLE);
    btnText.setVisibility(View.GONE);

    try {
        videoText.setMediaController(mediaControllerText);
        mediaControllerText.show();
        videoText.seekTo(1);
        videoText.pause();
        videoText.stopPlayback();
        videoText.seekTo(1);
        videoText.resume();
        progressBarMessage.setVisibility(View.GONE);
        videoText.start();

        videoText.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {

                mp.start();

                mp.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
                    @Override
                    public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {

                        progressBarMessage.setVisibility(View.GONE);
                        mp.start();
                    }
                });

            }
        });

        videoText.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {

                listOfMessages.setOnItemClickListener(this); // if I do like this android studio complains it cannot be applied inside MediaPlayer.OnCompletionListener
                btnText.setVisibility(View.VISIBLE);

            }
        });

        videoText.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {

                FancyToast.makeText(getContext(), "Something went wrong!", FancyToast.LENGTH_LONG,
                        FancyToast.ERROR, R.drawable.wheel, false).show();

                return false;
            }
        });

    } catch (Exception e) {
        PBar.setVisibility(View.GONE);
        FancyToast.makeText(getContext(), e.getMessage(),
                FancyToast.LENGTH_LONG, FancyToast.WARNING, R.drawable.wheel, false).show();
    }
}
英文:

I have set onItemClickListener in my list view,items including videos, images and text. When user tap on the video in list view it plays fine. Now inside the method of playVideo() I set the listview to null so as the user should not interact with the video while playing.

I want to regain/reset back the onitemClicklisterner to true after the video finishes to play. How do I implement this, Kindly assist.

    private void playVideo() {
listOfMessages.setOnItemClickListener(null);
videoText.setVideoURI(Uri.parse(chattVideo));
frameText.setVisibility(View.VISIBLE);
imageVideo.setVisibility(View.INVISIBLE);
videoText.setVisibility(View.VISIBLE);
btnText.setVisibility(View.GONE);
try {
videoText.setMediaController(mediaControllerText);
mediaControllerText.show();
videoText.seekTo(1);
videoText.pause();
videoText.stopPlayback();
videoText.seekTo(1);
videoText.resume();
progressBarMessage.setVisibility(View.GONE);
videoText.start();
videoText.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
mp.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
@Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
progressBarMessage.setVisibility(View.GONE);
mp.start();
}
});
}
});
videoText.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
listOfMessages.setOnItemClickListener(this);// if I do like this android studio complains it cannot be applied inside MediaPlayer.OnCompletionListener
btnText.setVisibility(View.VISIBLE);
}
});
videoText.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
FancyToast.makeText(getContext(),"Something went wrong!",FancyToast.LENGTH_LONG,
FancyToast.ERROR,R.drawable.wheel,false).show();
return false;
}
});
} catch (Exception e) {
PBar.setVisibility(View.GONE);
FancyToast.makeText(getContext(),e.getMessage(),
FancyToast.LENGTH_LONG,FancyToast.WARNING,R.drawable.wheel,false).show();
}
}

答案1

得分: 1

你可以按照以下方式创建一个方法来执行所需的操作:

以下是伪代码:

private void setListener(boolean isPlaying){
    if(isPlaying){
        your_listview.setOnItemClickListener(null);
    }
    else{
        your_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //你的代码
            }
        });
    }
}

现在,当视频播放时,只需简单调用 setListener(true),否则调用 setListener(false)

英文:

You can create a method as below to do required stuff

Here is pseudo code

private void setListener(boolean isPlaying){
if(isPlaying){
your_listview.setOnItemClickListener(null);
}
else{
your_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {
//YOUR CODE
}
});
}
}

Now,simply call setListener(true) when video plays or else
setListener(false)

答案2

得分: 0

我会使用一个布尔变量,当视频正在播放时,您可以将其设置为true/false,根据它的状态,onClick事件监听器可以有不同的反应。

编辑

或者使用 button.setClickable(false); (必须在 .setOnClickListener() 之后使用)。

或者 button.setEnabled(false); (甚至可以在 .setOnClickListener() 之前使用)。

这两种方法的优点是按钮动画也会被禁用,如果您在彼此上方堆叠了多个可点击对象,这些方法可能效果更好。
它们在View类中,也可以在ListView上调用(但我没有“实时”尝试过)。

boolean isPlaying = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button button = findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(isPlaying){
                //执行某些操作
            } else {
                //执行其他操作
            }
        }
    });
}
英文:

I would use a boolean that you can set to true/false when the video is playing and depending on it's state the onClickListener can react differently.

Edit:

Or use button.setClickable(false); (has to be used after .setOnClickListener().

Or button.setEnabled(false); (this one can even be used before .setOnClickListener().

These two methods have the advantage that button animations get disabled too and they might work better if you have multiple clickable objects stacked on top of each other.
They are in the View class and can be called on a ListView too (but i didn't try that "live").

boolean isPlaying = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isPlaying){
//do stuff
} else {
//do someting else
}
}
});
}

huangapple
  • 本文由 发表于 2020年3月16日 17:48:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/60703634.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定