android体系结构组件ViewModel和PageKeyedDataSource问题

时间:2018-08-21 06:26:15

标签: android android-architecture-components android-viewmodel android-livedata android-paging

我想要的东西:我想实现google paging library。加载一些数据(假设前10个)。当您向上滚动时,它会调用API并加载更多数据。

问题所在:它正在加载所有数据,而不是某些数据(比如说前10个)。我为此使用了 ViewModel PageKeyedDataSource

我尝试过的操作:代码如下

InProgressFragment.java

    public class InProgressFragment extends BaseFragment implements SalesDashboardView {

    @BindView(R.id.recyclerview_applicants_list)
    RecyclerView applicantsListRecyclerView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_applicants, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this, view);
        setupRecyclerView();
    }

    private void setupRecyclerView() {

        ApplicantsDataAdapter2 adapter = new ApplicantsDataAdapter2();

        applicantsListRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        applicantsListRecyclerView.setHasFixedSize(true);
        applicantsListRecyclerView.setAdapter(adapter);

        SearchLoansInput searchLoansInput = new SearchLoansInput();
        searchLoansInput.setUserName(sessionManager.getMobileNumber());

        adapter.submitList(salesDashboardViewModel.getLoanDetailOutput(searchLoansInput));
    }
}

SalesDashboardViewModel.java

    public class SalesDashboardViewModel extends ViewModel {

    private static final String TAG = SalesDashboardViewModel.class.getSimpleName();

    private int DEFAULT_PER_PAGE = 3;
    private MainThreadExecutor executor;
    private SalesLoginInquiryUseCase mSalesInquiryUseCase;

    @Inject
    public SalesDashboardViewModel(SalesLoginInquiryUseCase salesLoginInquiryUseCase) {
        mSalesInquiryUseCase = salesLoginInquiryUseCase;
    }

    public PagedList<LoanDetailOutput> getLoanDetailOutput(SearchLoansInput searchLoansInput) {

        executor = new MainThreadExecutor();

        SearchDataSource dataSource = new SearchDataSource(searchLoansInput, mSalesInquiryUseCase);

        PagedList.Config config = new PagedList.Config.Builder()
                .setPageSize(DEFAULT_PER_PAGE)
                .setInitialLoadSizeHint(DEFAULT_PER_PAGE * 2)
                .setEnablePlaceholders(true)
                .build();

        PagedList<LoanDetailOutput> loanDetailOutputPagedList =
                new PagedList.Builder<>(dataSource, config)
                        .setFetchExecutor(executor)
                        .setNotifyExecutor(executor)
                        .build();

        return loanDetailOutputPagedList;
    }
}

SearchDataSource.java

    public class SearchDataSource extends PageKeyedDataSource<Integer, LoanDetailOutput> {

    private static final String TAG = SearchDataSource.class.getSimpleName();

    private SearchLoansInput searchLoansInput;
    private SalesLoginInquiryUseCase mSalesInquiryUseCase;

    public SearchDataSource(SearchLoansInput searchLoansInput, SalesLoginInquiryUseCase mSalesInquiryUseCase) {
        this.searchLoansInput = searchLoansInput;
        this.mSalesInquiryUseCase = mSalesInquiryUseCase;
    }

    @Override
    public void loadInitial(@NonNull LoadInitialParams<Integer> params,
                            @NonNull LoadInitialCallback<Integer, LoanDetailOutput> callback) {

        Log.e("mk", "loadInitial, requestedLoadSize: " + params.requestedLoadSize);

        final int page = 1;

        mSalesInquiryUseCase.setSalesLoginInquiryBody(searchLoansInput);

        mSalesInquiryUseCase.execute()

                .subscribe(new DisposableObserver<SearchLoansOutput>() {

                    @Override
                    public void onNext(SearchLoansOutput value) {

                        callback.onResult(
                                value.getLoanDetailList(),
                                0,
                                value.getCountLA(),
                                null,
                                page + 1);

                        Log.e("mk", "loadInitial, List Size: " + value.getLoanDetailList().size());
                        Log.e("mk", "loadInitial, TotalCount: " + value.getCountLA());
                    }

                    @Override
                    public void onError(Throwable throwable) {
                        throwable.printStackTrace();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

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

    }

    @Override
    public void loadAfter(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, LoanDetailOutput> callback) {

        Log.e("mk", "loadAfter, requestedLoadSize: " + params.requestedLoadSize);

        final int page = params.key;

        mSalesInquiryUseCase.setSalesLoginInquiryBody(searchLoansInput);

        mSalesInquiryUseCase.execute()

                .subscribe(new DisposableObserver<SearchLoansOutput>() {

                    @Override
                    public void onNext(SearchLoansOutput value) {

                        callback.onResult(
                                value.getLoanDetailList(),
                                page + 1);

                        Log.e("mk", "loadAfter, List Size: " + value.getLoanDetailList().size());
                    }

                    @Override
                    public void onError(Throwable throwable) {
                        throwable.printStackTrace();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}

ApplicantsDataAdapter2.java

    public class ApplicantsDataAdapter2 extends PagedListAdapter<LoanDetailOutput, ApplicantsDataAdapter2.ApplicantsViewHolder> {

    public ApplicantsDataAdapter2() {
        super(DIFF_CALLBACK);
    }

    @Override
    public ApplicantsDataAdapter2.ApplicantsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_applicant, parent, false);

        return new ApplicantsDataAdapter2.ApplicantsViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ApplicantsDataAdapter2.ApplicantsViewHolder holder, int position) {
        holder.bindView(getItem(position));
    }

    public class ApplicantsViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_item_applicant_name)
        TextView mApplicantNameTextView;

        @BindView(R.id.tv_item_applicant_status)
        TextView mApplicantStatusTextView;

        @BindView(R.id.tv_item_applicant_creation_date)
        TextView mApplicantDateCreationTextView;

        ApplicantsViewHolder(View itemView) {
            super(itemView);

            ButterKnife.bind(this, itemView);
        }

        void bindView(LoanDetailOutput loanDetailOutput) {

            if (loanDetailOutput != null) {

                mApplicantNameTextView.setText(loanDetailOutput.getFirstName());
                mApplicantStatusTextView.setText(loanDetailOutput.getStatus().getDisplayName());

                SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy");
                String createdDate = null;

                if (loanDetailOutput.getCreatedDate() != null) {
                    createdDate = String.valueOf(sdf.format(loanDetailOutput.getCreatedDate()));
                }
                mApplicantDateCreationTextView.setText(createdDate);

            } else {
                mApplicantNameTextView.setText("Loading...");
                mApplicantStatusTextView.setText("Loading...");
                mApplicantDateCreationTextView.setText("Loading...");
            }
        }
    }

    private static DiffUtil.ItemCallback<LoanDetailOutput> DIFF_CALLBACK = new DiffUtil.ItemCallback<LoanDetailOutput>() {

        @Override
        public boolean areItemsTheSame(LoanDetailOutput oldItem, LoanDetailOutput newItem) {
            return oldItem.getId() == newItem.getId();
        }

        @Override
        public boolean areContentsTheSame(LoanDetailOutput oldItem, LoanDetailOutput newItem) {
            return true;
        }
    };
}

主要问题是从未调用 SearchDataSource 类的方法 loadAfter

任何帮助将不胜感激。

编辑::添加了logcat的屏幕截图。可能会有帮助。

enter image description here

0 个答案:

没有答案