CoordinatorLayout (CL) 中的 NestedScrollView (NSV):加载时 NSV 不在顶部
NestedScrollView (NSV) in CoordinatorLayout (CL): NSV Not at Top When Loaded
我在 CL 中使用 NSV,以便在 NSV 向下滚动时压缩工具栏。我遇到的问题是我的 NSV 在加载时没有滚动到顶部,相反,它从 NSV 的顶部偏移了相当大的距离(我不确定这个间距来自哪里,它不在布局)。
请看一下屏幕截图,第一个显示了 NSV 的加载方式,通过比较第二个(当我将 NSV 滚动到手动置顶):
我对此布局进行了一些更新,导致出现这种情况,之前,它毫无问题地加载到顶部。但是,我没有添加任何应该导致这种情况的间距。
这是我为此使用的布局:
<android.support.design.widget.CoordinatorLayout
android:id="@+id/cl_goal_detail"
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="0dp"
android:layout_weight="1">
<android.support.design.widget.AppBarLayout
android:id="@+id/abl_goal_detail"
android:layout_width="match_parent"
android:layout_height="144dp"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar_goal_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/content_space_double"
app:collapsedTitleTextAppearance="@style/title.dark"
app:expandedTitleTextAppearance="@style/display3.plus.dark"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_goal_detail"
style="@style/toolbar"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="@+id/nsv_goal_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/content_space_half"
android:paddingLeft="@dimen/content_space_half"
android:paddingRight="@dimen/content_space_half"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<FrameLayout
android:id="@+id/container_goal_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_vertical"/>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
如有任何想法,我们将不胜感激!
好的!经过整整一天调试我的布局和 Fragment 的每个组件后,我确定了我认为是错误的地方。
首先,问题:事实证明,NSV 的子视图中的元素在运行时将可见性更改为 View.GONE 导致列表向下滚动。我注意到列表滚动到可见性被切换的元素上方(包括视图上设置的任何边距)。
其次,修复:我通过在 xml 布局中将所有视图设置为具有 android:visibility="gone" 来解决此问题,然后,我根据需要切换每个视图的可见性.以前,默认情况下视图是可见的,然后我从那里开始工作。我只需要改变我的逻辑,从它们全部消失开始,不是很困难。
我认为这是可行的,因为当在 onCreateView() 中创建 NSV 时,您要在运行时隐藏的视图不会构成整体高度计算的一部分。一旦片段前进到 onCreateView() 之后,动态更改视图是安全的,但是,如果视图作为 onCreateView() 中的高度的一部分进行计算,然后用 View.GONE 隐藏,测量结果会变得不稳定,你最终会得到列表显着向下滚动。
您是否尝试过在您的视图组中添加以下行,即在您的情况下是 FrameLayout
android:descendantFocusability="blocksDescendants"
我认为这对您也适用。
如果没有尝试添加 NSV。
您的 post 回答对我找出问题有很大帮助(顺便说一句,是一样的)。但我得到它以不同的方式工作。我猜你正在使用 RecyclerView
。在我的例子中,我使用 3 RecyclerViews
。好吧,根据你的回答,我开始隐藏回收商,我发现只有其中一个导致了这个问题。我所做的是填充 postDelayed:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
recyler.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
recyler.setAdapter(new MyAdapter(myList));
}
}, 3000);
效果很好!
就我而言,在我滚动内容的底部附近有一个 EditText
吸引了焦点。由于 NestedScrollView
做了一些奇怪的布局,当 activity 开始时聚焦视图没有滚动到顶部,所以真正的原因并不明显。将此添加到 NestedScrollView
的子布局为我修复了它:
android:focusableInTouchMode="true"
我在 CL 中使用 NSV,以便在 NSV 向下滚动时压缩工具栏。我遇到的问题是我的 NSV 在加载时没有滚动到顶部,相反,它从 NSV 的顶部偏移了相当大的距离(我不确定这个间距来自哪里,它不在布局)。
请看一下屏幕截图,第一个显示了 NSV 的加载方式,通过比较第二个(当我将 NSV 滚动到手动置顶):
我对此布局进行了一些更新,导致出现这种情况,之前,它毫无问题地加载到顶部。但是,我没有添加任何应该导致这种情况的间距。
这是我为此使用的布局:
<android.support.design.widget.CoordinatorLayout
android:id="@+id/cl_goal_detail"
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="0dp"
android:layout_weight="1">
<android.support.design.widget.AppBarLayout
android:id="@+id/abl_goal_detail"
android:layout_width="match_parent"
android:layout_height="144dp"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar_goal_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/content_space_double"
app:collapsedTitleTextAppearance="@style/title.dark"
app:expandedTitleTextAppearance="@style/display3.plus.dark"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_goal_detail"
style="@style/toolbar"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="@+id/nsv_goal_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/content_space_half"
android:paddingLeft="@dimen/content_space_half"
android:paddingRight="@dimen/content_space_half"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<FrameLayout
android:id="@+id/container_goal_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_vertical"/>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
如有任何想法,我们将不胜感激!
好的!经过整整一天调试我的布局和 Fragment 的每个组件后,我确定了我认为是错误的地方。
首先,问题:事实证明,NSV 的子视图中的元素在运行时将可见性更改为 View.GONE 导致列表向下滚动。我注意到列表滚动到可见性被切换的元素上方(包括视图上设置的任何边距)。
其次,修复:我通过在 xml 布局中将所有视图设置为具有 android:visibility="gone" 来解决此问题,然后,我根据需要切换每个视图的可见性.以前,默认情况下视图是可见的,然后我从那里开始工作。我只需要改变我的逻辑,从它们全部消失开始,不是很困难。
我认为这是可行的,因为当在 onCreateView() 中创建 NSV 时,您要在运行时隐藏的视图不会构成整体高度计算的一部分。一旦片段前进到 onCreateView() 之后,动态更改视图是安全的,但是,如果视图作为 onCreateView() 中的高度的一部分进行计算,然后用 View.GONE 隐藏,测量结果会变得不稳定,你最终会得到列表显着向下滚动。
您是否尝试过在您的视图组中添加以下行,即在您的情况下是 FrameLayout
android:descendantFocusability="blocksDescendants"
我认为这对您也适用。
如果没有尝试添加 NSV。
您的 post 回答对我找出问题有很大帮助(顺便说一句,是一样的)。但我得到它以不同的方式工作。我猜你正在使用 RecyclerView
。在我的例子中,我使用 3 RecyclerViews
。好吧,根据你的回答,我开始隐藏回收商,我发现只有其中一个导致了这个问题。我所做的是填充 postDelayed:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
recyler.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
recyler.setAdapter(new MyAdapter(myList));
}
}, 3000);
效果很好!
就我而言,在我滚动内容的底部附近有一个 EditText
吸引了焦点。由于 NestedScrollView
做了一些奇怪的布局,当 activity 开始时聚焦视图没有滚动到顶部,所以真正的原因并不明显。将此添加到 NestedScrollView
的子布局为我修复了它:
android:focusableInTouchMode="true"