14

Tôi có thiết lập hiện tại:Disable trên thanh công cụ di chuyển

<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/coordiator_layout_in_main" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <android.support.v7.widget.Toolbar 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:minHeight="?attr/actionBarSize" 
     app:layout_scrollFlags="scroll|enterAlways"/> 
</android.support.design.widget.AppBarLayout> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

    <android.support.design.widget.NavigationView 
     android:layout_width="170dp" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true"/> 

    <FrameLayout 
     // I place a fragment containing a viewpager containing fragments that contain a recyclerview.... 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_toRightOf="@+id/nav_view"> 

    </FrameLayout> 
</RelativeLayout> 

<FrameLayout 
    android:id="@+id/settings_frame" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:visibility="invisible"> 
</FrameLayout> 

<android.support.design.widget.FloatingActionButton 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/ic_refresh" 
    app:layout_anchor="@id/coordiator_layout_in_main" 
    app:layout_anchorGravity="bottom|right|end" 
    app:layout_behavior="com.material.widget.MyFloatingActionButtonBehavior" /> 

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

Bây giờ tất cả mọi thứ làm việc như mong đợi nếu tôi di chuyển bên trong framelayout có chứa các mảnh vỡ, các slide thanh công cụ trong và ngoài như tôi muốn

Bây giờ vấn đề là tôi muốn vô hiệu hóa thanh công cụ trượt trong và ngoài nếu tôi di chuyển các NavView mà là ở phía bên của framelayout (và bên trong RelativeLayout)

Nhưng không có vấn đề nếu tôi loại bỏ tất cả các s hành vi crolling thanh công cụ giữ trên trượt vào và ra (chỉ có cách để vô hiệu hóa nó là loại bỏ các lá cờ cuộn hình thức appbarlayout, nhưng vô hiệu hóa tất cả trượt vào và ra khỏi tolbar)

Xin vui lòng những gì tôi thiếu ở đây? Không phải là hành vi scolling phải vượt qua các sự kiện di chuyển đến CoordinatorLayout?

Trả lời

6

Thật không may, NavigationView chứa NavigationMenuViewRecyclerView và vì vậy nó hỗ trợ cuộn và di chuyển lồng nhau AppBarLayout khi được cuộn. Cách tốt nhất để giải quyết vấn đề này là di chuyển NavigationView ra khỏi số CoordinatorLayout. Nếu không thể, bạn có thể thử đoạn mã sau mà tôi chưa thử nghiệm.

final RecyclerView navigationMenuView = 
    (RecyclerView) findViewById(R.id.design_navigation_view); 
navigationMenuView.setNestedScrollingEnabled(false); 

Hãy lưu ý rằng ngay cả khi mã này hoạt động, nó có thể bị hỏng khi thư viện thiết kế được cập nhật.

6

Di chuyển NavigationView của bạn bên trong DrawerLayout và CoordinatorLayout bên trong nội dung chính của DrawerLayout.

Từ tài liệu:

NavigationView thường đặt bên trong một DrawerLayout.

<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/drawer_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true"> 

<!-- Your contents --> 

<android.support.design.widget.NavigationView 
    android:id="@+id/navigation" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    app:menu="@menu/my_navigation_items" /> 
</android.support.v4.widget.DrawerLayout> 
6

thử này,

Đặt activity_screen.xml này

<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout" 
    android:layout_width="match_parent" android:layout_height="match_parent" 
    android:fitsSystemWindows="true" tools:openDrawer="start"> 

    <include layout="@layout/app_bar_screen" android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <android.support.design.widget.NavigationView android:id="@+id/nav_view" 
     android:layout_width="wrap_content" android:layout_height="match_parent" 
     android:layout_gravity="start" android:fitsSystemWindows="true" 
     app:headerLayout="@layout/nav_header_home_screen" 
     app:itemIconTint="@color/app_theme_color" 
     app:menu="@menu/activity_home_screen_drawer" /> 

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

Đặt app_bar_screen.xml này

<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true" 
    tools:context="com.test.app.HomeScreenActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay"/> 



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

    <include layout="@layout/content_screen" /> 


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

Đặt content_screen.xml này

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:showIn="@layout/app_bar_home_screen" 
    android:id="@+id/content_frame" 
    android:background="@android:color/white" 
    tools:context="com.test.app.HomeScreenActivity"> 


</RelativeLayout> 
+0

Bạn nên nhận xét hoặc chú thích mã của mình để giúp hiểu. Nếu không, điều này chỉ dẫn đến các nhà phát triển sao chép dán. – catch22

+0

có quyền tôi nhưng không đưa ra bất kỳ câu trả lời mà không có bất kỳ thử nghiệm. Nhưng cảm ơn cho đề xuất của bạn. –

0

Đối với bất kỳ ai có thể quan tâm thì đây là cách tôi giải quyết vấn đề.

Tôi tạo ra phiên bản tùy chỉnh của tôi về NavigationView bằng cách sao chép các tập tin có liên quan tạo nguồn,

đó là:

  • NavigationView.java
  • NavigationMenuItem.java
  • NavigationMenuPresente.java
  • NavigationMenuView.java
  • ThemeUtils.java

Fix nhập khẩu để các điểm NavigationView mới để các tập tin mới được sao chép.

Và cuối cùng, hãy thêm setNestedScrollingEnabled(false) này vào hàm tạo của NavigationMenuView.

Điều này là do, làm thế nào một cách chính xác @ Michael chỉ ra sự NavigationMenuView là một RecyclerView, và như vậy nó đi sự kiện di chuyển nó đến NestedScrollingParent (các CoordinatorLayout), bằng cách thiết lập setNestedScrollingEnabled(false) chúng tôi vô hiệu hóa hành vi này và chúng tôi nhận được kết quả mong muốn (cuộn NavView không mở rộng/thu gọn AppBar)

1

Xóa dòng mã này app:layout_scrollFlags="scroll|enterAlways" khỏi số ToolBar của bạn. Làm việc trong trường hợp của tôi.

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