页面滑动, top_bg随着页面滑动上线滑动,滑动最上面到title高度不在有滑动变化效果
<FrameLayout
android:id="@+id/layout_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F5F5F5">
<!--250 高度变化需要在java 中做相应改变,否则影响滑动效果-->
<View
android:id="@+id/top_bg"
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="@drawable/title_bg" />
</FrameLayout>
int barStatusBarHeight = 45;//假设状态栏高度
int titleHeight = 45;//假设title高度
int hgt = 250 - barStatusBarHeight - titleHeight;//假设特别背景高度250
int distance = 0;
irc.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
distance += dy;
//上下滑动的时候背景变
if (distance <= hgt) {
layout_bg.setScrollY(distance);
} else if (distance > hgt) {
layout_bg.setScrollY(hgt);
}
LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
int firstCompletelyVisibleItemPosition = layoutManager.findFirstCompletelyVisibleItemPosition();
if(firstCompletelyVisibleItemPosition == 0) {
distance = 0;
layout_bg.setScrollY(distance);
}
}
});