如果片段包含 AppBarLayout,BottomNavigationView 不会隐藏在片段内滚动
BottomNavigationView does not hide on scrolling inside fragment, if fragment contains AppBarLayout
我在 Android 中使用 coordinatorLayout 在片段内滚动和检测它时遇到很多问题。我有一个包含 3 个片段的 "MainActivity"。在其中一个片段中,我有一个 Appbar,它会在滚动片段时折叠。我设法做到了这一点,但如果我设置滚动行为以允许这样做,我的 bottomNavigationView(位于 mainactivity.xml 中)不会对滚动做出反应。代码是这样的:
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>
现在,这很好用,因为当我在 fragment 中滚动时,我的 AppBar 折叠到标题中(这是我想要的),但我的 bottomNavigationView 对滚动没有反应。
我发现如果我添加行
app:layout_behavior="@string/appbar_scrolling_view_behavior"
到 AppBarLayout xml 声明我让 bottomView 在滚动事件上折叠(当我向上滚动时它再次显示)。所以基本上,要么我有能力在片段内折叠应用栏,要么我有能力在我检测到片段内的滚动事件时隐藏 BottomNavigationView。
如有任何帮助,我们将不胜感激。
为了隐藏底部导航以响应在片段内滚动
修改fragment1.kt上的onCreateView
方法如下
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 中)
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)
}
})
}
将您的图书馆更新为 28.0.0 or above
或 androidx / material-1.0.0
并像下面那样更改您的 BottomNavigationView
-
<BottomNavigationView
android:id="@+id/navigation"
.
.
.
.
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior" />
希望对您有所帮助。
我在 Android 中使用 coordinatorLayout 在片段内滚动和检测它时遇到很多问题。我有一个包含 3 个片段的 "MainActivity"。在其中一个片段中,我有一个 Appbar,它会在滚动片段时折叠。我设法做到了这一点,但如果我设置滚动行为以允许这样做,我的 bottomNavigationView(位于 mainactivity.xml 中)不会对滚动做出反应。代码是这样的:
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>
现在,这很好用,因为当我在 fragment 中滚动时,我的 AppBar 折叠到标题中(这是我想要的),但我的 bottomNavigationView 对滚动没有反应。
我发现如果我添加行
app:layout_behavior="@string/appbar_scrolling_view_behavior"
到 AppBarLayout xml 声明我让 bottomView 在滚动事件上折叠(当我向上滚动时它再次显示)。所以基本上,要么我有能力在片段内折叠应用栏,要么我有能力在我检测到片段内的滚动事件时隐藏 BottomNavigationView。
如有任何帮助,我们将不胜感激。
为了隐藏底部导航以响应在片段内滚动
修改fragment1.kt上的onCreateView
方法如下
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 中)
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)
}
})
}
将您的图书馆更新为 28.0.0 or above
或 androidx / material-1.0.0
并像下面那样更改您的 BottomNavigationView
-
<BottomNavigationView
android:id="@+id/navigation"
.
.
.
.
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior" />
希望对您有所帮助。