Android Fragment实现顶部、底部导航栏
作者:Mondarzy 时间:2023-03-26 06:27:58
前言
无论是顶部还是底部导航栏,都是大多数APP的标配,网络上的相关实现教程也非常之多。最近回忆起以前写的小项目,发现对这块内容有些遗忘,不妨就再整理一遍代码逻辑,记录下来,方便日后查阅(指复制粘贴)。
实现的方式有很多,本文采用以下方式实现:
底部导航栏:Fragment + BottomNavigationView
顶部导航栏:Fragment + ViewPager2 + TabLayout
底部导航栏
<布局文件>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<FrameLayout
android:id="@+id/main_page_controller"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="50dp"
android:background="#EFEEEE"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/main_navigation_bar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#ffffff"
app:itemIconTint="@color/navigation_bar_color"
app:itemTextColor="@color/navigation_bar_color"
app:labelVisibilityMode="labeled"
app:menu="@menu/navigation_items"/>
</RelativeLayout>
<menu文件>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/home"
android:icon="@mipmap/home"
android:title="首页"/>
<item
android:id="@+id/plan"
android:icon="@mipmap/plan"
android:title="日程"/>
<item
android:id="@+id/game"
android:icon="@mipmap/game"
android:title="娱乐"/>
<item
android:id="@+id/setting"
android:icon="@mipmap/setting"
android:title="设置"/>
</menu>
<MainActivity.java>
public class MainActivity extends AppCompatActivity {
private BottomNavigationView mNavigationView;
private FragmentManager mFragmentManager;
private Fragment[] fragments;
private int lastFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationView = findViewById(R.id.main_navigation_bar);
initFragment();
initListener();
}
private void initFragment() {
HomeFragment mHomeFragment = new HomeFragment();
PlanFragment mPlanFragment = new PlanFragment();
GameFragment mGameFragment = new GameFragment();
SettingFragment mSettingFragment = new SettingFragment();
fragments = new Fragment[]{mHomeFragment, mPlanFragment, mGameFragment, mSettingFragment};
mFragmentManager = getSupportFragmentManager();
//默认显示HomeFragment
mFragmentManager.beginTransaction()
.replace(R.id.main_page_controller, mHomeFragment)
.show(mHomeFragment)
.commit();
}
private void initListener() {
mNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.home:
if (lastFragment != 0) {
MainActivity.this.switchFragment(lastFragment, 0);
lastFragment = 0;
}
return true;
case R.id.plan:
if (lastFragment != 1) {
MainActivity.this.switchFragment(lastFragment, 1);
lastFragment = 1;
}
return true;
case R.id.game:
if (lastFragment != 2) {
MainActivity.this.switchFragment(lastFragment, 2);
lastFragment = 2;
}
return true;
case R.id.setting:
if (lastFragment != 3) {
MainActivity.this.switchFragment(lastFragment, 3);
lastFragment = 3;
}
return true;
}
return false;
}
});
}
private void switchFragment(int lastFragment, int index) {
FragmentTransaction transaction = mFragmentManager.beginTransaction();
transaction.hide(fragments[lastFragment]);
if (!fragments[index].isAdded()){
transaction.add(R.id.main_page_controller,fragments[index]);
}
transaction.show(fragments[index]).commitAllowingStateLoss();
}
}
<实现效果>
顶部导航栏
<布局文件>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<com.google.android.material.tabs.TabLayout
android:id="@+id/home_indicator"
android:layout_width="match_parent"
android:layout_height="30dp"
app:tabGravity="fill" />
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/home_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<ViewPgaer2适配器>
public class HomePagerAdapter extends FragmentStateAdapter {
private List<String> mData = new ArrayList<>();
public HomePagerAdapter(@NonNull FragmentActivity fragmentActivity, List<String> data) {
super(fragmentActivity);
mData = data;
}
@NonNull
@Override
public Fragment createFragment(int position) {
return HomePageFragment.newInstance(mData.get(position));
}
@Override
public int getItemCount() {
return mData.size();
}
}
<HomeFragment.java>
public class HomeFragment extends Fragment {
private TabLayout mTabLayout;
private ViewPager2 mViewPager2;
private List<String> mData = new ArrayList<>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
initData();
initView(rootView);
return rootView;
}
private void initView(View rootView) {
mTabLayout = rootView.findViewById(R.id.home_indicator);
mViewPager2 = rootView.findViewById(R.id.home_pager);
HomePagerAdapter homePagerAdapter = new HomePagerAdapter(getActivity(), mData);
mViewPager2.setAdapter(homePagerAdapter);
new TabLayoutMediator(mTabLayout, mViewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
@Override
public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
tab.setText(mData.get(position));
}
}).attach();
}
private void initData() {
mData.add("页面1");
mData.add("页面2");
mData.add("页面3");
mData.add("页面4");
mData.add("页面5");
}
}
<HomePageFragment.java>
public class HomePageFragment extends Fragment {
private static final String ARG_PARAM1 = "param1";
private String mParam1;
private TextView textView;
public HomePageFragment() {
}
public static HomePageFragment newInstance(String param1) {
HomePageFragment fragment = new HomePageFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home_page, container, false);
textView = rootView.findViewById(R.id.home_page_tv);
textView.setText(mParam1);
return rootView;
}
}
<实现效果>
来源:https://blog.csdn.net/weixin_44139159/article/details/111590002
标签:Android,Fragment,导航栏
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
apollo更改配置刷新@ConfigurationProperties配置类
2022-01-09 22:28:22
![](https://img.aspxhome.com/file/2023/6/62666_0s.png)
浅析Java中的内存泄漏
2023-12-19 02:29:02
![](https://img.aspxhome.com/file/2023/3/70993_0s.jpg)
Java截取字符串的方法
2023-11-07 09:30:02
Android Studio多工程引用同一个library项目配置的解决方法
2022-04-07 16:39:40
![](https://img.aspxhome.com/file/2023/2/138972_0s.png)
c#生成验证码程序
2023-01-08 18:38:59
Android TextWatcher三个回调以及监听EditText的输入案例详解
2022-06-04 10:22:29
![](https://img.aspxhome.com/file/2023/2/138182_0s.png)
Android 文件数据存储实例详解
2023-07-28 17:08:03
Android编程简易实现XML解析的方法详解
2023-12-25 18:51:59
![](https://img.aspxhome.com/file/2023/3/88663_0s.jpg)
关于C# TabPage如何隐藏的问题
2023-01-10 00:01:39
在springboot中如何使用filter设置要排除的URL
2022-11-06 00:06:01
![](https://img.aspxhome.com/file/2023/6/90956_0s.png)
Android界面刷新的方法分享
2022-06-20 15:20:52
Java获取指定字符串出现次数的方法
2022-05-11 16:06:23
AndroidStudio替换项目图标ic_launcher操作
2023-03-20 09:48:07
![](https://img.aspxhome.com/file/2023/1/122061_0s.jpg)
详解java中产生死锁的原因及如何避免
2022-04-22 00:36:14
如何在Android中实现左右滑动的指引效果
2023-06-23 09:08:47
![](https://img.aspxhome.com/file/2023/8/82398_0s.png)
C语言根据协议分割获取字符串单元的实现代码
2023-06-21 08:20:27
![](https://img.aspxhome.com/file/2023/6/87146_0s.png)
C#特性 匿名类型与隐式类型局部变量使用介绍
2023-09-29 12:42:50
SpringBoot2.7 WebSecurityConfigurerAdapter类过期配置
2022-09-23 00:27:32
![](https://img.aspxhome.com/file/2023/8/92708_0s.jpg)
通过Java实现bash命令过程解析
2023-01-07 17:38:25
Volatile关键字的使用案例
2022-01-27 16:37:05
![](https://img.aspxhome.com/file/2023/4/86054_0s.png)