viewpager
中的片段在销毁并重新创建时不会更新UI。(当您滑回到该片段时,视图不会更新)
仅在第一次创建片段时更新视图。 这就是我在项目中实现MVVM的方式。
活动分类:
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
Fragment frag = new NewLocationFrag();
Bundle fragBundle = new Bundle();
pagerAdapter.addFrag(frag);
}
类片段:
public class NewLocationFrag extends Fragment {
private WeatherDataViewModel mWeatherDataViewMode;
public void onCreate(@Nullable Bundle savedInstanceState) {
mWeatherDataViewMode = ViewModelProviders.of(this).get(WeatherDataViewModel.class);
mWeatherDataViewMode.init(mLoc,getActivity());
mWeatherDataViewMode.getmForecastDataMutable().observe(this, new Observer<ArrayList<ForecastData>>() {
@Override
public void onChanged(@Nullable ArrayList<ForecastData> forecastData) {
Timber.d("mvvm");
// update UI like setText...
}
});
}
class WeatherDataViewModel.class
public class WeatherDataViewModel extends ViewModel {
private MutableLiveData<Map<String, String>> mWeatherDataMapMutable;// = new MutableLiveData<>();
private MutableLiveData<ArrayList<ForecastData>> mForecastDataMutable;// = new MutableLiveData<>();
private WeatherDataRepository mRepo;
private Context mCtx;
public void init(@NonNull String mLoc, @NonNull Context ctx) {
mCtx = ctx;
mRepo = WeatherDataRepository.getInstance(mCtx);
mRepo.setWeatherData(mLoc);
mForecastDataMutable = mRepo.getForecastDataMutable();
mWeatherDataMapMutable = mRepo.getWeatherDataMap();
}
public MutableLiveData<Map<String, String>> getmWeatherDataMapMutable() {
Timber.d("mvvm");
return mRepo.getWeatherDataMap();
}
public MutableLiveData<ArrayList<ForecastData>> getmForecastDataMutable() {
Timber.d("mvvm");
return mRepo.getForecastDataMutable();
}
}
存储库
public class WeatherDataRepository {
private MutableLiveData<Map<String,String>> weatherDataMapMutable = new MutableLiveData<Map<String,String>>();
private MutableLiveData<ArrayList<ForecastData>> forecastDataMutable = new MutableLiveData<ArrayList<ForecastData>>();
private static WeatherDataRepository instance;
private Context mCtx;
public WeatherDataRepository(Context ctx){
mCtx = ctx;
}
public static WeatherDataRepository getInstance(Context ctx){
if(instance == null){
instance = new WeatherDataRepository(ctx);
}
return instance;
}
// Pretend to get data from a webservice or online source
public void setWeatherData(@NonNull String mLoc){
RetrofitQuery query = new RetrofitQuery(mCtx);
//workaround
Call<predictWeatherData> call2= query.GetWeatherData(mLoc);
Map<String,String> weatherDataMap;
call2.enqueue(new Callback<predictWeatherData>(){
@Override
public void onResponse(@NonNull Call<predictWeatherData> call, @NonNull Response<predictWeatherData> response) {
if(response.isSuccessful()){
predictWeatherData weatherStatus = Objects.requireNonNull(response.body());
weatherDataMapMutable = new MutableLiveData<>();
weatherDataMapMutable.setValue(Objects.requireNonNull(CreateWeatherDataMap.
createWeatherData(weatherStatus)));
forecastDataMutable = new MutableLiveData<>();
forecastDataMutable.setValue(Objects.requireNonNull(CreateWeatherDataMap.
createOneWeekWeatherData(weatherStatus)));
}
}
@Override
public void onFailure(Call<predictWeatherData> call, Throwable t) {
if(t instanceof IOException){
Timber.d("Retrofit: IOException ");
}
Timber.d(" Retrofit: query failed: parsing error");
}
});
}
public MutableLiveData<Map<String,String>> getWeatherDataMap(){
Timber.d("mvvm");
return weatherDataMapMutable;
}
public MutableLiveData<ArrayList<ForecastData>> getForecastDataMutable(){
Timber.d("mvvm");
return forecastDataMutable;
}
}
我最初是用MVC架构实现的,并且可以正常工作。 不知道这种新方法有什么问题。