底部导航视图在片段上。

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

BottomNavigationView on fragment

问题

我使用了底部导航视图(Bottom Navigation View),在一个片段(Fragment)上它的工作不正常,问题在于没有过渡效果,只显示了第一个选定的项目。

  1. navigationView.setSelectedItemId(R.id.message_menu_Friend);

我的代码:

  1. public class MessageFragment extends Fragment {
  2. @Override
  3. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  4. Bundle savedInstanceState) {
  5. View rootView = inflater.inflate(R.layout.fragment_message, container, false);
  6. BottomNavigationView navigationView = rootView.findViewById(R.id.top_navigation_bar);
  7. navigationView.setOnNavigationItemSelectedListener(topNav);
  8. navigationView.setSelectedItemId(R.id.message_menu_Friend);
  9. getChildFragmentManager().beginTransaction().replace(R.id.hostFragment_M, new FriendFragment()).commit();
  10. return rootView;
  11. }
  12. private BottomNavigationView.OnNavigationItemSelectedListener topNav = new BottomNavigationView.OnNavigationItemSelectedListener() {
  13. @Override
  14. public boolean onNavigationItemSelected(@NonNull MenuItem item) {
  15. Fragment selectedItem = null;
  16. switch (item.getItemId()) {
  17. case R.id.message_menu_message:
  18. selectedItem = new MessagesFragment();
  19. break;
  20. case R.id.message_menu_Friend:
  21. selectedItem = new FriendFragment();
  22. break;
  23. }
  24. assert selectedItem != null;
  25. getChildFragmentManager().beginTransaction().replace(R.id.hostFragment_M, selectedItem).commit();
  26. return true;
  27. }
  28. };
  29. }

XML文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. xmlns:app="http://schemas.android.com/apk/res-auto"
  7. tools:context=".MessageFragment">
  8. <androidx.constraintlayout.widget.ConstraintLayout
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent">
  11. <FrameLayout
  12. android:layout_marginTop="112dp"
  13. android:layout_width="match_parent"
  14. android:layout_height="match_parent"
  15. android:id="@+id/hostFragment_M">
  16. </FrameLayout>
  17. <EditText
  18. android:id="@+id/editTextTextPersonSearch"
  19. android:layout_width="match_parent"
  20. android:layout_height="56dp"
  21. android:background="@color/White"
  22. android:backgroundTint="@color/White"
  23. android:drawableEnd="@drawable/ic_baseline_search_24"
  24. android:drawablePadding="8dp"
  25. android:ems="10"
  26. android:hint="@string/search"
  27. android:inputType="textPersonName"
  28. android:paddingStart="15dp"
  29. android:paddingEnd="15dp"
  30. app:layout_constraintEnd_toEndOf="parent"
  31. app:layout_constraintStart_toStartOf="parent"
  32. app:layout_constraintTop_toTopOf="parent" />
  33. <com.google.android.material.bottomnavigation.BottomNavigationView
  34. android:id="@+id/top_navigation_bar"
  35. android:layout_width="match_parent"
  36. android:layout_height="wrap_content"
  37. android:background="@color/White"
  38. app:layout_constraintEnd_toEndOf="parent"
  39. app:layout_constraintStart_toStartOf="parent"
  40. app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonSearch"
  41. app:menu="@menu/message_menu" />
  42. </androidx.constraintlayout.widget.ConstraintLayout>
  43. </FrameLayout>

当我点击底部导航时,如何使片段显示出来?

英文:

i use Bottom Navigation View and on a fragment its not work fine the problem is there is no transition its only display the first selected

  1. navigationView.setSelectedItemId(R.id.message_menu_Friend);

my code

public class MessageFragment extends Fragment {

  1. @Override
  2. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  3. Bundle savedInstanceState) {
  4. View rootView =inflater.inflate(R.layout.fragment_message, container, false);
  5. BottomNavigationView navigationView = rootView.findViewById(R.id.top_navigation_bar);
  6. navigationView.setOnNavigationItemSelectedListener(topNav);
  7. navigationView.setSelectedItemId(R.id.message_menu_Friend);
  8. getChildFragmentManager().beginTransaction().replace(R.id.hostFragment_M,new FriendFragment()).commit();
  9. // Inflate the layout for this fragment
  10. return inflater.inflate(R.layout.fragment_message, container, false);
  11. }
  12. private BottomNavigationView.OnNavigationItemSelectedListener topNav = new BottomNavigationView.OnNavigationItemSelectedListener() {
  13. @Override
  14. public boolean onNavigationItemSelected(@NonNull MenuItem item) {
  15. Fragment selectedItem = null;
  16. switch (item.getItemId()) {
  17. case R.id.message_menu_message:
  18. selectedItem = new MessagesFragment();
  19. break;
  20. case R.id.message_menu_Friend:
  21. selectedItem = new FriendFragment();
  22. break;
  23. }
  24. assert selectedItem != null;
  25. getChildFragmentManager().beginTransaction().replace(R.id.hostFragment_M,selectedItem).commit();
  26. return true ;
  27. }
  28. };}

Xml file

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;FrameLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  4. android:layout_width=&quot;match_parent&quot;
  5. android:layout_height=&quot;match_parent&quot;
  6. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  7. tools:context=&quot;.MessageFragment&quot;&gt;
  8. &lt;androidx.constraintlayout.widget.ConstraintLayout
  9. android:layout_width=&quot;match_parent&quot;
  10. android:layout_height=&quot;match_parent&quot;&gt;
  11. &lt;FrameLayout android:layout_marginTop=&quot;112dp&quot;
  12. android:layout_width=&quot;match_parent&quot;
  13. android:layout_height=&quot;match_parent&quot;
  14. android:id=&quot;@+id/hostFragment_M&quot;&gt;
  15. &lt;/FrameLayout&gt;
  16. &lt;EditText
  17. android:id=&quot;@+id/editTextTextPersonSearch&quot;
  18. android:layout_width=&quot;match_parent&quot;
  19. android:layout_height=&quot;56dp&quot;
  20. android:background=&quot;@color/White&quot;
  21. android:backgroundTint=&quot;@color/White&quot;
  22. android:drawableEnd=&quot;@drawable/ic_baseline_search_24&quot;
  23. android:drawablePadding=&quot;8dp&quot;
  24. android:ems=&quot;10&quot;
  25. android:hint=&quot;@string/search&quot;
  26. android:inputType=&quot;textPersonName&quot;
  27. android:paddingStart=&quot;15dp&quot;
  28. android:paddingEnd=&quot;15dp&quot;
  29. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  30. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  31. app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
  32. &lt;com.google.android.material.bottomnavigation.BottomNavigationView
  33. android:id=&quot;@+id/top_navigation_bar&quot;
  34. android:layout_width=&quot;match_parent&quot;
  35. android:layout_height=&quot;wrap_content&quot;
  36. android:background=&quot;@color/White&quot;
  37. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  38. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  39. app:layout_constraintTop_toBottomOf=&quot;@+id/editTextTextPersonSearch&quot;
  40. app:menu=&quot;@menu/message_menu&quot;
  41. /&gt;
  42. &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  43. &lt;/FrameLayout&gt;

how can i make it display the fragment when i click on the bottom navigation

答案1

得分: 1

删除这行代码:navigationView.setSelectedItemId(R.id.message_menu_Friend);,并且将getChildFragmentManager()改为getSupportFragmentManager()。另外,在onCreate方法中,只需返回rootView,而不是重新进行布局填充。

英文:

Remove the line navigationView.setSelectedItemId(R.id.message_menu_Friend);, and also change getChildFragmentManager() to getSupportFragmentManager(). Also, in the onCreate method, just return rootView instead of inflating again.

huangapple
  • 本文由 发表于 2020年8月28日 22:45:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63636031.html
匿名

发表评论

匿名网友

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

确定