StaggeredGridLayoutManager gridLayoutManager = new StaggeredGridLayoutManager(spanCount, StaggeredGridLayoutManager.VERTICAL); gridLayoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);//防止item交换位置 //以下三行去掉 RecyclerView 动画代码,防止闪烁 ((DefaultItemAnimator) mRecyclerView.getItemAnimator()).setSupportsChangeAnimations(false); ((SimpleItemAnimator) mRecyclerView.getItemAnimator()).setSupportsChangeAnimations(false); mRecyclerView.getItemAnimator().setChangeDuration(0); 方法一: mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); gridLayoutManager.invalidateSpanAssignments();//重新布局 } @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); } });
方法二:
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
// 滑动停止,刷新布局与分割线
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
if (recyclerView.getLayoutManager() instanceof StaggeredGridLayoutManager) {
((StaggeredGridLayoutManager) recyclerView.getLayoutManager()).invalidateSpanAssignments();
recyclerView.invalidateItemDecorations();
}
}
}
});