Android 选项卡布局仅在快速滚动时折叠,而不是在慢速滚动时折叠
对于下面的布局,当我在viewpager中向上滚动页面内容时。。。如果我把手指放在屏幕上,慢慢地向上滚动,选项卡布局不会折叠到工具栏中,但如果我向上滚动内容,它会折叠到工具栏中。你知道为什么吗 activity_home.xmlAndroid 选项卡布局仅在快速滚动时折叠,而不是在慢速滚动时折叠,android,android-tablayout,android-coordinatorlayout,android-appbarlayout,Android,Android Tablayout,Android Coordinatorlayout,Android Appbarlayout,对于下面的布局,当我在viewpager中向上滚动页面内容时。。。如果我把手指放在屏幕上,慢慢地向上滚动,选项卡布局不会折叠到工具栏中,但如果我向上滚动内容,它会折叠到工具栏中。你知道为什么吗 activity_home.xml <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".newnav.home.HomeActivity">
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".newnav.NavigationActivity">
<!-- Check whether do we need this surface view still? -->
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="0dp"
android:layout_height="0dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.my.package.view.MyToolBar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@color/primary_color" />
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar" />
<com.my.package.view.SomeCustomView
android:id="@+id/breaking_news_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
</RelativeLayout>
<com.my.package.navigation.NavigationLayout
android:id="@+id/navigation_layout"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/white"
android:fitsSystemWindows="true" />
</android.support.v4.widget.DrawerLayout>
</layout>
fragment_home.xml(父布局具有工具栏):
section_fragment.xml(滚动的视图):
旧答案
此解决方案在这种情况下不起作用。但它可能对其他人有帮助。
您尚未提供代码的必要部分。但是,我强烈怀疑您使用的是介于SwipeRefreshLayout
和Recyclerview
(或ListView
)之间的中间布局。换句话说,SwipeRefreshLayout
不是Recyclerview
的直接父级。修好它,它就会工作
如果您想知道背景中真正发生了什么 如果您查看android源代码,可以找到一个方法
canSwipeRefreshChildScrollUp()
。它负责决定此布局的子视图是否可以向上滚动。如果子视图是自定义视图,则需要覆盖此选项
public boolean canChildScrollUp() {
if (mChildScrollUpCallback != null) {
return mChildScrollUpCallback.canChildScrollUp(this, mTarget);
}
if (android.os.Build.VERSION.SDK_INT < 14) {
if (mTarget instanceof AbsListView) {
final AbsListView absListView = (AbsListView) mTarget;
return absListView.getChildCount() > 0
&& (absListView.getFirstVisiblePosition() > 0 || absListView.getChildAt(0)
.getTop() < absListView.getPaddingTop());
} else {
return ViewCompat.canScrollVertically(mTarget, -1) || mTarget.getScrollY() > 0;
}
} else {
return ViewCompat.canScrollVertically(mTarget, -1);
}
}
public boolean canChildScrollUp(){
if(mChildScrollUpCallback!=null){
返回mChildScrollUpCallback.canChildScrollUp(this,mTarget);
}
if(android.os.Build.VERSION.SDK_INT<14){
if(AbsListView的mTarget实例){
最终AbsListView AbsListView=(AbsListView)mTarget;
返回absListView.getChildCount()>0
&&(absListView.getFirstVisiblePosition()>0 | | absListView.getChildAt(0)
.getTop()0;
}
}否则{
返回ViewCompat.CanScrollVertical(mTarget,-1);
}
}
如果使用中间视图,滑动刷新布局将假定它是目标(请参见方法第6行中的mTarget),该视图的默认实现始终返回false,这将导致此问题
新答案
协调布局“的子级应实现NestedScrollingChild接口。因此,尝试在嵌套的滚动视图中包装您的框架布局您可能应该在
表格布局
@RowlandMtetezi上定义app:layout\u collapseMode
,如果我在表格布局上设置collapseMode,它根本不会折叠。您是否尝试删除android:fitsystemwindows=“true”?@ParaskevasNtsounos是的我have@Onik有很多习俗在发生。简而言之,它的活动(具有工具栏)->父片段(具有选项卡布局和viewpager)->子片段(x2)。在子片段中,我们有一个swipe刷新视图和一个recycler viewrecyclerView是SwiperFreshLayout的直接子视图,但刷新布局不是该布局文件中的顶级视图。它是frameLayout>frameLayout>refresh layout>RecycleServiceWCoordinatorLayout“的直接子级应实现NestedScrollingChild接口。因此,请尝试在NestedScrollView中包装您的框架布局。我已将xml添加到OP中。现在进行建议的更改仍然有效。必须让recyclerview在scroll视图中运行我将直接子对象设置为nestedscrollview,它立即崩溃。这些问题并不是固定的,因为我需要让它在我的用例中工作,但问题已经确定,解决方案也很清楚。请把你的答案改一下,我会核对的。谢谢你的帮助!
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.my.package.view.ProgressiveFrameLayout
android:id="@+id/progressiveLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/divider_white"
app:contentLayoutId="@id/contentLayout">
<FrameLayout
android:id="@+id/contentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/sectionRecyclerView"
android:layout_width="match_parent"
android:descendantFocusability="blocksDescendants"
android:layout_height="match_parent"
android:background="@android:color/white" />
</android.support.v4.widget.SwipeRefreshLayout>
<FrameLayout
android:id="@+id/adContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal" />
</FrameLayout>
</com.nbc.news.view.ProgressiveFrameLayout>
</layout>
public boolean canChildScrollUp() {
if (mChildScrollUpCallback != null) {
return mChildScrollUpCallback.canChildScrollUp(this, mTarget);
}
if (android.os.Build.VERSION.SDK_INT < 14) {
if (mTarget instanceof AbsListView) {
final AbsListView absListView = (AbsListView) mTarget;
return absListView.getChildCount() > 0
&& (absListView.getFirstVisiblePosition() > 0 || absListView.getChildAt(0)
.getTop() < absListView.getPaddingTop());
} else {
return ViewCompat.canScrollVertically(mTarget, -1) || mTarget.getScrollY() > 0;
}
} else {
return ViewCompat.canScrollVertically(mTarget, -1);
}
}