LiveData值已更改但未触发可观察的onchanged方法? (数据绑定MVVM)

时间:2018-03-03 07:40:47

标签: android mvvm data-binding observable android-livedata

在对回收器视图进行分页时,API中的值将更新为第2页上的LiveData,但不会触发可观察的onChanged方法。由于数组的旧大小为3,更新的数组大小为6。 这是我的代码片段:

以下是我的观察,我的liveata没有跟踪:

//inMyActivity observable:

private void observeViewModel() {
    // Observe project data
    newsFeedViewModel.getObservable().observe(this, new Observer<NewsFeedModel>() {
        @Override
        public void onChanged(@Nullable NewsFeedModel newsFeedModel) {
            Log.d("eee", "onChanged: sadere");
            if (newsFeedModel != null) {
                Log.d("eee", "onChanged: sadsa");
                newsFeedViewModel.setModel(newsFeedModel);
                newsListModels = newsFeedModel.getNewsListModels();
                if (newsListModels != null && newsListModels.size() > 0) {
                    setupRecyclerView(recyclerView, newsListModels);
                }
            }
        }
    });
 }

这是我的ViewModel,它将模型设置为视图:

public LiveData<NewsFeedModel> liveData;
public ObservableField<NewsFeedModel> observableField = new ObservableField<>();

NewsFeedManager newsFeedManager;
public NewsFeedViewModel(Application application,int page) {
    super(application);
    Log.d("eee", "NewsFeedViewModel: page="+page);
    newsFeedManager=new NewsFeedManager(application);
    callNextPageResult(page);
}

public LiveData<NewsFeedModel> getObservable() {
    return liveData;
}
public void setModel(NewsFeedModel model) {
    this.observableField.set(model);
}

NewManager:

public class NewsFeedManager {
Context context;
Call<NewsFeedModel> modelCall;

public NewsFeedManager(Context context) {
    this.context = context;
}

public LiveData<NewsFeedModel> getNewsFeedRequest(final int page) {
    final MutableLiveData<NewsFeedModel> data = new MutableLiveData<>();
    final PreferenceManager preferenceManager = new PreferenceManager(context);

    if (!AppUtils.isNetworkAvailable(context)) {
        Toast.makeText(context, R.string.no_internet, Toast.LENGTH_SHORT).show();
        String dataStr = preferenceManager.getNewsFeedData();
        if (!TextUtils.isEmpty(dataStr)) {
            NewsFeedModel newsFeedModel = new Gson().fromJson(dataStr, NewsFeedModel.class);
            data.setValue(newsFeedModel);
            return data;
        }
        return null;
    }

    NewsInterface newsInterface = NetworkGenerator.getAuthClient(context).create(NewsInterface.class);
    modelCall = newsInterface.geNewsFeedResponse(page);
    BaseManager baseManager = new BaseManager(context);
    baseManager.sendRequest(modelCall, new DataCallBackListener() {
        @Override
        public void onResponse(Object body) {
            if (body instanceof NewsFeedModel) {
                NewsFeedModel newsFeedModel = (NewsFeedModel) body;
                switch (newsFeedModel.getStatus()) {
                    case 200:
                        if(page==1){
                            String response = new GsonBuilder().create().toJson(newsFeedModel);
                            new PreferenceManager(context).setNewsFeedData(response);
                            data.setValue(newsFeedModel);
                            Log.d("eee", "onResponse: p1="+page);
                        }else {
                            Log.d("eee", "onResponse: p4341="+page);
                            String dataStr = preferenceManager.getNewsFeedData();
                            if (!TextUtils.isEmpty(dataStr)) {
                                NewsFeedModel newsFeedModel_pack = new Gson().fromJson(dataStr, NewsFeedModel.class);
                                Log.d("eee", "onResponse: before"+newsFeedModel_pack.getNewsListModels().size());
                                newsFeedModel_pack.getNewsListModels().addAll(newsFeedModel.getNewsListModels());

                                Log.d("eee", "onResponse: after"+newsFeedModel_pack.getNewsListModels().size());

                                String response = new GsonBuilder().create().toJson(newsFeedModel_pack);
                                new PreferenceManager(context).setNewsFeedData(response);
                                data.setValue(newsFeedModel_pack);
                            }
                        }

                        break;
                    default:
                        data.setValue(newsFeedModel);
                        break;
                }
            }
        }

        @Override
        public void onError(Throwable t) {
            data.setValue(null);
        }
    });
    return data;
}


public void cancelRequest() {
    modelCall.cancel();
}
}

Recycler onLoadMore Listener:

NewsFeedAdapter adapter;
private void setupRecyclerView(final RecyclerView recyclerView, final List<NewsListModel> newsListModels) {
    if (page==1) {
        adapter = new NewsFeedAdapter(recyclerView,linearLayoutManager,getContext(), newsListModels);
        recyclerView.setAdapter(adapter);

        adapter.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                Log.d("eee", "onLoadMore: 123");
                if (newsListModels!=null) {
                    Log.d("eee", "onLoadMore: adss123");
                    page++;
                    newsFeedViewModel.callNextPageResult(page);

                }

            }
        });
    } else {
        Log.d("eee", "onLoadMore: 5676");
        recyclerView.post(new Runnable() {
            @Override
            public void run() {
                Log.d("eee", "onLoadMore: 54564564");
                adapter.notifyDataSetChanged();
                adapter.setLoaded();
            }
        });
    }


}

0 个答案:

没有答案