在带有分页库的 RecyclerVew 中实现进度条(向下滚动以加载更多内容时)?

问题描述 投票:0回答:1

以下是我当前代码中的一些片段,但是当用力滚动时,动画就像已经到达列表底部一样,我想在其中添加进度条/旋转器,就像在 fb/insta feed 中一样。

虽然再次向下滚动时列表中会更新新项目,但我希望进度条能够平滑过渡,而不是每次看起来都像是提要已结束。

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RecyclerView recyclerView = findViewById(R.id.userList);
        LinearLayoutManager llm = new LinearLayoutManager(this);
        llm.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(llm);

        UserViewModel viewModel = ViewModelProviders.of(this).get(UserViewModel.class);
        viewModel.init();
        final UserAdapter userUserAdapter = new UserAdapter();

        viewModel.userList.observe(this, pagedList -> {
            userUserAdapter.setList(pagedList);
        });

        recyclerView.setAdapter(userUserAdapter);
    }
}

数据来源:

public class MyDataSource extends TiledDataSource<User> {

    Webservice webservice;

    MyDataSource(){
    webservice = WebServiceImpl.createGitHubService();
    }

    @Override
    public int countItems() {
        return DataSource.COUNT_UNDEFINED;
    }

    @Override
    public List<User> loadRange(int startPosition, int count) {
        List<User> gitHubUser = new ArrayList();
        try {
            Response<List<User>> response = webservice.getUsers(startPosition,count).execute();
            if (response.isSuccessful() && response.code() == 200) {
                gitHubUser.addAll(response.body());
            } else {
                Log.e("API CALL", response.message());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return gitHubUser;
    }    }

视图模型:

public class UserViewModel extends ViewModel {

    public LiveData<PagedList<User>> userList;
    MyDataSource tDataSource;

    public UserViewModel() {

    }

    public void init() {

        userList = new LivePagedListProvider<Integer, User>() {
            @Override
            protected DataSource<Integer, User> createDataSource() {
                tDataSource = new MyDataSource();
                return tDataSource;
            }

        }.create(0, new PagedList.Config.Builder()
                .setEnablePlaceholders(false)
                .setPageSize(20)
                .setInitialLoadSizeHint(20)
                .build());
    }
}
android android-architecture-components android-progressbar android-paging-library
1个回答
-1
投票

创建一个辅助类

EndlessRecyclerOnScrollListener
。 将以下代码粘贴到其中。

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener {
    public static String TAG = EndlessRecyclerOnScrollListener.class.getSimpleName();

    private int previousTotal = 0; // The total number of items in the dataset after the last load
    private boolean loading = true; // True if we are still waiting for the last set of data to load.
    private int visibleThreshold = 5; // The minimum amount of items to have below your current scroll position before loading more.
    int firstVisibleItem, visibleItemCount, totalItemCount;

    private int current_page = 1;

    private LinearLayoutManager mLinearLayoutManager;

    public EndlessRecyclerOnScrollListener(LinearLayoutManager linearLayoutManager) {
        this.mLinearLayoutManager = linearLayoutManager;
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);

        visibleItemCount = recyclerView.getChildCount();
        totalItemCount = mLinearLayoutManager.getItemCount();
        firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();

        if (loading) {
            if (totalItemCount > previousTotal) {
                loading = false;
                previousTotal = totalItemCount;
            }
        }
        if (!loading && (totalItemCount - visibleItemCount)
                <= (firstVisibleItem + visibleThreshold)) {
            // End has been reached

            // Do something
            current_page++;

            onLoadMore(current_page);

            loading = true;
        }
    }

    public abstract void onLoadMore(int current_page);
}

在您的活动中,添加以下代码。

public class SampleActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sample);

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setOnScrollListener(new EndlessRecyclerOnScrollListener(linearLayoutManager) {
            @Override
            public void onLoadMore(int current_page) {
                // make progress bar visible.
                //call API
            }
        });
    }
}

在 activiy 的 xml 中的

recyclerview
下面放置一个进度条。 这里,当
recyclerview
滚动到底部时,将调用 onLoadMore() 。

欲了解更多信息,请参阅 https://gist.github.com/ssinss/e06f12ef66c51252563e

© www.soinside.com 2019 - 2024. All rights reserved.