Android 修复RecycleView多次调用addItemDecoration方法导致间隔增大

需要用到RecycleView的getItemDecorationCount方法(如果这个方法没有那就是RecycleView的版本不高导致导致的27的版本是有的)
然后通过这一段代码判断就行了:

if(mRecyclerView.getItemDecorationCount() > 0){

            RecyclerView.ItemDecoration itemDecorationAt = mRecyclerView.getItemDecorationAt(0);

            if(itemDecorationAt == null){

                mRecyclerView.addItemDecoration(decoration);
            }
        }else{
            //需要在setLayoutManager()之后调用addItemDecoration()
            mRecyclerView.addItemDecoration(decoration);
        }

mRecyclerView为RecycleView,decoration为RecyclerView.ItemDecoration

你可能感兴趣的:(Andriod,Studio)