public void refreshPinnedView() { if (null == pinnedView || null == layoutManager) { Logger.e( TAG, "Please init pinnedView and layoutManager with initRecyclerPinned method first!"); return; } if (VISIBLE != pinnedView.getVisibility()) { pinnedView.setVisibility(VISIBLE); } int curPosition = layoutManager.findFirstVisibleItemPosition(); if (RecyclerView.NO_POSITION == curPosition) { return; } View curItemView = layoutManager.findViewByPosition(curPosition); if (null == curItemView) { return; } // 如果当前的curPosition和上次的lastPosition不一样,则说明需要重新刷新数据,避免curPosition一样的情况下重复刷新相同数据 if (curPosition != lastPosition) { if (null != onRecyclerViewPinnedViewListener) { onRecyclerViewPinnedViewListener.onPinnedViewRender(this, pinnedView, curPosition); } lastPosition = curPosition; } int displayTop; int itemHeight = curItemView.getHeight(); int curTop = curItemView.getTop(); int floatHeight = pinnedView.getHeight(); if (curTop < floatHeight - itemHeight) { displayTop = itemHeight + curTop - floatHeight; } else { displayTop = 0; } RelativeLayout.LayoutParams lp = (LayoutParams) pinnedView.getLayoutParams(); lp.topMargin = displayTop; pinnedView.setLayoutParams(lp); pinnedView.invalidate(); }
public void initRecyclerPinned( RecyclerView recyclerView, ABaseLinearLayoutManager layoutManager, View pinnedView) { this.pinnedView = pinnedView; this.layoutManager = layoutManager; this.addView(this.pinnedView); RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); this.pinnedView.setLayoutParams(lp); layoutManager .getRecyclerViewScrollManager() .addScrollListener( recyclerView, new OnRecyclerViewScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) {} @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { refreshPinnedView(); } }); pinnedView.setVisibility(GONE); }