android getitem,android – ItemDecoration重写getItemOffsets()和动画
我通过覆盖getItemOffsets()方法使用GridLayoutManager将相等的边距应用于我的RecyclerView(请参阅下面的代码).但是,从适配器中删除对象时,将调用删除动画而不使用偏移.因此,anmiation开始于与要移除的对象不同的位置.我尝试通过getSpanIndex(position)获取位置,但位置(parent.getChildAdapterPosition(v
我通过覆盖getItemOffsets()方法使用GridLayoutManager将相等的边距应用于我的RecyclerView(请参阅下面的代码).
但是,从适配器中删除对象时,将调用删除动画而不使用偏移.因此,anmiation开始于与要移除的对象不同的位置.
我尝试通过getSpanIndex(position)获取位置,但位置(parent.getChildAdapterPosition(view))返回NO_POSITION,因为当调用getItemOffsets()时,对象已从适配器中删除.
有没有办法在我的情况下得到抵消?
@Override
public void getItemOffsets(Rect outRect, View view,
RecyclerView parent, RecyclerView.State state) {
GridLayoutManager mgr = parent.getLayoutManager();
int position = parent.getChildAdapterPosition(view);
if (position == RecyclerView.NO_POSITION) {
// here I need to access the position of the current element
// and call outRect.set(left, top , right, bottom);
// which is not possible because it is no longer in the adapter
return;
}
int spanCount = mgr.getSpanCount();
int spanSize = mgr.getSpanSizeLookup().getSpanSize(position);
int spanIndex = mgr.getSpanSizeLookup().getSpanIndex(position, spanCount);
if (spanIndex == spanCount-1) {
// last element
left = space / 2;
right = space;
} else if (spanIndex == 0) {
// first element
left = space;
right = space / 2;
} else {
// middle element
left = space / 2;
right = space / 2;
}
outRect.set(left, top, right, bottom);
}
更多推荐
所有评论(0)