From b140501f31df36360cf8a6d336e99a0b1a51255d Mon Sep 17 00:00:00 2001 From: jingbin <770413277@qq.com> Date: Fri, 3 Jan 2020 17:28:01 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E9=BB=98=E8=AE=A4=E4=BD=BF=E6=BB=91?= =?UTF-8?q?=E5=8A=A8=E6=9D=A1=E7=BD=AE=E9=A1=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../me/jingbin/library/ByRecyclerView.java | 4 ++ .../decoration/SpacesItemDecoration.java | 42 +++++++++++++------ 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/ByRecyclerview/src/main/java/me/jingbin/library/ByRecyclerView.java b/ByRecyclerview/src/main/java/me/jingbin/library/ByRecyclerView.java index 4393e12..88a91aa 100644 --- a/ByRecyclerview/src/main/java/me/jingbin/library/ByRecyclerView.java +++ b/ByRecyclerview/src/main/java/me/jingbin/library/ByRecyclerView.java @@ -1202,6 +1202,10 @@ public void setOnItemChildLongClickListener(OnItemChildLongClickListener listene mOnItemChildLongClickListener = listener; } + public boolean isRefreshEnabled() { + return mRefreshEnabled; + } + /** * call it when you finish the activity, */ diff --git a/ByRecyclerview/src/main/java/me/jingbin/library/decoration/SpacesItemDecoration.java b/ByRecyclerview/src/main/java/me/jingbin/library/decoration/SpacesItemDecoration.java index 639a2fc..3206297 100644 --- a/ByRecyclerview/src/main/java/me/jingbin/library/decoration/SpacesItemDecoration.java +++ b/ByRecyclerview/src/main/java/me/jingbin/library/decoration/SpacesItemDecoration.java @@ -29,6 +29,8 @@ import androidx.core.content.ContextCompat; import androidx.recyclerview.widget.RecyclerView; +import me.jingbin.library.ByRecyclerView; + /** * 给 LinearLayoutManager 增加分割线,可设置去除首尾分割线个数 * @@ -48,7 +50,8 @@ public class SpacesItemDecoration extends RecyclerView.ItemDecoration { */ private static final int[] ATTRS = new int[]{android.R.attr.listDivider}; /** - * 头部 不显示分割线的item个数 + * 头部 不显示分割线的item个数 这里应该包含刷新头, + * 比如有一个headerView和有下拉刷新,则这里传 2 */ private int mHeaderNoShowSize = 0; /** @@ -75,6 +78,7 @@ public class SpacesItemDecoration extends RecyclerView.ItemDecoration { * 如果是纵向 - 下边距 */ private int mRightBottomPadding; + private ByRecyclerView byRecyclerView; public SpacesItemDecoration(Context context) { this(context, VERTICAL, 0, 1); @@ -92,10 +96,9 @@ public SpacesItemDecoration(Context context, int orientation, int headerNoShowSi * Creates a divider {@link RecyclerView.ItemDecoration} * * @param context Current context, it will be used to access resources. - * @param orientation Divider orientation. Should be {@link #HORIZONTAL} or - * {@link #VERTICAL}. - * @param headerNoShowSize not show header divider size - * @param footerNoShowSize not show footer divider size + * @param orientation Divider orientation. Should be {@link #HORIZONTAL} or {@link #VERTICAL}. + * @param headerNoShowSize headerViewSize + RefreshViewSize + * @param footerNoShowSize footerViewSize */ public SpacesItemDecoration(Context context, int orientation, int headerNoShowSize, int footerNoShowSize) { mContext = context; @@ -252,19 +255,34 @@ public void getItemOffsets(Rect outRect, View view, RecyclerView parent, Recycle outRect.set(0, 0, 0, 0); return; } + //parent.getChildCount() 不能拿到item的总数 + int lastPosition = state.getItemCount() - 1; + int position = parent.getChildAdapterPosition(view); + + boolean mScrollTopFix = false; + if (byRecyclerView == null && parent instanceof ByRecyclerView) { + byRecyclerView = (ByRecyclerView) parent; + } + if (byRecyclerView != null && byRecyclerView.isRefreshEnabled()) { + mScrollTopFix = true; + } + + // 滚动条置顶 + boolean isFixScrollTop = mScrollTopFix && position == 0; + boolean isShowDivider = mHeaderNoShowSize <= position && position <= lastPosition - mFooterNoShowSize; + if (mOrientation == VERTICAL) { - //parent.getChildCount() 不能拿到item的总数 - int lastPosition = state.getItemCount() - 1; - int position = parent.getChildAdapterPosition(view); - if (position <= lastPosition - mFooterNoShowSize) { + if (isFixScrollTop) { + outRect.set(0, 0, 0, 1); + } else if (isShowDivider) { outRect.set(0, 0, 0, mDivider != null ? mDivider.getIntrinsicHeight() : mDividerSpacing); } else { outRect.set(0, 0, 0, 0); } } else { - int lastPosition = state.getItemCount() - 1; - int position = parent.getChildAdapterPosition(view); - if (position <= lastPosition - mFooterNoShowSize) { + if (isFixScrollTop) { + outRect.set(0, 0, 1, 0); + } else if (isShowDivider) { outRect.set(0, 0, mDivider != null ? mDivider.getIntrinsicWidth() : mDividerSpacing, 0); } else { outRect.set(0, 0, 0, 0);