java代码:
private class SuspensionOnScrollListener extends RecyclerView.OnScrollListener {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
if (SuspensionBarAdapter.TYPETITLE == suspensionBarAdapter.getItemViewType(firstVisiblePoision + 1)) {
LinearLayout nextView = (LinearLayout) linearLayoutManager.findViewByPosition(firstVisiblePoision + 1);
if (nextView != null) {
if (nextView.getTop() <= suspensionBarHeight) {
headContainer.setY(-(suspensionBarHeight - nextView.getTop()));
}
}
}
nextHeadPosition = getNextHeadPosition(dy > 0);
if (firstVisiblePoision != linearLayoutManager.findFirstVisibleItemPosition()) {
firstVisiblePoision = linearLayoutManager.findFirstVisibleItemPosition();
headContainer.setY(0);
updateHeadView(nextHeadPosition);
}
}
}
private int getNextHeadPosition(boolean isUp) {
if (isUp) {
if(SuspensionBarAdapter.TYPETITLE == suspensionBarAdapter.getItemViewType(firstVisiblePoision+1)){
nextVisiblePosition = firstVisiblePoision+1;
}
} else {
if (firstVisiblePoision > 0) {
nextVisiblePosition = firstVisiblePoision - 1;
while (SuspensionBarAdapter.TYPETITLE != suspensionBarAdapter.getItemViewType(nextVisiblePosition) && nextVisiblePosition > 0) {
--nextVisiblePosition;
}
}
}
return nextVisiblePosition;
}
布局
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<include
android:id="@+id/head_container"
layout="@layout/title_item"/>
</FrameLayout>
使用:
recyclerView.addOnScrollListener(new SuspensionOnScrollListener());
recyclerview悬浮条效果
最新推荐文章于 2021-07-15 11:25:35 发布