gpt4 book ai didi

android - 如何创建类似instagram的底部导航栏

转载 作者:行者123 更新时间:2023-12-02 12:28:53 25 4
gpt4 key购买 nike

我是一名 Android 新手,试图为我的新 Android 应用程序创建一个类似于 Instagram 中的底部导航栏。像这样instagram.jpg单击搜索图标会在操作栏中添加搜索栏。我正在构建一个应用程序,用于提醒用户他的医疗预约,该应用程序底部有三个导航选项卡。我已经创建了这个 Screenshot_2017-06-04-11-59-56.png在此之后我被困住了。我是否应该使用三个 Activity 来显示相应选项卡或 fragment 的内容以及如何实现这一点。

我需要一个recyclerview来显示约会。如何仅当单击底部的搜索图标时才显示搜索栏。 为了实现这一目标进行了很多搜索,但找不到任何有用的东西。

任何对实现相同效果的代码或库的建议都会有很大的帮助,谢谢。

最佳答案

Should I use three activities to display the content of corresponding tabs or fragments and how can I achieve that?

你绝对应该使用Fragment对于每个底部导航 Item / Tab 。喜欢FragmentHome , FragmentSearchFragmentSettings .

更改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

I need a recyclerview to display appointments

在您的 Fragment's 中布局XML ,添加RecyclerView显示约会列表。在你的Fragment类,初始化RecyclerView并创建一个 ArrayList<Appointment>并通过这个list给您Adapter显示于RecyclerView行项目。

这里有一个关于:How to use RecyclerView in Fragment的教程

How can I display the search bar only when the search icon at bottom is clicked?

您可以通过编程方式显示/隐藏 ToolBar/ActionBar 中的选项项根据您的 fragment 更改。

在您的 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);
}

以下是一些有用的链接:

  1. Android Toolbar Adding Menu Items for different fragments
  2. Hide/Show Action Bar Option Menu Item for different fragments
  3. Adding ActionBar Items From Within Your Fragments

希望这有助于理解该场景。

关于android - 如何创建类似instagram的底部导航栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44351487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com