使用this tutorial實現靈活空間模式(具有摺疊工具欄的模式)。Android中的靈活空間
我試圖達到類似的效果作爲棒棒糖聯繫人活動,這在一開始進入活動時,觀看圖像頭的只是一部分:
然後,用戶可以揭示更多的它向下滾動圖像下方的佈局,直到它達到最大:
在我的應用程序中,我無法設法使其工作。
什麼情況是,在進入活動時,呈現圖像頭在它的最大尺寸,AppBarLayout的大小,就像佈局上面,而不像在棒棒堂聯繫人活動,在那裏只顯示部分的形象。
這是設定AppBarLayout(我想屏幕的寬度爲最大高度)的高度的代碼:
int widthPx = getResources().getDisplayMetrics().widthPixels;
AppBarLayout appbar = (AppBarLayout)findViewById(R.id.appbar);
appbar.setLayoutParams(new CoordinatorLayout.LayoutParams(CoordinatorLayout.LayoutParams.MATCH_PARENT, widthPx));
這是設定RecyclerView的代碼。使用scrollToPosition嘗試,認爲這將提升RecyclerView的看法,但它沒有任何效果可言:
mRecyclerView = (RecyclerView) findViewById(R.id.activity_profile_bottom_recyclerview);
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
// specify an adapter (see also next example)
if(mAdapter == null){
mAdapter = new ProfileAdapter(this, user, inEditMode);
mRecyclerView.setAdapter(mAdapter);
}
mRecyclerView.scrollToPosition(mAdapter.getItemCount() - 1); // itemCount is 4
這是佈局的xml:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_profile"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="0dp" // set programatically
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginBottom="32dp"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/header"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="@+id/anim_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/activity_profile_bottom_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
<include
layout="@layout/navigation_view"/>
</android.support.v4.widget.DrawerLayout>
注:如果我手動向下滾動,RecyclerView會下降並顯示更多的圖像,但它不會通過代碼工作。
我認爲scrollToPosition不是解決方案,有沒有人有任何想法?
想過用enterAlwaysCollapsed標誌也許在CoordinatorLayout mentioned here與了minHeight appbar部分:
enterAlwaysCollapsed:當你的觀點已經宣佈了minHeight,你 使用這個標誌,您的瀏覽只會在進入其最小高度(即, '摺疊'),當滾動 視圖達到最高時,僅重新展開至其全高。
所以,我在我的RecyclerView中設置了滾動| enterAlwaysCollapsed標誌到我的工具欄和minHeight,這沒有奏效。然後我嘗試將minHeight移動到其他佈局,如AppBarLayout,沒有任何工作。它只是縮小了圖像,有時沒有整個視圖。
http://stackoverflow.com/questions/31945667/set-initial-height-of-parallax-image-in-collapsingtoolbarlayout – tachyonflux
其他解決方案不存在? @karaokyo –
謝謝@karaokyo,這實際上工作。仍試圖找出是否還有其他解決方案。 – Jjang