显示/隐藏特定片段的BottomNavigationView。

huangapple go评论93阅读模式
英文:

Show/Hide BottomNavigationView for specific fragment

问题

在我的 Android 应用程序中,我希望在用户聚焦到 SearchView(这也会弹出软键盘)时底部菜单栏消失。当 SearchView 失去焦点时,我希望再次显示底部导航栏。

我尝试过使用 setVisibility() 方法,视图确实会隐藏或显示,但由于某种原因它总是保留其高度。以下是我的 BottomNavigationView 的代码:

  1. <com.google.android.material.bottomnavigation.BottomNavigationView
  2. android:id="@+id/bottom_navigation"
  3. android:layout_width="match_parent"
  4. android:layout_height="60dp"
  5. app:menu="@menu/bottom_navigation_menu"
  6. app:elevation="80dp"
  7. app:labelVisibilityMode="labeled"
  8. app:itemTextColor="@color/bottom_nav_color"
  9. app:itemIconTint="@color/bottom_nav_color"
  10. android:background="?attr/backgroundColor">

处理导航栏隐藏/显示的代码如下:

  1. // 在按下返回键时需要关闭 SearchView(而不仅仅是失去焦点)
  2. mSearchView.setOnQueryTextFocusChangeListener(
  3. (v, hasFocus) -> {
  4. if (!hasFocus) {
  5. adapter.isSearchMode = false;
  6. bottomNavigationView.setVisibility(View.VISIBLE);
  7. searchMenuItem.collapseActionView();
  8. adapter.notifyDataSetChanged();
  9. } else {
  10. adapter.isSearchMode = true;
  11. bottomNavigationView.setVisibility(View.GONE);
  12. searchMenuItem.collapseActionView();
  13. adapter.notifyDataSetChanged();
  14. }
  15. });

BottomNavigationView 由一个 LinearLayout 包含,如下所示:

  1. <LinearLayout
  2. android:id="@+id/footer"
  3. android:baselineAligned="false"
  4. android:layout_width="match_parent"
  5. android:layout_height="60dp"
  6. android:layout_alignParentBottom="true"
  7. android:orientation="vertical">
  8. <View
  9. android:layout_width="match_parent"
  10. android:layout_height="3dp"
  11. android:background="@drawable/bottom_bar_top_shadow"/>
  12. <com.google.android.material.bottomnavigation.BottomNavigationView
  13. android:id="@+id/bottom_navigation"
  14. android:layout_width="match_parent"
  15. android:layout_height="60dp"
  16. app:menu="@menu/bottom_navigation_menu"
  17. app:elevation="80dp"
  18. app:labelVisibilityMode="labeled"
  19. app:itemTextColor="@color/bottom_nav_color"
  20. app:itemIconTint="@color/bottom_nav_color"
  21. android:background="?attr/backgroundColor">
  22. <LinearLayout
  23. android:layout_width="match_parent"
  24. android:layout_height="50dp"
  25. android:orientation="horizontal"
  26. android:layout_marginTop="6dp"
  27. android:weightSum="5"
  28. android:elevation="16dp">
  29. <RelativeLayout
  30. android:layout_width="0dp"
  31. android:layout_height="match_parent"
  32. android:layout_weight="1"
  33. android:gravity="center|top">
  34. <TextView
  35. android:id="@+id/missed_calls"
  36. style="@style/unread_count_font"
  37. android:layout_width="wrap_content"
  38. android:layout_height="wrap_content"
  39. android:background="@drawable/unread_message_count_bg"
  40. android:layout_marginStart="20dp"
  41. android:gravity="center"
  42. android:visibility="gone"/>
  43. </RelativeLayout>
  44. <RelativeLayout
  45. android:layout_width="0dp"
  46. android:layout_height="match_parent"
  47. android:layout_weight="1"
  48. android:gravity="center|top">
  49. <TextView
  50. android:id="@+id/missed_chats"
  51. style="@style/unread_count_font"
  52. android:layout_width="wrap_content"
  53. android:layout_height="wrap_content"
  54. android:background="@drawable/unread_message_count_bg"
  55. android:layout_marginStart="20dp"
  56. android:gravity="center"
  57. android:visibility="gone"/>
  58. </RelativeLayout>
  59. </LinearLayout>
  60. </com.google.android.material.bottomnavigation.BottomNavigationView>
  61. </LinearLayout>
英文:

In my android application, I want the bottom menu bar to disappear when the user focuses the SearchView (this also pops the soft keyboard up). When the SearchView loses focus, I want to show the bottom navigation bar again.

I have tried using setVisibility() and the view does hide or show, but it always retains its height for some reason.Below is the code for my BottomNavigationView:

  1. <com.google.android.material.bottomnavigation.BottomNavigationView
  2. android:id="@+id/bottom_navigation"
  3. android:layout_width="match_parent"
  4. android:layout_height="60dp"
  5. app:menu="@menu/bottom_navigation_menu"
  6. app:elevation="80dp"
  7. app:labelVisibilityMode="labeled"
  8. app:itemTextColor="@color/bottom_nav_color"
  9. app:itemIconTint="@color/bottom_nav_color"
  10. android:background="?attr/backgroundColor">

Code that handles hiding/showing of the navigation bar:

  1. // Needed to close the SearchView when pressing back (instead of just losing focus)
  2. mSearchView.setOnQueryTextFocusChangeListener(
  3. (v, hasFocus) -> {
  4. if (!hasFocus) {
  5. adapter.isSearchMode = false;
  6. bottomNavigationView.setVisibility(View.VISIBLE);
  7. searchMenuItem.collapseActionView();
  8. adapter.notifyDataSetChanged();
  9. } else {
  10. adapter.isSearchMode = true;
  11. bottomNavigationView.setVisibility(View.GONE);
  12. searchMenuItem.collapseActionView();
  13. adapter.notifyDataSetChanged();
  14. }
  15. });

The BottomNavigationView is held by a LinearLayout like so:

  1. <LinearLayout
  2. android:id="@+id/footer"
  3. android:baselineAligned="false"
  4. android:layout_width="match_parent"
  5. android:layout_height="60dp"
  6. android:layout_alignParentBottom="true"
  7. android:orientation="vertical">
  8. <View
  9. android:layout_width="match_parent"
  10. android:layout_height="3dp"
  11. android:background="@drawable/bottom_bar_top_shadow"/>
  12. <com.google.android.material.bottomnavigation.BottomNavigationView
  13. android:id="@+id/bottom_navigation"
  14. android:layout_width="match_parent"
  15. android:layout_height="60dp"
  16. app:menu="@menu/bottom_navigation_menu"
  17. app:elevation="80dp"
  18. app:labelVisibilityMode="labeled"
  19. app:itemTextColor="@color/bottom_nav_color"
  20. app:itemIconTint="@color/bottom_nav_color"
  21. android:background="?attr/backgroundColor">
  22. <LinearLayout
  23. android:layout_width="match_parent"
  24. android:layout_height="50dp"
  25. android:orientation="horizontal"
  26. android:layout_marginTop="6dp"
  27. android:weightSum="5"
  28. android:elevation="16dp">
  29. <RelativeLayout
  30. android:layout_width="0dp"
  31. android:layout_height="match_parent"
  32. android:layout_weight="1"
  33. android:gravity="center|top">
  34. <TextView
  35. android:id="@+id/missed_calls"
  36. style="@style/unread_count_font"
  37. android:layout_width="wrap_content"
  38. android:layout_height="wrap_content"
  39. android:background="@drawable/unread_message_count_bg"
  40. android:layout_marginStart="20dp"
  41. android:gravity="center"
  42. android:visibility="gone"/>
  43. </RelativeLayout>
  44. <RelativeLayout
  45. android:layout_width="0dp"
  46. android:layout_height="match_parent"
  47. android:layout_weight="1"
  48. android:gravity="center|top">
  49. <TextView
  50. android:id="@+id/missed_chats"
  51. style="@style/unread_count_font"
  52. android:layout_width="wrap_content"
  53. android:layout_height="wrap_content"
  54. android:background="@drawable/unread_message_count_bg"
  55. android:layout_marginStart="20dp"
  56. android:gravity="center"
  57. android:visibility="gone"/>
  58. </RelativeLayout>
  59. </LinearLayout>
  60. </com.google.android.material.bottomnavigation.BottomNavigationView>
  61. </LinearLayout>

答案1

得分: 0

在您的BottomNavigation中使用以下代码片段:

setVisibility(View.GONE)

View.GONE = 视图不再占用空间。

英文:

On your BottomNavigation use this code snippet:

setVisibility(View.GONE)

View.Gone = No Space lefts for view.

huangapple
  • 本文由 发表于 2020年5月5日 19:01:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/61611570.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定