私は、垂直RecyclerView内で別々のビューとして機能する複数のViewHoldersを持っています。 私は新しいArchitecture Components ViewModelで練習しています。RecyclerViewアダプタ内のアーキテクチャコンポーネントViewModelの使用方法は?
私のViewModelの中に私は観察したいMutableLiveDataのリストをいくつか持っています。しかし、どのように私は活動を漏らすことなく
ViewModelProviders.of((AppCompatActivity)getActivity()).get(FilterViewModel.class)
と
mFilterViewModel.getCountries().observe(this, new Observer<ArrayList<TagModel>>() {
@Override
public void onChanged(@Nullable ArrayList<TagModel> tagModels) {
}
});
を呼び出すか、アダプターの内部でアクティビティを保存することができます。
私のViewModel
public class FilterViewModel extends ViewModel {
private final MutableLiveData<ArrayList<TagModel>> mCountries;
private final MutableLiveData<ArrayList<TagModel>> mSelectedCountryProvinceList;
private final MutableLiveData<ArrayList<TagModel>> mDistanceList;
public FilterViewModel(){
mCountries = new MutableLiveData<>();
mSelectedCountryProvinceList = new MutableLiveData<>();
mDistanceList = new MutableLiveData<>();
TagStore.getInstance().subscribe(new StoreObserver<TagSearchList>() {
@Override
public void update(TagSearchList object) {
mCountries.setValue(object.getCountries());
}
@Override
public void update(int id, TagSearchList object) {
if (id == 5){
TagStore.getInstance().unSubcribe(this);
update(object);
}
}
@Override
public void error(String error) {
}
}).get(5,"parent");
TagStore.getInstance().subscribe(new StoreObserver<TagSearchList>() {
@Override
public void update(TagSearchList object) {
mSelectedCountryProvinceList.setValue(object.toList());
}
@Override
public void update(int id, TagSearchList object) {
if (id == 6){
TagStore.getInstance().unSubcribe(this);
update(object);
}
}
@Override
public void error(String error) {
}
}).get(6,"parent");
TagStore.getInstance().subscribe(new StoreObserver<TagSearchList>() {
@Override
public void update(TagSearchList object) {
mDistanceList.setValue(object.toList());
}
@Override
public void update(int id, TagSearchList object) {
if (id == 51){
TagStore.getInstance().unSubcribe(this);
update(object);
}
}
@Override
public void error(String error) {
}
}).get(51,"parent");
}
public void selectCountry(final TagModel country){
TagStore.getInstance().subscribe(new StoreObserver<TagSearchList>() {
@Override
public void update(TagSearchList object) {
mSelectedCountryProvinceList.setValue(object.toList());
}
@Override
public void update(int id, TagSearchList object) {
if (id == country.getId()){
TagStore.getInstance().unSubcribe(this);
update(object);
}
}
@Override
public void error(String error) {
}
}).get(country.getId(),"parent");
}
public LiveData<ArrayList<TagModel>> getCountries(){
return mCountries;
}
public LiveData<ArrayList<TagModel>> getDistances(){
return mDistanceList;
}
public LiveData<ArrayList<TagModel>> getProvinces(){
return mSelectedCountryProvinceList;
}
}
こんにちは。あなたはAndroid Data Bindingを代わりに使用したいのですが? – JuliaKo
こんにちは@JuliaKo、MVVMに私のアプリをリファクタリングする私の意図ではありません。現時点では私はMVPを使用しています。 MVPフレームワークでViewModelクラスを使用して、ライフサイクルの問題を解決し、データと不要なObservableをリロードします。 –
私はあなたが必要とするものはバインディングアダプタを実装することだと思うし、Googleチームはデータバインディングライフサイクルを意識させるが、それが起こるためには、ライフサイクルは1.0の安定したAPIをヒットする必要がある。 https://github.com/googlesamples/android-architecture-components/issues/34 – JuliaKo