如果片段包含AppBarLayout,则BottomNavigationView不会在片段内部滚动时隐藏 [英] BottomNavigationView does not hide on scrolling inside fragment, if fragment contains AppBarLayout
问题描述
我在Android中使用coordinatorLayout在片段内滚动和检测时遇到很多问题。我有一个包含3个片段的 MainActivity。在这些片段之一中,我有一个Appbar,当滚动片段时它会折叠。我已经做到了,但是如果我设置了滚动行为以允许这样做,我的bottomNavigationView(在mainactivity.xml中找到)不会对滚动做出反应。代码如下所示:
I'm having a lot of problems with scrolling and detection of it inside fragments using coordinatorLayout in Android. I have a "MainActivity" that contains 3 fragments. In one of those fragments I have an Appbar that collapses when fragment is scrolled. I've managed to do that, but if I set scrolling behavior to allow that, my bottomNavigationView (which is found in mainactivity.xml) does NOT react to scrolling. Codes go something like this:
Fragment1.xml
<android.support.design.widget.CoordinatorLayout
...
...>
<android.support.design.widget.AppBarLayout
...
...>
<android.support.design.widget.CollapsingToolbarLayout
app:layout_scrollFlags="scroll|exitUntilCollapsed"
...>
<android.support.v7.widget.Toolbar
.../>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
MainActivity.xml
<android.support.design.widget.CoordinatorLayout
.../>
<FrameLayout
app:layout_behavior="@string/appbar_scrolling_view_behavior"
.../>
<android.support.design.widget.BottomNavigationView
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
.../>
</android.support.design.widget.CoordinatorLayout>
现在,在我向内滚动片段时,我的AppBar折叠成标题(这就是我想要的),但是我的bottomNavigationView对滚动没有反应。
Now, this works well enough, in the sense that when I scroll inside fragment my AppBar collapses into the title (which is what I want), BUT my bottomNavigationView does not react to scrolling.
我发现,如果我添加该行
What I found out is that if i add the line
app:layout_behavior="@string/appbar_scrolling_view_behavior"
到AppBarLayout xml声明中,我得到bottomView在滚动事件时折叠(当我向上滚动时,它再次显示)。因此,基本上我可以折叠片段内的appbar,或者当我检测到片段内的滚动事件时可以隐藏BottomNavigationView。
to the AppBarLayout xml declaration I get the bottomView to collapse on scroll event (when I scroll up it shows again). So basically either I have the ability to collapse appbar inside fragment OR I have the ability to hide BottomNavigationView when I detect a scroll event inside the fragment.
任何一种帮助都会
推荐答案
为了隐藏底部导航以响应在内部滚动
修改<$ c $ fragment1.kt 上的c> onCreateView 方法如下
In order to hide bottom navigation in reaction to scrolling inside the fragement
modify onCreateView
method on fragment1.kt as follows
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
...
// access the bottom nav which is on the main activity
val bottomNav: BottomNavigationView = activity!!.findViewById(R.id.bottom_nav)
// hide bottom nav when scrolling
rvList.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
if (dy > 0 || dy < 0) {
bottomNav.visibility = View.GONE
}
}
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
bottomNav.visibility = View.VISIBLE
}
super.onScrollStateChanged(recyclerView, newState)
}
})
return view
}
在Java中
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
// access the bottom nav which is on the main activity
BottomNavigationView bottomNav = getActivity.findViewById(R.id.bottom_nav);
// hide bottom nav when scrolling
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener()
{
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy)
{
if (dy > 0 ||dy<0)
{
bottomNav.setVisibility(View.GONE);
}
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState)
{
if (newState == RecyclerView.SCROLL_STATE_IDLE)
{
bottomNav.setVisibility(View.VISIBLE);
}
super.onScrollStateChanged(recyclerView, newState);
}
});
return view;
}
更新:
将侦听器代码提取到下面给出的单独函数中(在Kotlin中)
Extract the listener code to a separate function as given below (in Kotlin)
public fun hideBottomNav(
rvList: RecyclerView,
bottomNav: BottomNavigationView
) {
rvList.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
if (dy > 0 || dy < 0) {
bottomNav.visibility = View.GONE
}
}
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
bottomNav.visibility = View.VISIBLE
}
super.onScrollStateChanged(recyclerView, newState)
}
})
}
这篇关于如果片段包含AppBarLayout,则BottomNavigationView不会在片段内部滚动时隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!