ScrollView和NestedScrollView实际有什么区别?我用过他们两个.两者都延伸了FrameLayout.我想深入了解他们两人的利弊.如果有人可以请帮助我.谢谢.
xml android android-layout android-scrollview android-nestedscrollview
当我在NestedScrollView中添加RecyclerView时,我得到一个奇怪的滚动行为.
发生的事情是,只要滚动视图的行数多于屏幕中显示的行数,一旦启动活动,NestedScrollView就会从顶部开始偏移(图像1).如果滚动视图中的项目很少,以便可以一次显示它们,则不会发生这种情况(图2).
我使用的是支持库的23.2.0版.
图1:错误 - 从顶部偏移开始
图2:正确 - 回收者视图中的几个项目
我粘贴在我的布局代码下面:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title:"
style="@style/TextAppearance.AppCompat.Caption"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/bodyPadding"
style="@style/TextAppearance.AppCompat.Body1"
android:text="Neque porro quisquam est qui dolorem ipsum"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Subtitle:"
style="@style/TextAppearance.AppCompat.Caption"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/TextAppearance.AppCompat.Body1"
android:padding="@dimen/bodyPadding"
android:text="Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit..."/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:focusable="false" …Run Code Online (Sandbox Code Playgroud) android android-support-library android-recyclerview android-nestedscrollview
我在RecyclerView里面使用NestedScrollView,它的工作原理.但是当我使用RecyclerView内部LinearLayout或其他东西时,它会根据手势以不同的速度滚动.滚动听手势,如果我只滑了一下,那么滚动一点,如果我快速向上滑动,那么它滚动得非常快.现在我的问题是RecyclerView内部NestedScrollView肯定滚动但快速滚动不起作用.但是我快速或慢速滑动,RecyclerView或NestedScrollView只滚动一点点.
如何使滚动视图中的内部NestedScrollView或RecyclerView内部以各种速度滚动?
有没有办法以编程方式滚动到顶部NestedScrollView还通过触发父级的滚动事件?smoothScrollTo(x, y)不会将滚动事件委托给NestedScrollingParent.
它似乎CoordinatorLayout打破了Espresso行为的行为,如scrollTo()或RecyclerViewActions.scrollToPosition().
对于像这样的布局:
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
...
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
...
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
如果我尝试滚动到NestedScrollView使用ViewActions.scrollTo()第一个问题内部的任何视图我发现是我得到了PerformException.这是因为这个动作只支持ScrollView并NestedScrollView不会扩展它.此处解释了此问题的解决方法,基本上我们可以复制代码scrollTo()并更改约束以支持NestedScrollView.这似乎有效,如果NestedScrollView它不在一个CoordinatorLayout但只要你把它放在CoordinatorLayout滚动动作失败.
对于相同的布局,如果我替换了NestedScrollViewa RecyclerView,则滚动也存在问题.
在这种情况下,我正在使用RecyclerViewAction.scrollToPosition(position).不像NestedScrollView,在这里我可以看到一些滚动发生.但是,它看起来像滚动到错误的位置.例如,如果我滚动到最后一个位置,它会显示倒数第二个但不是最后一个.当我移动RecyclerView了的CoordinatorLayout滚动运作,因为它应该.
目前我们无法CoordinatorLayout为因此问题而使用的屏幕编写任何Espresso测试.任何遇到相同问题或知道解决方法的人?
android android-espresso android-recyclerview coordinator-layout android-nestedscrollview
在一个视图分页器中,我有几个片段,其中一个片段使用带有标题和recyclerview的嵌套scrollview:
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.m360.android.fragment.Members.MemberDetailsFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="20dp">
<header/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:paddingTop="0dp" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
Run Code Online (Sandbox Code Playgroud)
标签"header"代表了一个复杂的布局,我不想在这里发布,因为它会大量延伸代码.
当我在标签之间切换时,它会将海峡滚动到回收站视图.标题是隐藏的,我必须向上滚动才能看到它.
关于是什么原因的任何想法?如果可以避免,我不想在我的适配器中使用类型.
我在NestedScrollView中有一个GridView.每当GridView的内容发生变化时,我都使用下面的代码来调整GridView的大小.这工作正常,但是当我从应用程序的片段3滑回到片段2(NestedScrollView所在的位置)时,NestedScrollView滚动到最底部.奇怪的是,当从片段1滑动到片段2时不会发生这种情况.在调整GridView大小后,它也不会直接发生.
如何通过滚动到底部来压制NestedScrollView?
private static void resizeGridView(GridView gridView, int items, int columns) {
ViewGroup.LayoutParams params = gridView.getLayoutParams();
params.height = singleGridHeight * items;
gridView.setLayoutParams(params);
gridView.requestLayout();
}
Run Code Online (Sandbox Code Playgroud)
我已经搜索了很多关于我的查询,但没有一个是有用的.我在一个滚动视图中有一个recyclerview和一些静态数据,它位于root parentlayout中,如下所示.
我已经设定 -
scrollview.scrollto(0,0);
Run Code Online (Sandbox Code Playgroud)
因为每当我打开活动时它会跳转到recyclerview firstitem并且它会跳过recyclerview上方的静态数据.
recyclerview.setNestedScrollingEnabled(false);
recyclerview.setfocusable(false);
Run Code Online (Sandbox Code Playgroud)
对于smoothscroll.
问题是 -
layoutmanager.scrollToPositionWithOffset(pos,0);
Run Code Online (Sandbox Code Playgroud)
它不起作用.我在将适配器设置为recyclerview后设置了aboveline.也尝试使用NestedScrollView但是徒劳无功.
虽然我用过
layoutmanager.scrollToPosition(pos);
Run Code Online (Sandbox Code Playgroud)
对于那些跳过代码的人,我已将match_parent设置为ScrollView,并将fillviewport设置为true.
这是我的布局.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/bottomsheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.inkdrops.khaalijeb.BrandCouponActivity">
<RelativeLayout
android:id="@+id/inclusionviewgroup"
android:layout_width="match_parent"
android:layout_height="match_parent">
<static data/>
<ScrollView
android:id="@+id/scrollviewmain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/one"
android:fillViewport="true"
android:scrollbars="none"
android:layout_above="@+id/donelayout">
<staticdata/>
<TextView
android:id="@+id/dealstext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/card1"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:textSize="16sp"
android:text="Deals & Coupons"
android:textColor="#444444" />
<RelativeLayout
android:id="@+id/recyclerlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/dealstext"
android:layout_marginTop="5dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:background="@color/colorbackground">
<android.support.v7.widget.RecyclerView
android:id="@+id/coupon_rec_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorbackground"
android:visibility="visible"/>
</RelativeLayout>
<statisdata>
</RelativeLayout>
</ScrollView>
include layout="@layout/activity_coupons"
android:visibility="gone"
/> …Run Code Online (Sandbox Code Playgroud) android scrollview android-recyclerview linearlayoutmanager android-nestedscrollview
我有一个在顶部有一些视图的布局,它应该可以与它们下面的EditText一起滚动.
EditText占用了剩余的空间,占用了所需的空间.
这是一个演示它的示例POC布局(此处仅使用了2个EditTexts):
<android.support.v4.widget.NestedScrollView android:id="@+id/nestedScrollView"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent" android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical">
<EditText
android:id="@+id/titleEditText" android:layout_width="match_parent" android:layout_height="wrap_content"
android:ellipsize="end" android:hint="title" android:imeOptions="actionNext|flagNoExtractUi"
android:inputType="text|textAutoCorrect|textCapSentences" android:maxLines="1"
android:nextFocusDown="@id/contentEditText" android:nextFocusForward="@id/contentEditText"
android:scrollHorizontally="true" android:textColor="#2a2f3b" android:textColorHint="#a3a3a3"
android:textSize="21sp"/>
<EditText
android:id="@+id/contentEditText" android:layout_width="match_parent" android:layout_height="match_parent"
android:gravity="top" android:hint="content" android:background="@android:drawable/alert_light_frame"
android:imeOptions="actionDone|flagNoEnterAction|flagNoExtractUi" android:textSize="18sp"
android:inputType="textMultiLine|textAutoCorrect|textCapSentences"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
Run Code Online (Sandbox Code Playgroud)
我已经设置了一个背景框架,可以直观地显示EditText的大小.
我已经找到了很多我写的解决方案,但是没有一个能够很好地处理滚动.
我一直看到的,至少是其中一个问题:
我尝试过这些解决方案:
windowSoftInputMode在清单中尝试了各种值,并尝试isNestedScrollingEnabled在NestedScrollView中进行设置.如何让底部的EditText占用尽可能多的空间,并且仍然可以滚动整个NestedScrollView,而不会出现编辑问题?
编辑:因为原来的应用程序有点复杂,底部有一些视图(在工具栏内部)当你没有专注于底部的EditText时自动隐藏,这就得到了我找不到的答案上班.
此外,我不小心将赏金给予错误的答案,所以这是一个新的赏金,在更复杂的POC上.问题保持不变.NestedScrollView应该保持在同一个地方,而不是在关注底部EditText时滚动.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical">
<View
android:layout_width="0dp" android:layout_height="0dp" android:focusable="true" …Run Code Online (Sandbox Code Playgroud) 我在我的片段中使用NestedScrollView.在我的xml里面的RelativeLayout但它没有覆盖整个屏幕高度.
以下是我的代码 -
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:fitsSystemWindows="true"
android:background="@color/black"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:background="@color/red_error_color"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
Run Code Online (Sandbox Code Playgroud)
在运行代码时,只有黑色背景可见但不是红色,因为我的子视图背景为红色.
提前致谢
android android-scrollview android-relativelayout android-nestedscrollview