如何使用 Litho 框架进行分页?
How to perform pagination with the Litho framework?
我正在实施一个 Retrofit APi 以使用 Litho 框架从服务器获取数据并在 RecyclerView
中显示它,它运行良好。众所周知,当我们有无限数据要在 recyclerview 中显示时,我们必须实现分页模式。我知道这一点,但我对如何在 Litho 框架中实现这一点感到困惑。 Litho 提供 onScrollListener()
方法:
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();
我不知道:如何在 Litho 中使用自定义 EndlessRecyclerViewScrollListener
进行无限滚动?
我在 OnScrollListener
的 onScrolled
方法中利用 recyclerBinder
成功实现了 "infinite" 滚动模式。一个简单的例子如下:
//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++;
}
}
我正在实施一个 Retrofit APi 以使用 Litho 框架从服务器获取数据并在 RecyclerView
中显示它,它运行良好。众所周知,当我们有无限数据要在 recyclerview 中显示时,我们必须实现分页模式。我知道这一点,但我对如何在 Litho 框架中实现这一点感到困惑。 Litho 提供 onScrollListener()
方法:
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();
我不知道:如何在 Litho 中使用自定义 EndlessRecyclerViewScrollListener
进行无限滚动?
我在 OnScrollListener
的 onScrolled
方法中利用 recyclerBinder
成功实现了 "infinite" 滚动模式。一个简单的例子如下:
//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++;
}
}