gpt4 book ai didi

java - Android 如何检查语言是否已更改

转载 作者:行者123 更新时间:2023-12-02 01:06:48 27 4
gpt4 key购买 nike

我使用MVVM概念创建了一个应用程序,在我的Activity中有viewpagerfragment。当我更改应用程序中的语言时,一些数据发生了变化,但网络服务显示的数据没有变化。所以我尝试在每个 Activity 中添加 android:configChanges="locale" ,并且我已经在我的 Activity 类中添加了此代码:

  @Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
recreate();
}
}

但它使我的 UI 重新创建每个配置更改,包括屏幕旋转,而我只想在语言更改时重新创建。

这是我的 fragment 代码:

public class CatalogueFragment extends BaseFragment<FragmentCatalogueBinding, CatalogueViewModel>
implements CatalogueNavigator, CatalogueAdapter.CatalogueAdapterListener {

@Inject
CatalogueAdapter adapter;

@Inject
LinearLayoutManager mLayoutManager;

@Inject
ViewModelProvider.Factory factory;

FragmentCatalogueBinding fragmentCatalogueBinding;

private CatalogueViewModel catalogueViewModel;

public static CatalogueFragment newInstance(int Pos) {
Bundle args = new Bundle();
CatalogueFragment fragment = new CatalogueFragment();
fragment.setArguments(args);
return fragment;
}

@Override
public int getBindingVariable() {
return BR.viewModel;
}

@Override
public int getLayoutId() {
return R.layout.fragment_catalogue;
}

@Override
public CatalogueViewModel getViewModel() {
catalogueViewModel = ViewModelProviders.of(this, factory).get(CatalogueViewModel.class);
return catalogueViewModel;
}

@Override
public void handleError(String error) {
// handle error
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

catalogueViewModel.setNavigator(this);
adapter.setListener(this);
}

@Override
public void onRetryClick() {
catalogueViewModel.fetchData();
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
fragmentCatalogueBinding = getViewDataBinding();
setUp();
}

@Override
public void updateData(List<Movie> movieList) {
adapter.addItems(movieList);
}

private void setUp() {
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
fragmentCatalogueBinding.recyclerCatalogue.setLayoutManager(mLayoutManager);
fragmentCatalogueBinding.recyclerCatalogue.setItemAnimator(new DefaultItemAnimator());
fragmentCatalogueBinding.recyclerCatalogue.setAdapter(adapter);
}
}

这是我的 ViewModel

 public class CatalogueViewModel extends BaseViewModel {

private final MutableLiveData<List<Movie>> movieListLiveData;

public CatalogueViewModel(DataManager dataManager, SchedulerProvider schedulerProvider) {
super(dataManager, schedulerProvider);
movieListLiveData = new MutableLiveData<>();
fetchData();
}

public void fetchData() {
setIsLoading(true);
getCompositeDisposable().add(getDataManager()
.getApiHelper().doMovieCall(URLConfig.API_KEY, getDataManager().getLanguage())
.subscribeOn(getSchedulerProvider().io())
.observeOn(getSchedulerProvider().ui())
.subscribe(movieResponse -> {
if (movieResponse != null && movieResponse.getResults() != null) {
movieListLiveData.setValue(movieResponse.getResults());
}
setIsLoading(false);
}, throwable -> {
setIsLoading(false);
// getNavigator().handleError(throwable);
}));
}

public LiveData<List<Movie>> getMovieListLiveData() {
return movieListLiveData;
}
}

谁能告诉我我错在哪里?非常感谢

最佳答案

您可以使用:ACTION_LOCALE_CHANGED

这里是一个例子:

private BroadcastReceiver mLangReceiver = null;

protected BroadcastReceiver setupLangReceiver(){

if(mLangReceiver == null) {

mLangReceiver = new BroadcastReceiver() {

@Override
public void onReceive(Context context, Intent intent) {
// do what you want
}

};

registerReceiver(mLangReceiver, new IntentFilter(Intent.ACTION_LOCALE_CHANGED));
}

return mLangReceiver;
}

关于java - Android 如何检查语言是否已更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57722900/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com