我創建了recyclerview和頁眉佈局,當我向上滾動recyclerview時,頁眉佈局應根據recyclerview的滾動偏移量消失。如何在設置翻譯後刷新佈局Y
我的佈局xml文件是:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:showIn="@layout/activity_main"
tools:context=".MainActivity"
android:id="@+id/main_container">
<TextView android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/header"
android:text="Mao Minh Tri"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/header"/>
</RelativeLayout>
和滾動recyclerview當執行代碼
RecyclerView.OnScrollListener scrollListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if (originalHeight == 0) {
originalHeight = myrecyclerView.getHeight();
}
ViewGroup.LayoutParams lp = myrecyclerView.getLayoutParams();
calculateLocationAndProductStickerOffset();
textview.setTranslationY(-offset);
mainView.setTranslationY(-offset);
if ((offset < textview.getHeight() && dy > 0) || (offset > 0 && dy < 0)) {
offset += dy;
}
System.out.println("original height --> " + originalHeight);
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
}
};
myrecyclerView.addOnScrollListener(scrollListener);
}
private void calculateLocationAndProductStickerOffset() {
if (offset > textview.getHeight()) {
offset = textview.getHeight();
} else if (offset < 0) {
offset = 0;
}
}