2015-09-21 150 views
12

我正在創建一個ListView及其相應的DetailView應用程序。 我的ListView有項目,如果點擊將用戶帶到DetailViewActivity可摺疊工具欄:設置在創建多少工具欄應該摺疊

DetailViewActivity上,我實現了一個可摺疊工具欄。 現在,每次打開DetailViewActivity時,都會在可摺疊工具欄中的ImageView上設置不同的圖像(具有不同的尺寸)。

我希望Toolbar默認應該打開到一定的高度(比如256dp),但如果圖片高度大於此值,用戶應該能夠拉下來查看圖片的其餘部分。 (完全像Whatsapp)

我已經設法以編程方式設置每次打開活動時Toolbar的高度,但問題是,Toolbar始終完全展開。因此,如果圖像較大,默認情況下工具欄非常大。無論圖像的高度如何,我都希望它可以摺疊到256dp。


我佈局的代碼是:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.design.widget.CoordinatorLayout 
     android:id="@+id/rootLayout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <android.support.design.widget.AppBarLayout 
      android:id="@+id/appbar" 
      android:layout_width="match_parent" 
      android:layout_height="@dimen/app_bar_height" 
      android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

      <android.support.design.widget.CollapsingToolbarLayout 
       android:id="@+id/collapsingToolbarLayout" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       app:contentScrim="?attr/colorPrimary" 
       app:expandedTitleMarginStart="@dimen/expanded_toolbar_title_margin_start" 
       app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

       <ImageView 
        android:id="@+id/image" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:scaleType="centerCrop" 
        android:src="@drawable/background_navdrawer" 
        app:layout_collapseMode="parallax" 
        app:layout_collapseParallaxMultiplier="0.7"/> 

       <View 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_marginTop="130dp" 
        android:background="@drawable/gradient_header_background" 
        app:layout_collapseMode="parallax" 
        app:layout_collapseParallaxMultiplier="0.1"/> 

       <android.support.v7.widget.Toolbar 
        android:id="@+id/toolbar" 
        android:layout_width="match_parent" 
        android:layout_height="?attr/actionBarSize" 
        app:layout_collapseMode="pin" 
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/> 


      </android.support.design.widget.CollapsingToolbarLayout> 

     </android.support.design.widget.AppBarLayout> 


     <android.support.v4.widget.NestedScrollView 
      android:id="@+id/detail_nested_scroll_view" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

      <FrameLayout 
       android:id="@+id/detail_container" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       tools:ignore="MergeRootFrame"/> 

     </android.support.v4.widget.NestedScrollView> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="12dp" 
      android:orientation="vertical" 
      app:layout_anchor="@+id/appbar" 
      app:layout_anchorGravity="bottom"> 

      <View 
       android:id="@+id/toolbar_shadow_transparent" 
       android:layout_width="match_parent" 
       android:layout_height="@dimen/toolbar_elevation" 
       android:background="@color/transparent"/> 

      <View 
       android:id="@+id/toolbar_shadow" 
       android:layout_width="match_parent" 
       android:layout_height="@dimen/toolbar_elevation" 
       android:background="@drawable/dropshadow"/> 
     </LinearLayout> 


     <com.github.clans.fab.FloatingActionButton 
      android:id="@+id/action_edit" 
      style="@style/MenuButtonsStyle" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginRight="10dp" 
      android:src="@drawable/ic_edit" 
      app:layout_anchor="@+id/appbar" 
      app:layout_anchorGravity="bottom|right|end"/> 

    </android.support.design.widget.CoordinatorLayout> 

    <ImageView 
     android:id="@+id/detail_back_arrow_land" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:visibility="gone"/> 

    <TextView 
     android:id="@+id/course_name_textview" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:visibility="gone"/> 

</RelativeLayout> 

而且在我的活動,我發現高度,並將其設置爲Toolbar這樣的:

appBar = (AppBarLayout) findViewById(R.id.appbar); 
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) appBar.getLayoutParams(); 
lp.height = my_bitmap.getHeight(); 
DetailActivity.appBar.setLayoutParams(lp); 
DetailActivity.mImageView.setImageBitmap(my_bitmap); 

我附上截圖讓我的p oint更清晰。

這正是我想要多高我的工具欄是每個活動被推出時間: enter image description here

這是我從我的代碼獲得:

enter image description here

現在,我可以將代碼中的高度硬編碼爲256dp,但用戶將無法向下滾動查看圖像的其餘部分。請建議。

感謝您的回覆。 任何響應可以讓我開始

+0

漂亮的鍵盤) –

回答

12

最後找出解決方案。一旦我收到新圖像,我將我的位圖作爲參數傳遞給了我的expandToolbar()方法。 heightDp參數指定視圖的初始滾動高度。 我上心從https://stackoverflow.com/a/30747281/3286614,感謝疃陳英雄

public static void expandToolbar(Bitmap bmp, int heightDp) { 
    CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appBar.getLayoutParams(); 
    AppBarLayout.Behavior behavior = new AppBarLayout.Behavior(); 
    behavior.setTopAndBottomOffset(0); 
    behavior.onNestedPreScroll(rootLayout, appBar, null, 0, bmp.getHeight() - heightDp, new int[2]); 
    params.setBehavior(behavior); 
    DetailActivity.appBar.setLayoutParams(params); 
} 

希望這有助於人。

+0

非常感謝,有沒有相同的圖書館?我會用它。 –

+0

找不到一個,我一直在拼命尋找。 – Rachit

+0

哦該死:(好吧,你的代碼我會試圖找出如何做到這一點 –