[英]Second fragment in ViewPager consumes scroll events
我有一個SwipeRefreshLayout
在我的 Activity 中托管ViewPager
。 我正在向 ViewPager 添加兩個片段。 第一個片段有RecyclerView
,第二個片段有一個ScrollView
。
在第一個片段上, SwipeRefreshLayout
正在消耗滾動事件。 我添加了OnScrollListener
,如果視圖不在頂部,我將禁用刷新。
我在第二個片段中遇到了與ScrollView
相同的問題,我將ViewTreeObserver.OnScrollChangedListener()
添加到ScrollView
。
但是,當我在第一頁時,滾動事件會傳遞到第二頁。
我試圖在第一個片段的父視圖上設置android:clickable="true"
但它沒有用。
任何建議都會有所幫助。
我的活動.xml
<include android:id="@+id/toolbar" layout="@layout/include_toolbar" />
<FrameLayout
android:id="@id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/toolbar">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/login_background"
android:fillViewport="true"
android:fitsSystemWindows="true"
android:layout_below="@id/tab_layout">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/last_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/normal_padding"
android:paddingTop="@dimen/small_padding"
android:textColor="@color/gray"
android:paddingBottom="@dimen/small_padding"
android:textSize="@dimen/caption"
android:textStyle="bold" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_below="@id/last_update"
android:layout_width="match_parent"
android:layout_height="fill_parent"/>
</RelativeLayout>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
</FrameLayout>
first_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="16dp"
android:background="@android:color/transparent"
android:clickable="true">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
</LinearLayout>
second_fragment.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:background="@android:color/transparent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true">
There are some child views here.
</ScrollView
第二片段
onScrollChangedListener = new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) getActivity().findViewById(R.id.swipe_container);
int scrollY = scrollView.getScrollY();
if (scrollY == 0) swipeRefreshLayout.setEnabled(true);
else swipeRefreshLayout.setEnabled(false);
}
};
scrollView.getViewTreeObserver().addOnScrollChangedListener(onScrollChangedListener);
這是因為第二個Fragment與第一個Fragment一起創建並獲得了焦點。 使用深度頁面變壓器時遇到了這個問題。 第二個片段是在第一個片段下渲染的,因此消耗了很多筆觸。
嘗試使用以下Page Transformer:
viewPager.setPageTransformer(false, new CustomViewPager.PageTransformer() {
@Override
public void transformPage(View view, float position) {
// TODO Auto-generated method stub
int pageWidth = view.getWidth();
int pageHeight=view.getHeight();
if (position < -1) { // [-Infinity,-1)
// This page is way off-screen to the left.
view.setAlpha(0);
}
else if (position <= 1) { // [-1,1]
float scaleFactor = Math.max(min_scale, 1 -Math.abs(position));
float vertMargin = pageHeight * (1 - scaleFactor) / 2;
float horzMargin = pageWidth * (1 - scaleFactor) / 2;
if (position < 0) {
view.setTranslationX(horzMargin - vertMargin / 2);
} else {
view.setTranslationX(-horzMargin + vertMargin / 2);
}
// Scale the page down (between MIN_SCALE and 1)
view.setScaleX(scaleFactor);
view.setScaleY(scaleFactor);
// Fade the page relative to its size.
view.setAlpha((float) (0.2 +
(scaleFactor - min_scale) /
(1 - min_scale) * (1 - 0.2)));
}
else {
// This page is way off-screen to the right.
view.setAlpha(0);
}
}
});
現在將第二個片段渲染到右側,並因此將觸摸事件傳遞到第一個片段。
例如,嘗試添加單獨的SwipeRefreshLayout
而不是進行常規聲明。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="16dp"
android:background="@android:color/transparent"
android:clickable="true">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/login_background"
android:fillViewport="true"
android:fitsSystemWindows="true"
android:layout_below="@id/tab_layout">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
如果我們add
片段而不是replace
,下面的fragment
可以消耗點擊/觸摸事件,以防頂部的fragment
不消耗它。 為避免這種情況,我們可以將頂部fragment
的root view
設置為可點擊,如下所示(不是 XML 格式):
private var binding: FragmentFragment1Binding? = null
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentFragment1Binding.inflate(inflater, container, false)
binding?.root?.isClickable = true
return binding?.root
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.