我正在构建一个应用程序,我需要在运行时更改其主题。为此,我在主要活动中调用recreate,如下所示:
Log.d("[UI]", "Reloading " + settings.getStydersStyle().name());
setTheme(settings.getStydersStyle().getTheme());
recreate();
视图主题已正确更改,但是视图的侦听器没有更改,并且当我滑动或单击它们时没有任何反应,它们确实在重新加载之前起作用。这是我的片段之一:
public class HomeFragment extends Fragment {
private MainActivity mainActivity;
public HomeFragment(){
this.mainActivity = MainActivity.getMainActivity();
}
public HomeFragment(MainActivity mainActivity){
this.mainActivity = mainActivity;
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Log.d("[UI]", "Building home fragment...");
View colorOne = view.findViewById(R.id.colorOne);
colorOne.getLayoutParams().height = colorOne.getMeasuredWidth();
colorOne.requestLayout();
bindAllSeekBars(view);
bindSwitch(view);
bindColorIcons(view);
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
return view;
}
private void bindSwitch(View fragment){
SwitchCompat switchCompat = fragment.findViewById(R.id.borderSwitch);
switchCompat.setChecked(mainActivity.getSettings().isBorderEnabled());
if(!switchCompat.isChecked()){
fragment.findViewById(R.id.appSettingsTwo).setVisibility(View.GONE);
fragment.findViewById(R.id.appSettingsThree).setVisibility(View.GONE);
fragment.findViewById(R.id.appSettingsFour).setVisibility(View.GONE);
fragment.findViewById(R.id.appSettingsFive).setVisibility(View.GONE);
}
switchCompat.setOnCheckedChangeListener((compoundButton, b) -> {
if(fragment.findViewById(R.id.appSettingsTwo).getVisibility() == View.VISIBLE){
fragment.findViewById(R.id.appSettingsTwo).setVisibility(View.GONE);
fragment.findViewById(R.id.appSettingsThree).setVisibility(View.GONE);
fragment.findViewById(R.id.appSettingsFour).setVisibility(View.GONE);
fragment.findViewById(R.id.appSettingsFive).setVisibility(View.GONE);
mainActivity.getSettings().setBorderEnabled(false);
}else {
fragment.findViewById(R.id.appSettingsTwo).setVisibility(View.VISIBLE);
fragment.findViewById(R.id.appSettingsThree).setVisibility(View.VISIBLE);
fragment.findViewById(R.id.appSettingsFour).setVisibility(View.VISIBLE);
fragment.findViewById(R.id.appSettingsFive).setVisibility(View.VISIBLE);
mainActivity.getSettings().setBorderEnabled(true);
bindAllSeekBars(fragment);
}
});
}
private void bindAllSeekBars(View fragment){
bindSeekBar(fragment, R.id.seekBarTwo, WallpaperSetting.BORDER_SPEED);
bindSeekBar(fragment, R.id.seekBarFive, WallpaperSetting.BORDER_SIZE_HOME);
bindSeekBar(fragment, R.id.seekBarSix, WallpaperSetting.BORDER_SIZE_LOCK);
bindSeekBar(fragment, R.id.seekBarEight, WallpaperSetting.RADIUS_BOTTOM);
bindSeekBar(fragment, R.id.seekBarSeven, WallpaperSetting.RADIUS_TOP);
bindSeekBar(fragment, R.id.seekBarThree, WallpaperSetting.BORDER_BRIGHTNESS);
}
private void bindSeekBar(View fragment, int seekBarId, final String preference) {
SeekBar speedSeekBar = fragment.findViewById(seekBarId);
switch (preference){
case WallpaperSetting.BORDER_SPEED:
speedSeekBar.setProgress(mainActivity.getSettings().getBorderSpeed());
break;
case WallpaperSetting.BORDER_SIZE_HOME:
speedSeekBar.setProgress(mainActivity.getSettings().getBorderSizeHomeScreen());
break;
case WallpaperSetting.BORDER_SIZE_LOCK:
speedSeekBar.setProgress(mainActivity.getSettings().getBorderSizeLockScreen());
break;
case WallpaperSetting.RADIUS_BOTTOM:
speedSeekBar.setProgress(mainActivity.getSettings().getRadiusBottom());
break;
case WallpaperSetting.RADIUS_TOP:
speedSeekBar.setProgress(mainActivity.getSettings().getRadiusTop());
break;
case WallpaperSetting.BORDER_BRIGHTNESS:
speedSeekBar.setProgress(mainActivity.getSettings().getImageBorderBrightness());
break;
}
speedSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
switch (preference) {
case WallpaperSetting.BORDER_SPEED:
mainActivity.getSettings().setBorderSpeed(i);
break;
case WallpaperSetting.BORDER_SIZE_HOME:
mainActivity.getSettings().setBorderSizeHomeScreen(i);
break;
case WallpaperSetting.BORDER_SIZE_LOCK:
mainActivity.getSettings().setBorderSizeLockScreen(i);
break;
case WallpaperSetting.RADIUS_BOTTOM:
mainActivity.getSettings().setRadiusBottom(i);
break;
case WallpaperSetting.RADIUS_TOP:
mainActivity.getSettings().setRadiusTop(i);
break;
case WallpaperSetting.BORDER_BRIGHTNESS:
mainActivity.getSettings().
setImageBorderBrightness(i);
break;
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
private void bindColorIcons(View fragment){
for (int x = 0; x < 6; x++){
switch (x){
case 0:
bindColor(fragment.findViewById(R.id.colorOne), x);
break;
case 1:
bindColor(fragment.findViewById(R.id.colorTwo), x);
break;
case 2:
bindColor(fragment.findViewById(R.id.colorThree), x);
break;
case 3:
bindColor(fragment.findViewById(R.id.colorFour), x);
break;
case 4:
bindColor(fragment.findViewById(R.id.colorFive), x);
break;
case 5:
bindColor(fragment.findViewById(R.id.colorSix), x);
break;
}
}
}
private void bindColor(CardView cardView, int x){
cardView.setCardBackgroundColor(mainActivity.getSettings().getColors()[x]);
cardView.setOnClickListener(view -> new ColorPickerDialog()
.withColor(mainActivity.getSettings().getColors()[x])
.withPresets(Color.BLUE, Color.YELLOW, Color.RED, Color.MAGENTA, Color.GREEN, Color.BLACK, Color.GRAY, Color.LTGRAY, Color.WHITE)
.withListener((pickerView, color) -> {
cardView.setCardBackgroundColor(color);
mainActivity.getSettings().getColors()[x] = color;
mainActivity.getSettings().getSettingsChangeListener().onSettingChanged(WallpaperSetting.NEW_COLORS);
})
.withTheme(R.style.ColorPickerDialogDark)
.show(mainActivity.getSupportFragmentManager(), "ColorPicker"));
}
}
有什么想法吗?