简体   繁体   English

显示带有android分页库的进度条

[英]showing a progress bar with android paging library

I'm using paging library and room, making my room db as the single layer of truth of my app.我正在使用分页库和房间,使我的房间数据库成为我的应用程序的单层真相。 when fetching is done from the server using retrofit, i save the result locally in the db then i get the data from the db using paging library.当使用改造从服务器完成获取时,我将结果本地保存在数据库中,然后我使用分页库从数据库中获取数据。 I'm using BoundaryCallback .我正在使用BoundaryCallback

@Override
    public void onZeroItemsLoaded() {
        requestAndSaveData();
    }
    @Override
    public void onItemAtEndLoaded(@NonNull Photo itemAtEnd) {
        requestAndSaveData();
}

 private void requestAndSaveData() {
        if (isRequestInProgress) return;
        isRequestInProgress = true;
        apiInterface.getPhotos(lastRequestPage, NETWORK_PAGE_SIZE).enqueue(new Callback<PhotoList>() {
            @Override
            public void onResponse(Call<PhotoList> call, Response<PhotoList> response) {
                if (response.isSuccessful()) {
                    cache.insertPhotos(response.body().getHits()); //todo only save 20 - 40 items
                    lastRequestPage++;
                    isRequestInProgress = false;
                    Log.i("deb", "number from boundary: " + response.body().getHits().size());
                }
}

When there is no item in the db or the user scroll to the last item i call requestAndSaveData() method that fetch the data from the server and save it into the db.当数据库中没有项目或用户滚动到最后一个项目时,我调用requestAndSaveData()方法从服务器获取数据并将其保存到数据库中。

My question is how to show a progress bar at the bottom of the list while loading the next page from the server and saving it to the db as it may take a long time?我的问题是如何在从服务器加载下一页并将其保存到数据库时在列表底部显示进度条,因为这可能需要很长时间?

First of all you could broaden your question scope by asking about how to show a loading indicator in the bottom of RecyclerView instead of narrowing it to the Paging Library..首先,您可以通过询问如何在RecyclerView底部显示加载指示器而不是将其缩小到分页库来扩大您的问题范围。

Secondly, you can easily show a loading indicator in the bottom of your list by adding an extra cell to the list that is just only for showing load indicator, the similar idea is used here to show the network status:其次,您可以通过在列表中添加一个仅用于显示负载指示器的额外单元格,轻松地在列表底部显示加载指示器,这里使用类似的想法来显示网络状态:

https://github.com/googlesamples/android-architecture-components/blob/master/PagingWithNetworkSample/lib/src/main/java/com/android/example/paging/pagingwithnetwork/reddit/ui/PostsAdapter.kt https://github.com/googlesamples/android-architecture-components/blob/master/PagingWithNetworkSample/lib/src/main/java/com/android/example/paging/pagingwithnetwork/reddit/ui/PostsAdapter.kt

Put a ProgressBar component inside your layout xml.ProgressBar组件放在布局 xml 中。 Then change its visibility according to api call.然后根据 api 调用更改其可见性。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv"
        style="@style/recyclerViewDefaultStyle" />

    <ProgressBar
        android:id="@+id/pb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:visibility="gone" />

</LinearLayout>

In your activity在您的活动中

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ProgressBar;

import in.ks.widgetClock.R;

/**
 * Created by KHEMRAJ on 6/23/2018.
 */
public class Sample extends AppCompatActivity {
    ProgressBar progressBar;

    private void showProgressBar() {
        progressBar.setVisibility(View.VISIBLE);
    }

    private void hideProgressBar() {
        progressBar.setVisibility(View.GONE);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        progressBar = findViewById(R.id.pb);
        ...
    }

    private void requestAndSaveData() {
        if (isRequestInProgress) return;
        isRequestInProgress = true;
        showProgressBar();
        apiInterface.getPhotos(lastRequestPage, NETWORK_PAGE_SIZE).
                enqueue(new Callback<PhotoList>() {
                    @Override
                    public void onResponse(Call<PhotoList> call, Response<PhotoList> response) {
                        hideProgressBar();
                        if (response.isSuccessful()) {
                            cache.insertPhotos(response.body().getHits()); //todo only save 20 - 40 items
                            lastRequestPage++;
                            isRequestInProgress = false;
                            Log.i("deb", "number from boundary: " + response.body().getHits().size());
                        }
                    }
                }
    }

}

Provide placeholders in your UI:在您的 UI 中提供占位符:

I don't this is there any need of this if you are using placeholder and it has advantage as well see below:如果您使用占位符,我不知道这有什么需要,它也有优势,见下文:

No loading spinner necessary: Because the list size is already known, there's no need to alert users that more items are loading.无需加载微调器:由于列表大小已知,因此无需提醒用户正在加载更多项目。 The placeholders themselves convey that information.占位符本身传达了该信息。

Source : https://developer.android.com/topic/libraries/architecture/paging/ui来源: https : //developer.android.com/topic/libraries/architecture/paging/ui

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM