工具栏在AppBarLayout是滚动的,虽然RecyclerView有没有足够的内容来滚动 [英] Toolbar in AppBarLayout is scrollable although RecyclerView has not enough content to scroll
问题描述
难道真的意在工具栏的AppBarLayout是滚动的,虽然与appbar_scrolling_view_behavior有没有足够的内容主要容器,真正滚动?
我已经测试至今:
当我使用一个NestedScrollView(用WRAP_CONTENT属性)作为主容器和一个TextView作为孩子,AppBarLayout正常工作,不会滚动。
然而,当我使用的只有少数项目和WRAP_CONTENT属性(这样就不需要滚动)一个RecyclerView,工具栏上的AppBarLayout是滚动的,即使RecyclerView永远不会收到scroll事件(测试有OnScrollChangeListener)。
下面是我的布局code:
< android.support.design.widget.CoordinatorLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:程序=http://schemas.android.com/apk/res-auto
机器人:ID =@ + ID / coordinatorLayout
机器人:layout_width =match_parent
机器人:layout_height =match_parent>
< android.support.design.widget.AppBarLayout
机器人:ID =@ + ID / appBarLayout
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT>
< android.support.v7.widget.Toolbar
机器人:ID =@ + ID /工具栏
机器人:layout_width =match_parent
机器人:layout_height =?ATTR / actionBarSize
机器人:ATTR / colorPrimary后台=
应用程序:layout_scrollFlags =滚动| enterAlways
应用程序:主题=@风格/ ToolbarStyle/>
< /android.support.design.widget.AppBarLayout>
< android.support.v7.widget.RecyclerView
机器人:ID =@ + ID /回收
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
应用程序:layout_behavior =@字符串/ appbar_scrolling_view_behavior/>
< /android.support.design.widget.CoordinatorLayout>
通过下面的效果,该工具栏滚动虽然它不是必需的:
我还发现了一种通过检查所有RecyclerView项目都可见,并使用RecyclerView的setNestedScrollingEnabled()方法来处理这个问题。
然而,它似乎更像是一个错误的意图我。任何意见? :D
编辑:谁的人有可能有兴趣在我目前的解决办法,我只好把setNestedScrollingEnabled()的逻辑与5ms的处理程序的postDelayed()方法延误是由于布局管理,它总是返回-1时调用方法来判断是否是第一个和最后一个项目是可见的。
我在ONSTART()方法,用这个code(后我RecyclerView已初始化)和RecyclerView的内容改变后,每次发生。
最后LinearLayoutManager的layoutManager =(LinearLayoutManager)mRecyclerView.getLayoutManager();
新的处理程序()。postDelayed(新的Runnable(){
@覆盖
公共无效的run(){
//在RecyclerView没有项目
如果(mRecyclerView.getAdapter()。getItemCount()== 0)
mRecyclerView.setNestedScrollingEnabled(假);
//如果第一和最后一个项目是可见
否则,如果(layoutManager.findFirstCompletelyVisibleItemPosition()== 0
&功放;&安培; layoutManager.findLastCompletelyVisibleItemPosition()== mRecyclerView.getAdapter()getItemCount() - 。1)
mRecyclerView.setNestedScrollingEnabled(假);
其他
mRecyclerView.setNestedScrollingEnabled(真正的);
}
},5);
在你的工具栏
删除滚动
标志,只剩下 enterAlways
标志,你应该得到你预期的效果。为了完整起见,你的布局应该是这样的:
< android.support.design.widget.CoordinatorLayout
的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:程序=http://schemas.android.com/apk/res-auto
机器人:ID =@ + ID / coordinatorLayout
机器人:layout_width =match_parent
机器人:layout_height =match_parent>
< android.support.design.widget.AppBarLayout
机器人:ID =@ + ID / appBarLayout
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT>
< android.support.v7.widget.Toolbar
机器人:ID =@ + ID /工具栏
机器人:layout_width =match_parent
机器人:layout_height =?ATTR / actionBarSize
机器人:ATTR / colorPrimary后台=
应用程序:layout_scrollFlags =enterAlways
应用程序:主题=@风格/ ToolbarStyle/>
< /android.support.design.widget.AppBarLayout>
< android.support.v7.widget.RecyclerView
机器人:ID =@ + ID /回收
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
应用程序:layout_behavior =@字符串/ appbar_scrolling_view_behavior/>
< /android.support.design.widget.CoordinatorLayout>
Is it really intended that the Toolbar in a AppBarLayout is scrollable although the main container with the "appbar_scrolling_view_behavior" has not enough content to really scroll?
What I have tested so far:
When I use a NestedScrollView (with "wrap_content" attribute) as main container and a TextView as child, the AppBarLayout works properly and does not scroll.
However, when I use a RecyclerView with only a few entries and the "wrap_content" attribute (so that there is no need to scroll), the Toolbar in the AppBarLayout is scrollable even though the RecyclerView never receives a scroll event (tested with a OnScrollChangeListener).
Here's my layout code:
<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/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<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:theme="@style/ToolbarStyle" />
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
With the following effect that the toolbar is scrollable although it's not necessary:
I've also found a way to deal with this by checking if all RecyclerView items are visible and using the setNestedScrollingEnabled() method of the RecyclerView.
Nevertheless, it does seem more like a bug as intended to me. Any opinions? :D
EDIT: For people who are might be interested in my current solution, I had to put the setNestedScrollingEnabled() logic in the postDelayed() method of a Handler with 5 ms delay due to the LayoutManager which always returned -1 when calling the methods to find out whether the first and the last item is visible.
I use this code in the onStart() method (after my RecyclerView has been initialized) and every time after a content change of the RecyclerView occurs.
final LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//no items in the RecyclerView
if (mRecyclerView.getAdapter().getItemCount() == 0)
mRecyclerView.setNestedScrollingEnabled(false);
//if the first and the last item is visible
else if (layoutManager.findFirstCompletelyVisibleItemPosition() == 0
&& layoutManager.findLastCompletelyVisibleItemPosition() == mRecyclerView.getAdapter().getItemCount() - 1)
mRecyclerView.setNestedScrollingEnabled(false);
else
mRecyclerView.setNestedScrollingEnabled(true);
}
}, 5);
In your Toolbar
remove the scroll
flag, leaving only the enterAlways
flag and you should get the effect you intended. For completeness, your layout should look like:
<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/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<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="enterAlways"
app:theme="@style/ToolbarStyle" />
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
这篇关于工具栏在AppBarLayout是滚动的,虽然RecyclerView有没有足够的内容来滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!