Android RecyclerView查找ItemDecoration的第一个和最后一个视图 [英] Android RecyclerView finding out first and last view on ItemDecoration
问题描述
这是我的情况
- 我必须在Recycler View的第一项和最后一项上添加空间
-
我正在使用
ItemDecoration
recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int totalHeight = parent.getHeight();
int viewHeight = getResources().getDimensionPixelOffset(R.dimen.breadcrumb_btn_height);
int padding = (totalHeight - viewHeight) / 2;
//to position view on middle
if (view.getTag() instanceof Integer) {
int position = (int) view.getTag();
if (position == 0) {
//first position
outRect.set(padding, padding, 0, padding);
} else if (position == linearLayoutManager.getItemCount() - 1) {
//last position
outRect.set(0, padding, padding, padding);
} else {
outRect.set(0, padding, 0, padding);
}
}
}
});
我在这里
LinearLayoutManager
和Horizontal Orientation
我正在按以下方式在RecyclerView Adapter
上添加position
标签
I am adding position
tag on RecyclerView Adapter
in the following manner
@Override
public void onBindViewHolder(BreadCrumbViewHolder holder, int position) {
holder.setPosition(position);
}
还有其他方法可以访问RecyclerView
上的第一个孩子和最后一个孩子吗,以便以后删除和添加项目时不会有任何问题.
Is there any other way to access RecyclerView
first and last child on ItemDecoration
so that later on removing and adding items won't have any problem.
推荐答案
是否还有其他方法可以访问RecyclerView的第一个和最后一个孩子 ItemDecoration,以便以后删除和添加项目时不再需要 没问题.
Is there any other way to access RecyclerView first and last child on ItemDecoration so that later on removing and adding items won't have any problem.
是的.您只需实现自己的ItemDecoration
即可实现所需的目标.
Yes. You can achieve what you want only by implementing your own ItemDecoration
.
要获取当前的商品/子位置:
To get the current item/child position:
int position = parent.getChildAdapterPosition(view);
要获取项目/子项的数量:
To get the number of items/childs:
int itemCount = state.getItemCount();
这样,您可以在RecyclerView
的第一个和最后一个孩子上添加一个空格.
With this you can add a space on the first and last child of your RecyclerView
.
现在通过这种方法更改代码,您将得到:
Now changing your code for this approach you get:
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
//your padding...
final int itemPosition = parent.getChildAdapterPosition(view);
if (itemPosition == RecyclerView.NO_POSITION) {
return;
}
final int itemCount = state.getItemCount();
/** first position */
if (itemPosition == 0) {
outRect.set(padding, padding, 0, padding);
}
/** last position */
else if (itemCount > 0 && itemPosition == itemCount - 1) {
outRect.set(0, padding, padding, padding);
}
/** positions between first and last */
else {
outRect.set(0, padding, 0, padding);
}
}
奖金:如果您希望对RecyclerView的所有项目进行填充,请查看我创建的要点:https://gist.github.com/ryanamaral/93b5bd95412baf85a81a
Bonus: If you are looking to give padding to all items of the RecyclerView take a look at the Gist I've created: https://gist.github.com/ryanamaral/93b5bd95412baf85a81a
这篇关于Android RecyclerView查找ItemDecoration的第一个和最后一个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!