我想创建一个动态加载视图,其中包含水平recyclerview在垂直recyclerview中,垂直recyclerview应该在scrollview中,因为主视图包含更多的视图组件。
所以我的动态层次结构如下:
Scrollview [
| Viewpager --
| AdView --
| Vertical Items - Recyclerview {
1) [Horizontal Recyclerview {1.1, 1.2, 1.3} [Lazy Loading of Items]]
2) [Horizontal Recyclerview {2.1, 2.2, 2.3} [Lazy Loading of Items]]
3) [Horizontal Recyclerview {3.1, 3.2, 3.3} [Lazy Loading of Items]]
4) [Horizontal Recyclerview {4.1, 4.2, 4.3} [Lazy Loading of Items]]
.
.
.
[Lazy Loading of Items in Vertical Direction also]
}
| Some Another View --
]
在android中有没有实现这种视图的最佳方法。所以在水平和垂直方向上都可以实现动态加载?
Android中有可用的库吗?
请给我举几个很好的例子。
最佳答案
最好的方法是使用RendererRecyclerViewAdapter
步骤1:将ViewModel
接口添加到简单项
public class SimpleItem implements ViewModel {
/* your getters */
}
步骤2:为您的简单项目创建
ViewBinder
。private ViewRenderer getSimpleViewBinder() {
return new ViewBinder<>(
R.layout.simple_item, /* your item layout */
SimpleItem.class, /* your model class */
(model, finder, payloads) -> {
/* your binding */
finder.setText(R.id.textView, model.getText());
});
}
步骤3:扩展
DefaultCompositeViewModel
并添加水平项的uniq idpublic class HorizontalViewModel extends DefaultCompositeViewModel {
private int mID;
public HorizontalViewModel(int ID, List<? extends ViewModel> items) {
super(items);
mID = ID;
}
public int getID() {
return mID;
}
}
它是你的水平项,里面包含所有的简单项。
步骤4:创建
HorizontalViewBinder
public class HorizontalViewBinder extends CompositeViewBinder<HorizontalViewModel> {
public HorizontalViewBinder(int layoutID,
int recyclerViewID,
Class<HorizontalViewModel> type) {
super(layoutID, recyclerViewID, type);
}
@Override
public void bindView(HorizontalViewModel model, CompositeViewHolder holder) {
super.bindView(model, holder);
holder.getRecyclerView().addOnScrollListener(new EndlessScrollListener() {
@Override
public void onLoadMore(int page, int totalItemsCount) {
holder.getAdapter().showLoadMore();
/* sendLoadMoreRequest(model.getID()); */
}
});
}
@NonNull
@Override
public CompositeViewHolder createViewHolder(ViewGroup parent) {
final CompositeViewHolder viewHolder = super.createViewHolder(parent);
viewHolder.getAdapter().registerRenderer(new LoadMoreViewBinder(R.layout.load_more));
/* don't forget to create load more layout */
return viewHolder;
}
}
步骤5:为水平视图绑定器创建getter
private ViewRenderer getHorizontalViewBinder() {
return new HorizontalViewBinder<>(
R.layout.horizontal_layout, /* your horizontal layout that contains RecyclerView */
R.id.recycler_view, /* an ID of RecyclerView */
HorizontalViewModel.class /* your created class, see Step 3 */
);
}
步骤6:在片段/活动中初始化主renderrecyclerviewapter
RendererRecyclerViewAdapter mRecyclerViewAdapter = new RendererRecyclerViewAdapter();
mRecyclerViewAdapter.registerRenderer(new Horizontal().registerRenderer(getHorizontalViewBinder().registerRenderer(getSimpleViewBinder())); /* register HorizontalViewBinder and SimpleViewBinder */
mRecyclerViewAdapter.setItems(getYourList());
步骤7:将loadMoreListener添加到主RecyclerView
mRecyclerView.addOnScrollListener(new EndlessScrollListener() {
@Override
public void onLoadMore(int page, int totalItemsCount) {
mRecyclerViewAdapter.showLoadMore();
/* send request to a server */
}
});
步骤8:为其他项目创建specials viewbinder:adviewbinder,其他viewbinder并在适配器中注册它们。请参阅步骤1和步骤2。
仅供参考:endlesscrollllistener-这是loadmorelistener的实现
享受吧!:)
如果需要更多详细信息,请参见wiki页面:简单项、复合项、加载更多指示器。