- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我通过覆盖 getItemOffsets()
方法(请参阅下面的代码)使用 GridLayoutManager
为我的 RecyclerView
应用相等的边距。
但是,当一个对象从适配器中移除时,移除动画会在没有偏移的情况下被调用。因此,动画开始于与要移除的对象不同的位置。
我试图通过 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);
}
最佳答案
尝试使用 View LayoutParams
。如果您不使用某些自定义 LayoutManager
,它应该包含您需要的信息。
int position = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewAdapterPosition();
关于android - ItemDecoration 覆盖 getItemOffsets() 和动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34575853/
我的回收站 View 附有元素装饰。它应该只在最后一项上添加大量填充。这很好用。但是,当我添加新项目时,getItemOffsets 仅针对最后一项(而不是 recycler view 中的每一项)调
我通过覆盖 getItemOffsets() 方法(请参阅下面的代码)使用 GridLayoutManager 为我的 RecyclerView 应用相等的边距。 但是,当一个对象从适配器中移除时,移
我正在尝试创建一个 ItemDecoration,它将通过向最后一个项目动态添加填充来保持 RecyclerView 的最小高度。 要计算填充量,我需要知道所有 child 的总高度。我通过获取单个
我是一名优秀的程序员,十分优秀!