2015-09-21 149 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

哦该死:(好吧,你的代码我会试图找出如何做到这一点 –