2016-05-20 21 views
8

Tôi đang có một ScrollView lồng nhau có chứa nội dung bên trong Layout tuyến tính.Android Nested Scrollview không cuộn

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:CoverFlowPager="http://schemas.android.com/apk/res-auto" 
    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.v4.widget.NestedScrollView 
     android:id="@+id/scroll" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginTop="10dp" 
     android:clipToPadding="false" 
     android:fillViewport="true" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

     <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 
     </LinearLayout> 

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

</FrameLayout> 

Tôi có bố cục này bên trong ViewPager và ViewPager nằm trong CordinatorLayout.

<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:id="@+id/main_content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true"> 

Bây giờ khi tôi cuộn chế độ xem, chế độ xem không cuộn. Nhưng kể từ khi bố trí được đặt bên trong bố trí Cordinator nó di chuyển lên cho đến khi ToolBar được giấu. Nhưng nó không cuộn lên.

Đây là hoạt động chính xml của tôi, Trình xem trang nằm bên trong bố cục theo tab.

<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:id="@+id/main_content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true"> 

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

      <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:app="http://schemas.android.com/apk/res-auto" 
       xmlns:attrs="http://schemas.android.com/apk/res-auto" 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:background="?attr/colorPrimary" 
       app:layout_scrollFlags="scroll|enterAlways" 
       app:popupTheme="@style/AppTheme.PopupOverlay"> 

      <FrameLayout 
       android:id="@+id/titleContainer" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center"  
       android:gravity="center"> 

       <com.CustomFontTextView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:gravity="center" 
        android:text="Toolbar Title" 
        android:textColor="#ffffff" 
        attrs:customFont="handyman_bold" 
        android:textSize="8pt" 
        android:layout_marginLeft="-20dp" 
        android:id="@+id/toolbar_title"/> 
      </FrameLayout> 

      <ImageButton 
       android:id="@+id/btn_ToolBarRightBtn" 
       android:layout_width="32dp" 
       android:layout_height="28dp" 
       android:tag="0" 
       android:background="@null" 
       android:scaleType="fitXY" 
       android:src="@drawable/icon_shopping" 
       android:layout_alignParentRight="true" 
       android:layout_gravity="right" 
       android:layout_marginRight="10dp" 
       /> 

     </android.support.v7.widget.Toolbar> 

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

    <android.support.v4.view.ViewPager 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 

    <android.support.design.widget.TabLayout 
     android:id="@+id/tabs" 
     android:layout_gravity="bottom" 
     android:background="@color/offwhite" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 

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

Dưới đây là ảnh chụp màn hình của tôi,

ban đầu Xem enter image description here

Khi xem được cuộn lên, nó cuộn chỉ để ẩn thanh điều hướng trên. Nó không di chuyển để hiển thị các mục bên dưới thanh tab,

enter image description here

+0

không nên 'android: layout_height = "match_parent" 'trong NestedScrool Xem được' android: layout_height = "wrap_content" '... và trong bố cục tuyến tính lồng nhau của bạn? – longilong

+0

Bạn có thể thêm hình ảnh về những gì bạn đang cố gắng đạt được không? – jobbert

+0

vui lòng xem ảnh chụp màn hình – Zach

Trả lời

1

app:layout_behavior="..." nên được đặt trên một con trực tiếp của CoordinatorLayout. Nếu số ViewPager của bạn là con trực tiếp của CoordinatorLayout, hãy đặt số đó vào tờ khai ViewPager.

0

Trích từ đây: Troubleshooting Coordinator Layouts

Khi phối hợp giữa một mảnh với một danh sách các mục bên trong một ViewPager và một hoạt động phụ huynh, bạn muốn đặt ứng dụng: bất động sản layout_behavior trên ViewPager như outlined here nên cuộn trong máy nhắn tin được tạo bọt và có thể được điều phối bởi CoordinatorLayout. Lưu ý rằng bạn không nên đặt ứng dụng đó: thuộc tính layout_behavior ở bất kỳ đâu trong phân đoạn hoặc danh sách bên trong.

0

Bạn có thể thử điều này! Tôi đã gặp vấn đề tương tự với ứng dụng của mình. Điều này làm việc cho tôi.

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


    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     xmlns:attrs="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:layout_scrollFlags="scroll|enterAlways" 
     app:popupTheme="@style/AppTheme.PopupOverlay"> 

     <FrameLayout 
      android:id="@+id/titleContainer" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 

      android:gravity="center"> 

      <com.CustomFontTextView 
       android:id="@+id/toolbar_title" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="-20dp" 
       android:gravity="center" 
       android:text="Toolbar Title" 
       android:textColor="#ffffff" 
       android:textSize="8pt" 
       attrs:customFont="handyman_bold" /> 
     </FrameLayout> 

     <ImageButton 
      android:id="@+id/btn_ToolBarRightBtn" 
      android:layout_width="32dp" 
      android:layout_height="28dp" 
      android:layout_alignParentRight="true" 
      android:layout_gravity="right" 
      android:layout_marginRight="10dp" 
      android:background="@null" 
      android:scaleType="fitXY" 
      android:src="@drawable/icon_shopping" 
      android:tag="0" /> 


    </android.support.v7.widget.Toolbar> 

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

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

    <android.support.design.widget.TabLayout 
     android:id="@+id/tabs" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom" 
     android:background="@color/offwhite" /> 


    <android.support.v4.view.ViewPager 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 


</LinearLayout> 

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<android.support.v4.widget.NestedScrollView 
    android:id="@+id/scroll" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_marginTop="10dp" 
    android:clipToPadding="false" 
    android:fillViewport="true"> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"></LinearLayout> 
</android.support.v4.widget.NestedScrollView> 

0

Vì vậy, đây là vấn đề, bạn không thể có một NestedScrollView trong FrameLayout.

Ngoài ra, một số thay đổi nhỏ hơn cần thiết trong xml để làm cho nó hoạt động. Giống như TabLayout phải nằm trong Giao diện AppBar và được ghim.

Dưới đây là mã của bạn ->

</android.support.v7.widget.Toolbar> 

<android.support.design.widget.TabLayout 
android:id="@+id/tabs" 
android:layout_gravity="bottom" 
android:background="@color/offwhite" 
android:layout_width="match_parent" 
app:layout_collapseMode="pin" 
app:layout_anchor="@+id/appbar" 
app:layout_anchorGravity="bottom" 
android:layout_height="wrap_content"/> 

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


<android.support.v4.view.ViewPager 
android:id="@+id/container" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 

Và -> Thay đổi mã Fragment của bạn để loại bỏ các bố cục khung hình!

<android.support.v4.widget.NestedScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:CoverFlowPager="http://schemas.android.com/apk/res-auto" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/scroll" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginTop="10dp" 
     android:clipToPadding="false" 
     android:fillViewport="true"> 

     <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 
     </LinearLayout> 

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

Hy vọng điều này sẽ hữu ích!

+0

@Zach Tôi đã cập nhật câu trả lời, vui lòng kiểm tra. –

0

thử này

scroll.setOnTouchListener(new View.OnTouchListener() { 
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     v.getParent().requestDisallowInterceptTouchEvent(true); 
     return false; 
    } 
}); 
0

Các ScrollingViewBehavior là chỉ cần thêm đỉnh và đáy bù đắp để ViewPager của bạn. Nói chung bù đắp phụ thuộc vào chiều cao AppBarLayout và nó không phụ thuộc vào chiều cao TabLayout của bạn bởi vì nó không có trong AppBarLayout. Vì vậy, đối với cách giải quyết nhanh chóng, bạn chỉ có thể thêm paddingBottom để ViewPager của bạn như thế này:

<android.support.v4.view.ViewPager 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:paddingBottom="50dp" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 

    <android.support.design.widget.TabLayout 
     android:id="@+id/tabs" 
     android:layout_gravity="bottom" 
     android:background="@android:color/black" 
     android:layout_width="match_parent" 
     android:layout_height="50dp"/> 
Các vấn đề liên quan