我应该使用三个activities
显示的内容相应的 tabs
或fragments
,我该如何做到这一点?
当然你应该使用Fragment
为每个底部导航Item/Tab
。像FragmentHome
,FragmentSearch
和FragmentSettings
。
要改变Fragment
,添加NavigationItemSelectedListener
您BottomNavigationView
和更改Fragment
按MenuItem
选择:
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
显示约会
在您的布局XML
中,添加RecyclerView
以显示约会列表。在您的Fragment
课程中,初始化RecyclerView
并创建ArrayList<Appointment>
并将此list
传递给您的Adapter
以显示RecyclerView
行项目。
这里是一个教程的:How to use RecyclerView in Fragment
我怎样才能显示search bar
点击在底部 的search
图标只有当?
您可以按照您的片段更改以编程方式从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
希望这将有助于了解情况。
来源
2017-06-04 07:52:58
FAT
看来你并不真正了解Android开发中的很多基本概念。我建议你按照Android开发的适当教程学习基础知识,然后自己尝试。那时你应该知道如何使用片段,标签和动态显示/隐藏搜索栏。 – paradite