我有RecyclerView
GridLayoutManager
,如果用户向上或向下滚动,我想在类getItemOffsets
的方法RecyclerView.ItemDecoration
内找到。下面是我在GridDividerDecoration
类中的代码,其中我在项目之间绘制边框。
public class GridDividerDecorations extends RecyclerView.ItemDecoration {
private int mInsets;
List<Integer> itemsHeaderPos= new ArrayList<>();
List<Integer> itemsPos= new ArrayList<>();
List<GadgetItem> mList = new ArrayList<>();
int lastItem;
int itemPosition, itemposition2 = 0;
public GridDividerDecorations(Context context,List<GadgetItem> mList ) {
this.mList = mList;
mInsets = 6;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
itemPosition = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewAdapterPosition();
if ((parent.getAdapter().getItemViewType(itemPosition) == 0))
itemsHeaderPos.add(itemPosition);
if((parent.getAdapter().getItemViewType(itemPosition) != 0))
itemsPos.add(itemPosition);
outRect.top = mInsets;
GridLayoutManager layoutManager = (GridLayoutManager)parent.getLayoutManager();
if(itemPosition == layoutManager.findLastVisibleItemPosition())
outRect.right = mInsets;
if (itemPosition % 2 == 0 && itemsHeaderPos.get(itemsHeaderPos.size() - 1) % 2 == 0) {
if (parent.getAdapter().getItemViewType(itemPosition) != 0)
outRect.left = mInsets;
} else if (itemPosition % 2 == 0 && itemsHeaderPos.get(itemsHeaderPos.size() - 1) % 2 != 0) {
if (parent.getAdapter().getItemViewType(itemPosition) != 0) {
outRect.right = mInsets;
}
}
}
}
问题是,当我向上滚动时,分隔线被颠倒了,所以如果用户向上滚动我可以恢复我的实现。谢谢!
答案 0 :(得分:1)
您可以使用OnScrollChangeListener确定是向上还是向下滚动:
recycler.setOnScrollChangeListener(new View.OnScrollChangeListener() {
@Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if(oldScrollY - scrollY > 0) {
// do stuff
}
else {
// do stuff
}
}
});