簡體   English   中英

Android如何檢查語言是否已更改

[英]Android how to Check if language has changed

我使用MVVM概念創建了一個應用程序,在Activityviewpager fragment 當我在應用程序中更改語言時,某些數據已更改,但webservices顯示的數據未更改。 所以我嘗試在每個Activity添加android:configChanges="locale" ,並且已經在Activity類中添加了以下代碼:

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

但是它使我的UI重新創建每個配置更改,包括“ Screen Rotation而我只想在更改語言時重新創建。

這是我的片段代碼:

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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM