11

Tôi đang sử dụng một ViewPager trong một CoordinatorLayout (từ phiên bản mới nhất của Thư viện thiết kế) trong một Hoạt động. Một số phân đoạn cho ViewPager này có bố trí như RecyclerView hoặc NestedScrollView, nhưng một số không thể cuộn được cho nội dung nhỏ của chúng.Phân đoạn không cuộn trong một ViewPager bên trong CoordinatorLayout

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

     <android.support.v7.widget.Toolbar 
      android:id="@+id/tabanim_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:layout_scrollFlags="scroll|enterAlways" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 


     <android.support.design.widget.TabLayout 
      android:id="@+id/tabanim_tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

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

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

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

Nhưng trong một Fragment với một FrameLayout như xem gốc, tôi cần phải có một nút được neo vào phía dưới, nhưng nó dường như được rút ra khỏi màn hình. Để có thể nhìn thấy nó, tôi cần phải thêm phần đệm đáy bằng với chiều cao của Thanh công cụ.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/white"> 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:text="Home screen" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="70dp" 
     android:layout_gravity="bottom" 
     android:gravity="center" 
     android:text="@string/brand" 
     android:textColor="@color/brandColor" 
     android:textSize="20sp" /> 
</FrameLayout> 

Tương tự như vậy, bố cục_gravity được đặt thành 'center' trên phần tử dường như không ở giữa khu vực hiển thị cho đoạn này.

Hiểu biết của tôi là CoordinatorLayout chỉ có ý định làm việc với nội dung cuộn, đúng không? Vì vậy, chỉ sử dụng ViewGroup thông thường như FrameLayout, RelativeLayout, LinearLayout cho các đoạn ViewPager sẽ có phần dưới cùng của chúng được vẽ ngoài màn hình?

Trong trường hợp đó, tôi có cần xóa nút này khỏi bố cục đoạn này và di chuyển nó đến bố cục hoạt động chứa CoordinatorLayout không? Nó chỉ cần được hiển thị trên đoạn đầu tiên.

+1

Bạn đã bao giờ nhận được một giải pháp cho việc này? – g0ld2k

+1

@cdodo u tìm thấy giải pháp nào? – Erum

+0

Tôi tìm thấy một giải pháp. Kiểm tra câu trả lời bên dưới – jaffa

Trả lời

0

Sự cố là do việc chặn các sự kiện chạm từ ViewPager bởi vùng chứa chính. Phụ huynh cuộn container lắng nghe cho các sự kiện cảm ứng di chuyển và nếu có sự thay đổi theo chiều dọc nó đánh cắp sự kiện từ quan điểm con của nó. Nó chỉ nghĩ rằng người dùng muốn di chuyển container cha mẹ theo chiều dọc

Dưới đây là mã quyết nó

import android.content.Context; 
import android.util.AttributeSet; 
import android.view.GestureDetector; 
import android.view.MotionEvent; 
import android.view.View; 
import android.widget.ScrollView; 

public class CustomScrollView extends ScrollView { 

    private GestureDetector mGestureDetector; 
    private View.OnTouchListener mGestureListener; 

    public CustomScrollView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     mGestureDetector = new GestureDetector(context, new YScrollDetector()); 
     setFadingEdgeLength(0); 
    } 

    @Override 
    public boolean onInterceptTouchEvent(MotionEvent ev) { 
     return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev); 
    } 

    class YScrollDetector extends GestureDetector.SimpleOnGestureListener { 
     @Override 
     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { 
      return Math.abs(distanceY) > Math.abs(distanceX); 
     } 
    } 

} 

Sử dụng lớp này thay vì bạn ViewPager

<(your packagename).CustomScrollView 
    android:id="@+id/tabanim_viewpager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/> 
3

Từ tài liệu tham khảo để this answer dưới đây làm việc cho tôi

Kéo dài hành vi cuộn xem

public class MyBehavior extends AppBarLayout.ScrollingViewBehavior { 

private View layout; 

public MyBehavior() { 
} 

public MyBehavior(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

@Override 
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) { 
    boolean result = super.onDependentViewChanged(parent, child, dependency); 
    if (layout != null) { 
     layout.setPadding(layout.getPaddingLeft(), layout.getPaddingTop(), layout 
       .getPaddingRight(), layout.getTop()); 
    } 
    return result; 
} 

public void setLayout(View layout) { 
    this.layout = layout; 
} 
} 

Chỉ định rằng để ViewPager

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

<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.support.v7.widget.Toolbar 
     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/ThemeOverlay.AppCompat.Light" /> 
</android.support.design.widget.AppBarLayout> 
<org.nsu.myapplication.VerticalViewPager 
    android:id="@+id/viewpager" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    app:layout_behavior="your.domain.name.MyBehavior" 
    /> 
    </android.support.design.widget.CoordinatorLayout> 

và trong onCreate hoạt động

CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) viewPager.getLayoutParams(); 
    MyBehavior behavior = (MyBehavior) lp.getBehavior(); 
    behavior.setLayout(viewPager); 
Các vấn đề liên quan