是否有可能添加项目装饰页脚,而不使用适配器?由于我正在处理一个非常复杂的适配器,并且有很多不同的视图持有者类型,所以我希望为我的应用中的每个列表无缝添加一个相同的页脚。RecyclerView:添加项目装饰页脚?
2
A
回答
0
据我所知,这是最好的做法。
下面是RecyclerView.ItemDecoration类描述:
/**
* An ItemDecoration allows the application to add a special drawing and layout offset
* to specific item views from the adapter's data set. This can be useful for drawing dividers
* between items, highlights, visual grouping boundaries and more.
然而,根据适配器viewtype分频器实现自己的除法时,您可以设置特定的行为必须处理。以下是我在一个在线课程使用的示例代码:
public class Divider extends RecyclerView.ItemDecoration {
private Drawable mDivider;
private int mOrientation;
public Divider(Context context, int orientation) {
mDivider = ContextCompat.getDrawable(context, R.drawable.divider);
if (orientation != LinearLayoutManager.VERTICAL) {
throw new IllegalArgumentException("This Item Decoration can be used only with a RecyclerView that uses a LinearLayoutManager with vertical orientation");
}
mOrientation = orientation;
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
if (mOrientation == LinearLayoutManager.VERTICAL) {
drawHorizontalDivider(c, parent, state);
}
}
private void drawHorizontalDivider(Canvas c, RecyclerView parent, RecyclerView.State state) {
int left, top, right, bottom;
left = parent.getPaddingLeft();
right = parent.getWidth() - parent.getPaddingRight();
int count = parent.getChildCount();
for (int i = 0; i < count; i++) {
//here we check the itemViewType we deal with, you can implement your own behaviour for Footer type.
// In this example i draw a drawable below every item that IS NOT Footer, as i defined Footer as a button in view
if (Adapter.FOOTER != parent.getAdapter().getItemViewType(i)) {
View current = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) current.getLayoutParams();
top = current.getTop() - params.topMargin;
bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
if (mOrientation == LinearLayoutManager.VERTICAL) {
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
}
}
或者你可以使用一个名为Flexible Divider库,允许使用自定义绘制或资源进行设置。
+0
感谢您的回答安东。你是不是最好的做法,因为项目装饰器可能有不同的用途,所以我可能会回退到旧的和干净的适配器,使用不同的视图类型。 – rqiu
相关问题
- 1. RecyclerView添加页脚
- 2. Recyclerview与中心项目装饰
- 3. RecyclerView不同的项目装饰不同的项目
- 4. 如何重新装饰recyclerView项目被删除的项目下
- 5. RecyclerView添加项目动画
- 6. 添加项目到RecyclerView从其他删除RecyclerView项目
- 7. 仅装饰RecyclerView的绝对第一项
- 8. 在RecyclerView片段中添加项目
- 9. TreeViewer中的项目装饰
- 10. 的Android RecyclerView addItemDecoration不会增加装饰RecyclerView
- 11. RecyclerView添加项目,但替换最初的项目
- 12. 页脚文字装饰不起作用
- 13. 在RecyclerView的最后一个视图上绘制的自定义项目装饰
- 14. RecyclerView删除最后一个项目后的分隔器/装饰器
- 15. RecyclerView:新加载的项目不会添加到顶部
- 16. 增加recyclerview的高度动态的项目添加
- 17. 包装追加方法添加三个项目。如何添加一个项目?
- 18. Django装饰器,添加方法到WSGIRequest
- 19. 装饰Java的调用添加行为
- 20. 添加类装饰器的类属性
- 21. 添加装饰功能的一类
- 22. Swift collectionView添加货架装饰器
- 23. 通过装饰添加功能
- 24. 添加脚本角CLI项目
- 25. 将python脚本添加到C++项目
- 26. 添加框架4.0到安装项目
- 27. SSRS - 页脚页脚移动的项目
- 28. Recyclerview - smoothScrollToPosition到列表的顶部,然后动画添加项目
- 29. RecyclerView仅在一些项目之间添加分隔线
- 30. 安卓:添加边框和舍入到recyclerView项目
你使用的适配器与listView或gridview? –
抱歉不清楚,但正如标题所述,我正在使用recyclerview – rqiu