2015-09-21 13 views
12

私はListViewとそれに対応するDetailViewアプリケーションを作成しています。 私のListViewには、クリックするとDetailViewActivityに移動するアイテムがあります。折りたたみツールバー:onCreateでツールバーを折りたたむ量を設定します

DetailViewActivityでは、折りたたみツールバーを実装しました。 DetailViewActivityが開かれるたびに、折りたたみツールバー内のImageViewに別の画像(寸法が異なる)が設定されます。

は、デフォルトでは特定の高さ(たとえば256dp)まで開いていなければなりませんが、画像の高さがそれよりも大きい場合、ユーザーはプルダウンして残りの画像を表示できるはずです。

私はアクティビティを開くたびに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); 

私はスクリーンショットをつけて自分の写真を撮っています澄んだ汚れ。 enter image description here

そして、これは私が私のコードから得るものです::これはまさに私が私のツールバーは、すべての活動を立ち上げます、時間にしたいどのように背の高いです

enter image description here

、Iコードで高さを256dpにハードコードすることができますが、ユーザーは画像の残りの部分を見るためにスクロールすることができません。提案してください。

ありがとうございます。 何か応答が私を起動させる可能性があります

+0

Pretty keyboard) –

答えて

12

最後に解決策を考え出しました。新しい画像を受け取ったら、expandToolbar()メソッドのパラメータとしてBitmapを渡しました。 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

1つを見つけることができませんでした、私は必死に1つを探していました。 – Rachit

+0

ああ、私はそれをやり遂げる方法を見つけようとしています。あなたのコードをよく理解してください。 –