11

Tôi đang gặp phải sự cố cuộn lớn với ViewPager bên trong CoordinatorLayout.Cuộn đột ngột với NestedScrollView trong ViewPager Fragment bên trong một CoordinatorLayout Android

Quang cảnh hiện tại:

  1. app:appbarScrollingViewBehaviour được thiết lập trên ViewPager.
  2. Đoạn ViewPagerNestedScrollView làm cha mẹ gốc của nó.
  3. Bố cục đoạn có chứa Ngang RecyclerView.
  4. Khi tôi cuộn lên bằng cách chạm vào trên RecyclerView, thanh ứng dụng không cuộn, thay vào đó chỉ cuộn lồng nhau xảy ra.
  5. Khi tôi cuộn bằng cách chạm vào phần còn lại của chế độ xem, các cuộn số CoordinatorLayout. 6) Ngoài ra cuộn lồng nhau dừng giữa chừng và cắt bỏ nội dung.

Những gì tôi đã cố gắng đã:

  1. Đưa ViewPager bên NestedScrollView và thiết lập thuộc tính để app:appbarScrollingViewBehaviourNestedScrollView. Điều này hoàn toàn cắt bỏ nội dung đoạn như RecyclerView không thể quyết định chiều cao của nó.
  2. Đặt setAutoMeasureEnabled(true) trên RecyclerView, vẫn không thể sửa chiều cao.
  3. Và nhiều cách tiếp cận khác.

    Vui lòng trợ giúp về vấn đề này. Bạn có thể xem hành vi hiện tại trong tải lên .gif. Tôi cũng đăng mã của mình cho bố cục Hoạt động và bố cục phân đoạn và bố cục hàng RecyclerView.

Cảm ơn bạn

activity.xml:

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

<android.support.design.widget.AppBarLayout 
    android:id="@+id/appbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/transparent"> 

    <android.support.design.widget.CollapsingToolbarLayout 
     android:id="@+id/collapsingLayout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true" 
     android:elevation="0dp" 
     app:layout_scrollFlags="scroll|exitUntilCollapsed" 
     android:minHeight="?attr/actionBarSize"> 


     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:fitsSystemWindows="true" 
      app:layout_scrollFlags="scroll|enterAlways" 
      android:layout_height="?attr/actionBarSize"> 

      <FrameLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

       <ImageView 
        android:layout_width="30dp" 
        android:layout_height="30dp" 
        android:layout_gravity="center_vertical" 
        android:src="@drawable/ic_arrow_back" /> 



    <com.fitsquare.app.fitsquare.Entity.Customs.CustomFontTextView 
        android:layout_width="wrap_content" 
        android:layout_height="match_parent" 
        android:layout_gravity="center_horizontal" 
        android:gravity="center_vertical" 
        android:text="Workout" 
        android:textColor="#FFFFFF" 
        android:textSize="19sp" 
        android:textStyle="bold" 
        Customs:font="LatoRegular.ttf" /> 
      </FrameLayout> 

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


      <com.github.mikephil.charting.charts.LineChart 
       android:id="@+id/workout_insights_graph" 
       android:layout_width="match_parent" 
       android:layout_height="150dp" 
       android:layout_below="@+id/toolbar" 
       app:layout_scrollFlags="scroll|snap" 
       android:layout_marginTop="?attr/actionBarSize" 
       app:layout_collapseMode="parallax" 
       android:animateLayoutChanges="true" /> 

      <com.fitsquare.app.fitsquare.Entity.Customs.SlidingTabLayout 
       android:id="@+id/tabs" 
       android:layout_width="match_parent" 
       android:layout_below="@+id/diet_insights_graph" 
       android:layout_height="50dp" 
       android:layout_gravity="bottom" 
       android:background="@color/transparent" /> 
     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="right|top" 
      android:layout_marginRight="-50dp" 
      android:layout_marginTop="-50dp" 
      android:src="@drawable/workout_translucent" /> 

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




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


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

pager_fragment.xml:

<?xml version="1.0" encoding="utf-8"?> 


<android.support.v4.widget.NestedScrollView  
xmlns:android="http://schemas.android.com/apk/res/android" 

xmlns:Customs="http://schemas.android.com/apk/res-auto" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:card_view="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:paddingLeft="7dp" 
     android:paddingRight="7dp" 
     android:paddingTop="10dp"> 


     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:orientation="vertical"> 


      <LinearLayout 
       android:layout_width="100dp" 
       android:layout_height="4dp" 
       android:layout_marginTop="10dp" 
       android:alpha="0.5" 
       android:background="@drawable/round_gray_dark" 
       android:orientation="horizontal"></LinearLayout> 


      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="5dp" 
       android:gravity="center_vertical" 
       android:orientation="horizontal"> 


       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="vertical" 
        android:paddingLeft="10dp"> 


        <LinearLayout 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginTop="10dp" 
         android:orientation="horizontal"> 


    <com.fitsquare.app.fitsquare.Entity.Customs.CustomFontTextView 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="Target Calories to burn : " 
          android:textColor="@color/white" 
          android:textSize="14sp" 
          Customs:font="LatoRegular.ttf" /> 



<com.fitsquare.app.fitsquare.Entity.Customs.CustomFontTextView 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="2300" 
          android:textColor="@color/white" 
          android:textSize="14sp" 
          android:textStyle="bold" 
          Customs:font="LatoRegular.ttf" /> 
        </LinearLayout> 

        <LinearLayout 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginTop="10dp" 
         android:orientation="horizontal"> 



<com.fitsquare.app.fitsquare.Entity.Customs.CustomFontTextView 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="Burned calories : " 
          android:textColor="@color/white" 
          android:textSize="14sp" 
          Customs:font="LatoRegular.ttf" /> 


    <com.fitsquare.app.fitsquare.Entity.Customs.CustomFontTextView 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="2300" 
          android:textColor="@color/white" 
          android:textSize="14sp" 
          android:textStyle="bold" 
          Customs:font="LatoRegular.ttf" /> 
        </LinearLayout> 

        <LinearLayout 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginTop="10dp" 
         android:orientation="horizontal"> 


    <com.fitsquare.app.fitsquare.Entity.Customs.CustomFontTextView 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="Total workout time : " 
          android:textColor="@color/white" 
          android:textSize="14sp" 
          Customs:font="LatoRegular.ttf" /> 



    <com.fitsquare.app.fitsquare.Entity.Customs.CustomFontTextView 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="20 mins" 
          android:textColor="@color/white" 
          android:textSize="14sp" 
          android:textStyle="bold" 
          Customs:font="LatoRegular.ttf" /> 
        </LinearLayout> 


       </LinearLayout> 

      </LinearLayout> 

      <LinearLayout 
       android:layout_width="100dp" 
       android:layout_height="4dp" 
       android:layout_marginTop="10dp" 
       android:alpha="0.5" 
       android:background="@drawable/round_gray_dark" 
       android:orientation="horizontal"></LinearLayout> 
     </LinearLayout> 


     <com.fitsquare.app.fitsquare.Entity.Customs.CustomFontTextView 
      android:id="@+id/textView27" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="20dp" 
      android:text="Today" 
      android:textColor="@color/white" 
      android:textSize="21sp" 
      Customs:font="LatoRegular.ttf" /> 

     <LinearLayout 
      android:layout_width="100dp" 
      android:layout_height="4dp" 
      android:layout_marginTop="5dp" 
      android:alpha="0.5" 
      android:background="@drawable/round_gray_dark" 
      android:orientation="horizontal"></LinearLayout> 


     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:orientation="vertical"> 

      <android.support.v7.widget.RecyclerView 
       android:id="@+id/recycler_view" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 
     </android.support.v7.widget.RecyclerView> 


      <com.fitsquare.app.fitsquare.Entity.Customs.CustomFontTextView 
       android:id="@+id/textView31" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Tomorrow" 
       android:textColor="@color/white" 
       android:textSize="21sp" 
       Customs:font="LatoRegular.ttf" /> 

      <LinearLayout 
       android:layout_width="100dp" 
       android:layout_height="4dp" 
       android:layout_marginTop="5dp" 
       android:alpha="0.5" 
       android:background="@drawable/round_gray_dark" 
       android:orientation="horizontal"></LinearLayout> 


      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:paddingLeft="10dp"> 


       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="10dp" 
        android:orientation="horizontal"> 


     <com.fitsquare.app.fitsquare.Entity.Customs.CustomFontTextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="Target Calories to burn : " 
         android:textColor="@color/white" 
         android:textSize="14sp" 
         Customs:font="LatoRegular.ttf" /> 


     <com.fitsquare.app.fitsquare.Entity.Customs.CustomFontTextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="2300" 
         android:textColor="@color/white" 
         android:textSize="14sp" 
         android:textStyle="bold" 
         Customs:font="LatoRegular.ttf" /> 
       </LinearLayout> 


       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="10dp" 
        android:orientation="horizontal"> 


     <com.fitsquare.app.fitsquare.Entity.Customs.CustomFontTextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="Total workout time : " 
         android:textColor="@color/white" 
         android:textSize="14sp" 
         Customs:font="LatoRegular.ttf" /> 


     <com.fitsquare.app.fitsquare.Entity.Customs.CustomFontTextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="20 mins" 
         android:textColor="@color/white" 
         android:textSize="14sp" 
         android:textStyle="bold" 
         Customs:font="LatoRegular.ttf" /> 
       </LinearLayout> 
      </LinearLayout> 

     </LinearLayout> 
    </LinearLayout> 

    <!-- <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:clickable="true" 
     android:src="@color/transparent" />--> 
</FrameLayout> 

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

Bạn có thể giải thích chi tiết vấn đề của mình không? Bạn muốn đạt được những gì? – rom4ek

Trả lời

9

Tôi tìm thấy giải pháp và giải quyết vấn đề bản thân mình. Khi RecyclerView nằm ngang là một con của Nested Scroll View, nó không chuyển các sự kiện cuộn đến CoordinatorLayout. Tôi chỉ giải quyết nó bằng cách vô hiệu hóa cuộn lồng nhau bằng cách sử dụng dòng mã này.

recyclerView.setNestedScrollingEnabled(false);

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