我应该用三个activities来显示对应的内容吗
tabs 或 fragments 我该如何实现?
您绝对应该为每个底部导航Item / Tab 使用Fragment。喜欢FragmentHome、FragmentSearch 和FragmentSettings。
要更改Fragment,请将NavigationItemSelectedListener 添加到您的BottomNavigationView 并根据MenuItem 选择更改Fragment:
BottomNavigationView bottomNavigationView = (BottomNavigationView)
findViewById(R.id.bottom_navigation_view);
bottomNavigationView.setOnNavigationItemSelectedListener
(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.action_item1:
selectedFragment = FragmentHome.newInstance();
break;
case R.id.action_item2:
selectedFragment = FragmentSearch.newInstance();
break;
case R.id.action_item3:
selectedFragment = FragmentSettings.newInstance();
break;
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout, selectedFragment);
transaction.commit();
return true;
}
});
这里有一个关于:BottomNavigationView with multiple Fragments的教程
我需要recyclerview 来显示约会
在您的Fragment's 布局XML 中,添加RecyclerView 以显示约会列表。在您的 Fragment 类中,初始化 RecyclerView 并创建一个 ArrayList<Appointment> 并将此 list 传递给您的 Adapter 以显示在 RecyclerView 行项目上。
这里有一个关于:How to use RecyclerView in Fragment的教程
只有在底部有search 图标时,我才能显示search bar
被点击了吗?
您可以根据片段更改以编程方式从ToolBar/ActionBar 显示/隐藏选项项。
在您的FragmentSearch 中,执行以下更改以显示Searchbar:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.fragmet_search, parent, false);
return v;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.your_search_menu_xml, menu);
super.onCreateOptionsMenu(menu, inflater);
}
这里有一些有用的链接:
-
Android Toolbar Adding Menu Items for different fragments
-
Hide/Show Action Bar Option Menu Item for different fragments
- Adding ActionBar Items From Within Your Fragments
希望这将有助于理解该场景。