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

标签 java android mvvm configuration android-mvvm

我使用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/

相关文章:

java - 如何实现在 X 分钟内未收到任何事件后发出的 Flink 事件时间触发器

java - JTable fireTableDataChanged() 方法不刷新表

java - 如何通过我的应用程序通过 Intent 将视频数据共享/发送到 Tik-Tok 应用程序,就像画廊共享一样

android - 如何在 Phonegap 中针对不同的主题/布局

mvvm - Caliburn.Micro、MVVM 和业务层

android - 如何在 Activity 中的 API 调用上从 ViewModel 获取错误?

wpf - 如何自动缩放一组控件的字体大小?

java - 颠覆性 Eclipse 错误 - 切换操作失败。

Java NumberFormat 忽略美国语言环境中的逗号?

android - Flutter build apk 有 bug 而 debug 很完美