我正在实现一个改进的api,用于从服务器获取数据,并使用litho框架在RecyclerView中显示,它做得很好。我们都知道,当我们有无限的数据要在recyclerview中显示时,我们必须实现分页模式。我知道这一点,但我很困惑如何在litho框架中实现这一点。litho the providesonScrollListener()方法:

final Component component = Recycler.create(context)
    .binder(recyclerBinder)
    .onScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            //
        }

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

我不知道:如何使用自定义的EndlessRecyclerViewScrollListener在litho中进行无休止的滚动?

最佳答案

我已经成功地实现了利用recyclerBinder方法中的onScrolled来实现“无限”滚动模式。一个简单的例子如下:

//Initialize the RecyclerBinder
recyclerBinder = new RecyclerBinder(c, new LinearLayoutInfo(getContext(), OrientationHelper.VERTICAL, false));

//Initialize a recycler component
Component component = Recycler.create(c)
                .onScrollListener(new RecyclerView.OnScrollListener() {
                    @Override
                    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                        super.onScrolled(recyclerView, dx, dy);

                        // use the recyclerBinder to determine what items at what position are visible -
                        // (you could also use the findLastVisibleItemPosition() method depending on your implementation)
                        int firstVisibleItemPosition = recyclerBinder.findFirstVisibleItemPosition();

                        //check if it is within range relative to the current available items and is not loading (room to improve/modify logic pending use case)
                        if((recyclerBinder.getItemCount() - 5) <= firstVisibleItemPosition && !isLoading) {
                            //if so - use your service to get the next page
                            service.getNextPage();
                        }
                    }
                })
                .build();

然后在回调方法中-可以从项目计数开始插入项目
public void callback(List<T> results) {
    int position = recyclerBinder.getItemCount();
    for(T result: results) {
        Component component = //assemble your component(s) ...
        ComponentInfo.Builder info = ComponentInfo.create().component(component);
        recyclerBinder.insertItemAt(position, info.build());
        position++;
    }
}

10-06 03:22