ปิดใช้งานการเลื่อนแถบเครื่องมือ

ฉันมีการตั้งค่าปัจจุบัน:

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

ตอนนี้ทุกอย่างทำงานได้ตามที่คาดไว้ ถ้าฉันเลื่อนเข้าไปภายในเค้าโครงเฟรมที่มีส่วนต่างๆ แถบเครื่องมือจะเลื่อนเข้าและออกตามที่ฉันต้องการ

ประเด็นก็คือ ฉันต้องการ ปิดการใช้งานแถบเครื่องมือที่เลื่อนเข้าและออก หากฉันเลื่อน NavView ซึ่งอยู่ที่ด้านข้างของเค้าโครงเฟรม (และภายในเค้าโครงแบบสัมพันธ์)

แต่ไม่ว่าฉันจะลบพฤติกรรมการเลื่อนทั้งหมดออกหรือไม่ แถบเครื่องมือก็ยังคงเลื่อนเข้าและออก (วิธีเดียวที่จะปิดใช้งานได้คือลบค่าสถานะการเลื่อนออกจากเค้าโครงแอปพลิเคชัน แต่จะปิดใช้งานการเลื่อนเข้าและออกจากแถบเครื่องมือทั้งหมด)

กรุณาสิ่งที่ฉันหายไปที่นี่? พฤติกรรมการสก๊อตไม่ควรส่งผ่านเหตุการณ์การเลื่อนไปยัง CoordinatorLayout ใช่หรือไม่




คำตอบ (5)


น่าเสียดายที่ NavigationView มี NavigationMenuView ซึ่งเป็น RecyclerView ดังนั้นจึงรองรับการเลื่อนแบบซ้อนและการเคลื่อนไหว AppBarLayout เมื่อเลื่อน วิธีที่ดีที่สุดในการแก้ปัญหานี้คือการย้าย NavigationView ออกจาก CoordinatorLayout หากเป็นไปไม่ได้ คุณสามารถลองใช้โค้ดต่อไปนี้ซึ่งฉันไม่ได้ทดสอบ

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

โปรดคำนึงว่าแม้ว่ารหัสนี้จะใช้งานได้ แต่ก็อาจเสียหายได้เมื่ออัปเดตไลบรารีการออกแบบ

person Michael    schedule 09.04.2016

ย้าย NavigationView ของคุณภายใน DrawerLayout และ CoordinatorLayout ภายในเนื้อหาหลักของ DrawerLayout

จากเอกสาร:

โดยทั่วไป NavigationView จะถูกวางไว้ภายใน 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>
person GPack    schedule 10.04.2016

ลองสิ่งนี้

ใส่ Activity_screen.xml นี้

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

ใส่ app_bar_screen.xml นี้

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

ใส่ content_screen.xml นี้

 <?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>
person Pradeep Gupta    schedule 15.04.2016
comment
คุณควรแสดงความคิดเห็นหรือใส่คำอธิบายประกอบโค้ดของคุณเพื่อช่วยในการทำความเข้าใจ มิฉะนั้นสิ่งนี้จะนำไปสู่นักพัฒนาซอฟต์แวร์คัดลอกและวาง - person catch22; 16.04.2016
comment
ใช่แล้ว ฉันไม่ได้ให้คำตอบใด ๆ โดยไม่มีการทดสอบใด ๆ แต่ขอบคุณสำหรับข้อเสนอแนะของคุณ - person Pradeep Gupta; 18.04.2016

ลบบรรทัดของ codeapp:layout_scrollFlags="scroll|enterAlways" นี้ออกจาก ToolBar ของคุณ ทำงานในกรณีของฉัน

person Mikhail    schedule 22.09.2017

สำหรับใครก็ตามที่อาจสนใจ นี่คือวิธีที่ฉันจะจัดการกับปัญหานี้

ฉันสร้าง NavigationView เวอร์ชันที่กำหนดเองของฉันโดยการคัดลอกไฟล์ที่เกี่ยวข้องจากแหล่งที่มา

พวกเขาคือ:

  • NavigationView.java
  • NavigationMenuItem.java
  • ระบบนำทางMenuPresente.java
  • NavigationMenuView.java
  • ThemeUtils.java

แก้ไขการนำเข้าเพื่อให้ NavigationView ใหม่ชี้ไปที่ไฟล์ที่คัดลอกใหม่

และสุดท้ายก็เพิ่ม setNestedScrollingEnabled(false) นี้ลงใน Constructor ของ NavigationMenuView

นี่เป็นเพราะว่า @Michael ชี้ให้เห็นอย่างถูกต้องว่า NavigationMenuView เป็น RecyclerView ได้อย่างไร และด้วยเหตุนี้ มันจึงส่งผ่านเหตุการณ์การเลื่อนไปที่ NestedScrollingParent (CoordinorLayout) โดยการตั้งค่า setNestedScrollingEnabled(false) เราปิดการใช้งานพฤติกรรมนี้ และเราได้ผลลัพธ์ที่ต้องการ (การเลื่อน NavView ทำได้ ไม่ขยาย/ยุบ AppBar)

person Lisa Anne    schedule 16.04.2016