自动隐藏媒体播放器布局在android

时间:2021-10-03 19:01:28

I am designing a media player with a custom layout. I want the interface to disappear after 16s of inactivity. It should reappear if the user touches the screen. The code snippet is given below:

我正在设计一个自定义布局的媒体播放器。我想让界面在16秒后消失。如果用户触碰屏幕,它应该会重新出现。代码片段如下:

 public void showhideControllers(int n) {
    if (n == 1) {
        /* make layout invisible */

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                volumeBar.setVisibility(View.INVISIBLE);
                audioControllView.setVisibility(View.INVISIBLE);
                topBar.setVisibility(View.INVISIBLE);
            }
        }, 16000);

    } else {
        /* make layout visible */           
        volumeBar.setVisibility(View.VISIBLE);
        topBar.setVisibility(View.VISIBLE);
        audioControllView.setVisibility(View.VISIBLE);

        showhideControllers(1);
    }

}

    @Override
public void onUserInteraction() {
    super.onUserInteraction();
    showhideControllers(2);
}

Inside the onCreate(), I am starting the timer by calling showhideControllers(1);. Now, when I click on the screen the layout reappears and the timer is reset. But if I randomly go on clicking the screen the timer is not reset after every click and the layout disappears after 16s. Can you tell me what am I doing wrong ?

在onCreate()中,我通过调用showhideControllers(1)来启动计时器。现在,当我点击屏幕时,布局重新出现,计时器重新设置。但如果我随机地点击屏幕计时器不会在每次点击后重置布局在16秒后消失。你能告诉我我做错了什么吗?

1 个解决方案

#1


11  

Sorry for late response. But here is the solution. I was having similar issue. So I have made following changes in your code, please try this and let me know if it helps you.

抱歉迟回复。但这就是解决方案。我也有类似的问题。所以我对你的代码做了以下修改,请尝试一下,如果对你有帮助的话,请告诉我。

private Runnable hideControllerThread = new Runnable() {

    public void run() {
            volumeBar.setVisibility(View.GONE);
            audioControllView.setVisibility(View.GONE);
            topBar.setVisibility(View.GONE);
    }
};


public void hideControllers() {
        hidehandler.postDelayed(hideControllerThread, 15000);
}

public void showControllers() {
        volumeBar.setVisibility(View.VISIBLE);
        topBar.setVisibility(View.VISIBLE);
        audioControllView.setVisibility(View.VISIBLE);
        hidehandler.removeCallbacks(hideControllerThread);
        hideControllers();
}

@Override
public void onUserInteraction() {
        super.onUserInteraction();

        if (audioControllView.getVisibility() == View.VISIBLE) {
            hidehandler.removeCallbacks(hideControllerThread);
            hideControllers();
        } else {
            showControllers();
        }
}

#1


11  

Sorry for late response. But here is the solution. I was having similar issue. So I have made following changes in your code, please try this and let me know if it helps you.

抱歉迟回复。但这就是解决方案。我也有类似的问题。所以我对你的代码做了以下修改,请尝试一下,如果对你有帮助的话,请告诉我。

private Runnable hideControllerThread = new Runnable() {

    public void run() {
            volumeBar.setVisibility(View.GONE);
            audioControllView.setVisibility(View.GONE);
            topBar.setVisibility(View.GONE);
    }
};


public void hideControllers() {
        hidehandler.postDelayed(hideControllerThread, 15000);
}

public void showControllers() {
        volumeBar.setVisibility(View.VISIBLE);
        topBar.setVisibility(View.VISIBLE);
        audioControllView.setVisibility(View.VISIBLE);
        hidehandler.removeCallbacks(hideControllerThread);
        hideControllers();
}

@Override
public void onUserInteraction() {
        super.onUserInteraction();

        if (audioControllView.getVisibility() == View.VISIBLE) {
            hidehandler.removeCallbacks(hideControllerThread);
            hideControllers();
        } else {
            showControllers();
        }
}