2015-09-21 42 views
12

Tôi đang tạo một ListView và ứng dụng DetailView tương ứng của nó. My ListView có các mục nếu được nhấp đưa người dùng đến DetailViewActivity.Thanh công cụ có thể gập lại: Đặt số lượng thanh công cụ sẽ được thu gọn trong onCreate

Trên DetailViewActivity, tôi đã triển khai Thanh công cụ có thể thu gọn. Bây giờ, mỗi khi mở DetailViewActivity, một hình ảnh khác (có kích thước khác nhau) được đặt trên ImageView trong Thanh công cụ có thể thu gọn.

Tôi muốn rằng Toolbar phải mở lên một chiều cao nhất định theo mặc định (nói 256dp), nhưng nếu chiều cao hình ảnh lớn hơn, người dùng sẽ có thể kéo xuống để xem phần còn lại của hình ảnh. (giống như Whatsapp)

Tôi đã quản lý lập trình chiều cao của Toolbar mỗi khi tôi mở hoạt động, nhưng vấn đề là, Toolbar luôn được mở rộng hoàn toàn. Vì vậy, nếu hình ảnh lớn hơn, Thanh công cụ theo mặc định là rất lớn. Tôi muốn nó được thu gọn xuống 256dp bất chấp chiều cao của hình ảnh.


Mã cho bố trí của tôi là:

<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> 

Và trong Hoạt động của tôi, tôi đã tìm thấy chiều cao và đặt nó vào Toolbar như thế này:

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); 

Tôi đang đính kèm ảnh chụp màn hình để làm cho p của tôi oint rõ ràng hơn.

Đây chính là cách cao Tôi muốn Thanh công cụ của tôi là mỗi khi hoạt động này được đưa ra: enter image description here

Và đây là những gì tôi nhận được từ mã của tôi:

enter image description here

Bây giờ, tôi có thể mã hóa chiều cao lên 256dp trong mã, nhưng sau đó người dùng sẽ không thể cuộn xuống để xem phần còn lại của hình ảnh. Xin đề nghị.

Cảm ơn bạn đã phản hồi. Mọi phản hồi có thể khiến tôi bắt đầu

+0

Bàn phím đẹp) –

Trả lời

12

Đã tìm ra giải pháp cuối cùng. Khi tôi nhận được hình ảnh mới, tôi đã chuyển Bitmap của mình như một tham số đến phương thức expandToolbar() của tôi. Tham số heightDp chỉ định scrolledHeight ban đầu của View. tôi đã có ý tưởng từ https://stackoverflow.com/a/30747281/3286614, nhờ Tuấn Trần Anh

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); 
} 

Hy vọng điều này sẽ giúp ai đó.

+0

Cảm ơn rất nhiều, có thư viện nào giống nhau không? Tôi sẽ sử dụng nó. –

+0

Không thể tìm thấy một, tôi đã tìm kiếm một cách tuyệt vọng. – Rachit

+0

Oh damn :(! Tốt với mã của bạn tôi sẽ cố gắng tìm ra cách để làm điều đó –

Các vấn đề liên quan