本案例中需要用的控件ViewPager和BottomNavigationView ViewPager:主要是页面的切换Fragment:碎片(也就是每个页面的内容)BottomNavigationView:底部导航栏 非常简单,主要就
本案例中需要用的控件ViewPager和BottomNavigationView
非常简单,主要就是一个Viewpager和BottomNavigationView
先来说一下思路:BottomNavigationView底部导航栏 ViewPager+Fragment页面
那么我么如何设置两个控件联动(仅需要两个方法即可)
activity_main.xml
注意:BottomNavigationView需要一个app:menu菜单文件(下面会作讲解)
bottom_nav_menu.xml
主要就是两个属性 icon图标 title文字
接下来创建空白的Fragment,不用做任何修改
创建过程如下:
创建完成后会生成Fragment的java文件和对应的XML文件
Fragment文件不需要修改,后期只需要传入两个参数即可
创建5个即可,创建方法相同,以下只示例一个
示例:
HomeFragment.java
package com.example.myapplication;import Android.os.Bundle;import androidx.annotation.NonNull;import androidx.annotation.Nullable;import androidx.fragment.app.Fragment;import androidx.viewpager.widget.ViewPager;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import com.Google.android.material.tabs.TabLayout;import java.util.ArrayList;import java.util.List;public class HomeFragment extends Fragment { private static final String ARG_PARAM1 = "param1"; private static final String ARG_PARAM2 = "param2"; private String mParam1; private String mParam2; public HomeFragment() { } public static HomeFragment newInstance(String param1, String param2) { HomeFragment fragment = new HomeFragment(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_home, container, false); } @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); }}
fragment_home.xml
MainActivity.java
package com.example.myapplication;import android.os.Bundle;import android.view.MenuItem;import androidx.annotation.NonNull;import androidx.appcompat.app.AppCompatActivity;import androidx.fragment.app.Fragment;import androidx.viewpager.widget.ViewPager;import com.google.android.material.bottomnavigation.BottomNavigationView;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity { List fragmentList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ViewPager viewPager = findViewById(R.id.main_vp); BottomNavigationView bottomNavigationView = findViewById(R.id.main_bnv); initData(); MainActivityAdapter adapter = new MainActivityAdapter(getSupportFragmentManager(), fragmentList); viewPager.setAdapter(adapter);// 页面更改监听 viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { switch (position) { case 0: bottomNavigationView.setSelectedItemId(R.id.menu_home); break; case 1: bottomNavigationView.setSelectedItemId(R.id.menu_service); break; case 2: bottomNavigationView.setSelectedItemId(R.id.menu_rich); break; case 3: bottomNavigationView.setSelectedItemId(R.id.menu_news); break; case 4: bottomNavigationView.setSelectedItemId(R.id.menu_mine); break; } } @Override public void onPageScrollStateChanged(int state) { } });// 图标选择监听 bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { switch (item.getItemId()){ case R.id.menu_home: viewPager.setCurrentItem(0); break; case R.id.menu_service: viewPager.setCurrentItem(1); break; case R.id.menu_rich: viewPager.setCurrentItem(2); break; case R.id.menu_news: viewPager.setCurrentItem(3); break; case R.id.menu_mine: viewPager.setCurrentItem(4); break; } return true; } }); } private void initData() { HomeFragment homeFragment = HomeFragment.newInstance("首页", ""); fragmentList.add(homeFragment); ServiceFragment serviceFragment = ServiceFragment.newInstance("全部服务", ""); fragmentList.add(serviceFragment); RichFragment richFragment = RichFragment.newInstance("精准扶贫", ""); fragmentList.add(richFragment); NewsFragment newsFragment = NewsFragment.newInstance("新闻", ""); fragmentList.add(newsFragment); MineFragment mineFragment = MineFragment.newInstance("个人中心", ""); fragmentList.add(mineFragment); }}
接下来是Viewpager的适配器
MainActivityAdapter.java
package com.example.myapplication;import androidx.annotation.NonNull;import androidx.fragment.app.Fragment;import androidx.fragment.app.FragmentManager;import androidx.fragment.app.FragmentStatePagerAdapter;import java.util.ArrayList;import java.util.List;public class MainActivityAdapter extends FragmentStatePagerAdapter { List fragmentList=new ArrayList<>(); public MainActivityAdapter(@NonNull FragmentManager fm, List fragmentList) { super(fm); this.fragmentList = fragmentList; } @NonNull @Override public Fragment getItem(int position) { return fragmentList.get(position); } @Override public int getCount() { return fragmentList.size(); }}
来源地址:https://blog.csdn.net/weixin_63987141/article/details/129734170
--结束END--
本文标题: android : 底部导航栏的实现(使用ViewPager和BottomNavigationView)
本文链接: https://lsjlt.com/news/387773.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0