方向更改后,图像按钮状态不正确

时间:2019-11-01 14:05:04

标签: java android orientation-changes

我从服务中获得一个布尔值,如果正在播放一首歌曲,则返回true。

发生方向更改后,我在onResume()中调用updateSongInfo()。

这些日志返回服务类中的所有值。

Log.d(TAG, "songIndex: " + songIndex);
Log.d(TAG,"songList: " + songList);
Log.d(TAG,"isPlaying: " + isPlaying);
当我更改方向时,

isPlaying 在logcat中返回true。

但是由于某种原因,当我更改方向时未调用此 if else语句,它仅在将应用程序置于前台并再次打开时才提供正确的按钮。

但是如何在方向改变时获得正确的图像?

这不会在方向更改上调用

if (isPlaying){
          mBtnPlayPause.setImageResource(R.drawable.ic_action_pause_white);
          Log.d(TAG,"value test: " + isPlaying);
}else{
          mBtnPlayPause.setImageResource(R.drawable.ic_action_play);
          Log.d(TAG,"value test: " + isPlaying);
}

updateSongInfo()中的代码

private void updateSongInfo(){
    if (Main.getInstance().mServiceIsBound) {
        if (songList != null && !songList.isEmpty()) {
            if (mSlideUpPanel.getPanelState() == SlidingUpPanelLayout.PanelState.HIDDEN){
                mSlideUpPanel.setPanelState(SlidingUpPanelLayout.PanelState.EXPANDED);
            }
            songList = Main.getInstance().mediaPlayerService.getSongList();
            songIndex = Main.getInstance().mediaPlayerService.getSongIndex();
            isPlaying = Main.getInstance().mediaPlayerService.mpIsPlaying();

            Log.d(TAG, "songIndex: " + songIndex);
            Log.d(TAG,"songList: " + songList);
            Log.d(TAG,"isPlaying: " + isPlaying);

            tvSongTitle.setText(songList.get(songIndex).getTitle());
            tvArtistName.setText(songList.get(songIndex).getArtist());
            tvAlbumName.setText(songList.get(songIndex).getAlbum());

            String songListSize = (songIndex + 1) + "/" + songList.size();
            tvSongListSize.setText(songListSize);

            loadAlbumArt();
            loadAlbumArtBottom();
            startAnimation();

            updateProgressBar();

            if (isPlaying){
                mBtnPlayPause.setImageResource(R.drawable.ic_action_pause_white);
                Log.d(TAG,"value test: " + isPlaying);
            }else{
                mBtnPlayPause.setImageResource(R.drawable.ic_action_play);
                Log.d(TAG,"value test: " + isPlaying);
            }

        }
 }

0 个答案:

没有答案