2015-08-12 25 views
7

Tôi đang sử dụng Công cụ thu gọn Bố cục với NestedScrollView và Viewpager bên trong Scrollview lồng nhau.Công cụ thu gọnBar Layout với Viewpager bên trong NestedScrollView

Tôi đang có 3 tab và có 3 đoạn cho các tab này. Những mảnh này đang sử dụng RecyclerView để thiết lập dữ liệu.

Bây giờ với nestedScrollView và viewpager, khi tôi cuộn nội dung RecyclerView, hiệu ứng thu gọn không hoạt động với điều này.

Tôi cũng cần đặt NestedScrollView, vì tôi có một số thông tin mà tôi cần hiển thị ở trên Tab.

Đây là mã của tôi:

<android.support.design.widget.AppBarLayout 
    android:id="@+id/appbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
    android:fitsSystemWindows="true"> 

    <android.support.design.widget.CollapsingToolbarLayout 
    android:id="@+id/collapsing_toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_scrollFlags="scroll|exitUntilCollapsed" 
    android:fitsSystemWindows="true" 
    app:contentScrim="?attr/colorPrimary" 
    app:expandedTitleMarginEnd="64dp" 
    app:expandedTitleMarginStart="48dp" 
    app:expandedTitleTextAppearance="@style/TransparentText"> 

    <FrameLayout 
     android:id="@+id/carouselLayout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_scrollFlags="scroll|exitUntilCollapsed" 
     android:fitsSystemWindows="true" 
     app:layout_collapseMode="parallax"> 

     <ImageView 
      android:id="@+id/coverImage" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:src="@drawable/example" 
      android:scaleType="centerCrop"/> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:gravity="bottom" 
      android:orientation="vertical" 
      android:layout_gravity="bottom" 
      android:padding="@dimen/profile_image_margin" 
      android:background="@drawable/gradient_bg" 
      app:layout_behavior="@string/appbar_scrolling_view_behavior" 
      android:layout_height="wrap_content"> 

      <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="@dimen/profile_image_margin" 
      android:textSize="@dimen/text_size_xlarge" 
      android:textStyle="bold" 
      android:textColor="@color/white" 
      android:text="Title" 
      android:id="@+id/content_title"/> 

     </LinearLayout> 

    </FrameLayout> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      app:theme="@style/ActionBarThemeOverlay" 
      app:popupTheme="@style/ActionBarPopupThemeOverlay" 
      app:layout_scrollFlags="scroll|enterAlways" 
      android:background="@drawable/gradient_bg" /> 

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

<android.support.v4.widget.NestedScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="fill" 
    android:isScrollContainer="true" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

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

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="sdjsfksdfsd" 
      android:textColor="@color/red"/> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="csdffsfsdfsdfsdf" 
      android:textColor="@color/red"/> 

     <android.support.design.widget.TabLayout 
      android:id="@+id/slidingTabs" 
      android:layout_width="match_parent" 
      app:tabMode="scrollable" 
      app:tabGravity="fill" 
      android:layout_height="wrap_content"/> 

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

     </android.support.v4.view.ViewPager> 
     </LinearLayout> 
    </android.support.v4.widget.NestedScrollView> 

Xin hãy giúp tôi nếu bạn có bất kỳ ý tưởng làm thế nào để thực hiện Recyclerview bên NestedScrollview trong android, để tôi có thể làm việc này .

+0

Có xem xét này http://stackoverflow.com/questions/30580954/viewpager-in -a-nestedscrollview? rq = 1 – CaptRisky

+0

bạn có thể vui lòng xây dựng hoặc thay vào đó tải lên ảnh chụp nhanh. Vì vậy, chúng tôi sẽ giúp bạn với các giải pháp thay thế hoặc giải pháp phù hợp. –

+0

vui lòng tham khảo [this] (http://inthecheesefactory.com/blog/android-design-support-library-codelab/en) –

Trả lời

3

Có vẻ như này đã được trả lời ở đây: https://stackoverflow.com/a/31561790/4931825

Bạn cũng có thể kiểm tra ví dụ này được cung cấp trong câu trả lời nói trên https://github.com/TheLittleNaruto/SupportDesignExample/

Tác giả tạo một lớp tùy chỉnh NestedScrollView mở rộng NestedScrollView rằng overrode phương pháp onInterceptTouchEvent như vậy:

@Override 
public boolean onInterceptTouchEvent(MotionEvent ev) { 
    final float x = ev.getX(); 
    final float y = ev.getY(); 
    switch (ev.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
      xDistance = yDistance = 0f; 
      lastX = ev.getX(); 
      lastY = ev.getY(); 
      break; 
     case MotionEvent.ACTION_MOVE: 
      final float curX = ev.getX(); 
      final float curY = ev.getY(); 
      xDistance += Math.abs(curX - lastX); 
      yDistance += Math.abs(curY - lastY); 
      lastX = curX; 
      lastY = curY; 
      if (xDistance > yDistance) 
       return false; 
    } 

    return super.onInterceptTouchEvent(ev) || ev.getPointerCount() == 2; 
} 

và lớp ViewPager tùy chỉnh mở rộng ViewPager rằng overrode phương pháp onMeasure của nó như sau:

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)  { 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

    int height = 0; 
    for (int i = 0; i < getChildCount(); i++) { 
     View child = getChildAt(i); 
     child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); 
     int h = child.getMeasuredHeight(); 
     if (h > height) height = h; 
    } 
    heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
} 

Cuối cùng, ông đã sử dụng cả hai bên trong CoordinatorLayout:

<com.thelittlenaruto.supportdesignexample.customview.MyNestedScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

    <LinearLayout 
     android:id="@+id/lin" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:visibility="visible"> 

     <com.thelittlenaruto.supportdesignexample.customview.WrapContentHeightViewPager 
      android:id="@+id/viewPager" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

    </LinearLayout> 

</com.thelittlenaruto.supportdesignexample.customview.MyNestedScrollView> 
+0

Điều này không cung cấp câu trả lời cho câu hỏi. Khi bạn có đủ [danh tiếng] (http://stackoverflow.com/help/whats-reputation), bạn sẽ có thể [nhận xét về bất kỳ bài đăng nào] (http://stackoverflow.com/help/privileges/comment); thay vào đó, [cung cấp câu trả lời không yêu cầu làm rõ từ người hỏi] (http://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can- i-do-thay thế). - [Từ đánh giá] (/ review/low-quality-posts/14760873) – GilZ

+0

Trong khi liên kết này có thể trả lời câu hỏi, tốt hơn nên bao gồm các phần quan trọng của câu trả lời ở đây và cung cấp liên kết để tham khảo. Câu trả lời chỉ liên kết có thể trở thành không hợp lệ nếu trang được liên kết thay đổi. - [Từ đánh giá] (/ review/low-quality-posts/14760873) –

+0

@FelixSFD, hy vọng chỉnh sửa suffices –

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