在分页数据上应用过滤器时,同时调用两种方法LoadInitial和LoadAfter

时间:2018-10-26 12:09:56

标签: android pagination

我正在使用分页库从网络中获取数据。从网络中获取数据后,就像电子商务网站一样,我在分页数据上应用了过滤器。 在对其应用过滤器并再次使用分页从网络中获取数据后,首先调用LoadInitial方法,同时调用LoadAfter方法。在滚动数据之前,我不希望调用LoadAfter方法。

在我的片段中,我将分页应用为:

public void setPagination{
    FilterModel viewModel = ViewModelProviders.of(this).get(FilterModel.class);
    viewModel.getData(mPatientModel, filterList, mActivity).observe(this, new Observer<PagedList<Patient>>() {
        @Override
        public void onChanged(@Nullable PagedList<Patient> pagedList) {
            if (pagedList != null)
                mPatientAdapter.submitList(pagedList);
        }
    });
}

FilterModel类:

public class FilterModel extends ViewModel {

private static final String TAG = FilterModel.class.getName();
LiveData<NetworkState> networkState;
LiveData<PagedList<Patient>> getData(MyPatientModel model, ArrayList<SelectedFilterList> filterList, Activity activity) {
    Log.d(TAG, "getData");
    PatientDataSourceFactory patientDataSourceFactory = new PatientDataSourceFactory(model, filterList, activity);

    networkState = Transformations.switchMap(patientDataSourceFactory.getMutableLiveData(), new Function<ItemKeyedUserDataSource, LiveData<NetworkState>>() {
        @Override
        public LiveData<NetworkState> apply(ItemKeyedUserDataSource dataSource) {
            return dataSource.getNetworkState();
        }
    });


    PagedList.Config pagedListConfig =
            (new PagedList.Config.Builder()).setEnablePlaceholders(false)
                    .setInitialLoadSizeHint(10)
                    .setPageSize(2).build();

    return new LivePagedListBuilder<>(patientDataSourceFactory, pagedListConfig).build();
}
}

DataSourceFactory类如下:

public class PatientDataSourceFactory extends DataSource.Factory<Long, Patient> {

private static final String TAG = PatientDataSourceFactory.class.getName();
private MutableLiveData<ItemKeyedUserDataSource> mutableLiveData;
private MyPatientModel mModel;
private ArrayList<SelectedFilterList> mFilteredList;
private Activity mActivity;

PatientDataSourceFactory(MyPatientModel model, ArrayList<SelectedFilterList> filterList, Activity activity) {
    Log.d(TAG, "PatientDataSourceFactory");
    this.mutableLiveData = new MutableLiveData<>();
    mFilteredList = filterList;
    mModel = model;
    mActivity = activity;
}

@Override
public DataSource<Long, Patient> create() {
    Log.d(TAG, "create");
    ItemKeyedUserDataSource itemKeyedUserDataSource = new ItemKeyedUserDataSource(mModel, mFilteredList, mActivity);
    mModel.getDataSourceObject(itemKeyedUserDataSource);
    mutableLiveData.postValue(itemKeyedUserDataSource);
    return itemKeyedUserDataSource;
}

MutableLiveData<ItemKeyedUserDataSource> getMutableLiveData() {
    Log.d(TAG, "getMutableLiveData");
    return mutableLiveData;
}
}

ItemKeyedDataSourec类:

public class ItemKeyedUserDataSource extends ItemKeyedDataSource<Long, Patient> {

private static final String TAG = ItemKeyedUserDataSource.class.getName();
private CountDownLatch mLoadInitialCountDownLatch, mLoadAfterCountDownLatch;
private List<Patient> mPatientList = null;
private int pageSize = 1;
private MutableLiveData<NetworkState> networkState;
private MutableLiveData<NetworkState> initialLoading;
private MyPatientModel mPatientModel;
private ArrayList<SelectedFilterList> mFilterList;
private Activity mActivity;
private boolean mIsSendPatientListToAdapter;


ItemKeyedUserDataSource(MyPatientModel patientModel, ArrayList<SelectedFilterList> filterList, Activity activity) {
    Log.d(TAG, "ItemKeyedUserDataSource");
    mPatientModel = patientModel;
    networkState = new MutableLiveData<>();
    initialLoading = new MutableLiveData<>();
    mFilterList = filterList;
    mActivity = activity;
}

MutableLiveData<NetworkState> getNetworkState() {
    return networkState;
}

@Override
public void loadInitial(@NonNull LoadInitialParams<Long> params, @NonNull final LoadInitialCallback<Patient> callback) {
    Log.d(TAG, "loadInitial");
    mLoadInitialCountDownLatch = new CountDownLatch(1);
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Log.d("MyCountDown", "await");
                mLoadInitialCountDownLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (mActivity != null)
                mActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        initialLoading.postValue(NetworkState.LOADED);
                        networkState.postValue(NetworkState.LOADED);
                        Log.d("MyCountDown", "callback");
                        Log.d("MyCountDown", "callback Patient List Size is: " + mPatientList.size());
                        callback.onResult(mPatientList);
                    }
                });
        }
    }).start();

    initialLoading.postValue(NetworkState.LOADING);
    networkState.postValue(NetworkState.LOADING);
    try {
        Log.d("MyCountDown", "postRequestForPatientList");
        mPatientModel.postRequestForPatientList(pageSize, mFilterList);
    } catch (Exception e) {
        e.printStackTrace();
        Log.d(TAG, "Exception occur: " + e.toString());
        initialLoading.postValue(new NetworkState(Status.FAILED, e.toString()));
        networkState.postValue(new NetworkState(Status.FAILED, e.toString()));
    }
}

@Override
public void loadAfter(@NonNull LoadParams<Long> params, @NonNull final LoadCallback<Patient> callback) {
    Log.d("MyCountDown", "loadAfter");
    networkState.postValue(NetworkState.LOADING);
    mLoadAfterCountDownLatch = new CountDownLatch(1);
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                mLoadAfterCountDownLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (mActivity != null)
                mActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (mIsSendPatientListToAdapter) {
                            callback.onResult(mPatientList);
                            networkState.postValue(NetworkState.LOADED);
                        }
                    }
                });
        }
    }).start();
    try {
        pageSize = pageSize + 1;
        Log.d("MyCountDown", "loadAfter hit");
        mPatientModel.postRequestForPatientList(pageSize, mFilterList);
    } catch (Exception e) {
        e.printStackTrace();
        Log.d(TAG, "Exception occur: " + e.toString());
        networkState.postValue(new NetworkState(Status.FAILED, e.toString()));
    }
}

@Override
public void loadBefore(@NonNull LoadParams<Long> params, @NonNull LoadCallback<Patient> callback) {

}

@NonNull
@Override
public Long getKey(@NonNull Patient item) {
    return null;
}

void getPatientData(List<Patient> patientList, boolean sendPatientListToAdapter) {
    mIsSendPatientListToAdapter = sendPatientListToAdapter;
    if (patientList != null)
        mPatientList = patientList;
    else
        mPatientList = new ArrayList<>();
    mLoadInitialCountDownLatch.countDown();
    if (mLoadAfterCountDownLatch != null)
        mLoadAfterCountDownLatch.countDown();
}
}

我在方法getPatientData()的ItemKeyedDataSource类中获得网络响应。

每当我的Fragment启动时,我都会调用类中存在的Method setPagination()。数据已提取并正确显示。

现在,当我应用过滤器时,我再次使用新的过滤器列表调用相同的setPagination()方法,调用LoadInitial方法,同时调用LoadAfter。

我是新手,无法分页,无法找到任何解决方案。请帮忙。谢谢。

0 个答案:

没有答案