動態變化RecyclerView的高度似乎反模式。
我建議你使用paddingTop和clipToPadding =「false」。
您的佈局XML可能是這樣的:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"/>
<FrameLayout
android:id="@+id/header_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
... here is same layout, samll text and "STARTEN" button.
</FrameLayout>
</FrameLayout>
Java程序可能是這樣的:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
final View headerContainer = findViewById(R.id.header_container);
final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
... initialization of recyclerView ...
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//change height of headerContainer
}
});
// set paddingTop of RecyclerView
headerContainer.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
headerContainer.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
headerContainer.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
recyclerView.setPadding(
recyclerView.getPaddingLeft(),
headerContainer.getHeight(),
recyclerView.getPaddingRight(),
recyclerView.getPaddingBottom()
);
}
});
}
請添加您的佈局XML文件或截屏。 – nshmura
@nshmura請看更新的問題。 –
我認爲你要找的是'CoordinatorLayout'。 http://android-developers.blogspot.com/2015/05/android-design-support-library.html –