Android?Fragment實(shí)現(xiàn)頂部、底部導(dǎo)航欄
前言
無(wú)論是頂部還是底部導(dǎo)航欄,都是大多數(shù)APP的標(biāo)配,網(wǎng)絡(luò)上的相關(guān)實(shí)現(xiàn)教程也非常之多。最近回憶起以前寫的小項(xiàng)目,發(fā)現(xiàn)對(duì)這塊內(nèi)容有些遺忘,不妨就再整理一遍代碼邏輯,記錄下來(lái),方便日后查閱(指復(fù)制粘貼)。
實(shí)現(xiàn)的方式有很多,本文采用以下方式實(shí)現(xiàn):
- 底部導(dǎo)航欄:Fragment + BottomNavigationView
- 頂部導(dǎo)航欄:Fragment + ViewPager2 + TabLayout
底部導(dǎo)航欄
<布局文件>
<?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="首頁(yè)"/> ? ? <item ? ? ? ? android:id="@+id/plan" ? ? ? ? android:icon="@mipmap/plan" ? ? ? ? android:title="日程"/> ? ? <item ? ? ? ? android:id="@+id/game" ? ? ? ? android:icon="@mipmap/game" ? ? ? ? android:title="娛樂(lè)"/> ? ? <item ? ? ? ? android:id="@+id/setting" ? ? ? ? android:icon="@mipmap/setting" ? ? ? ? android:title="設(shè)置"/> </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(); ? ? ? ? //默認(rèn)顯示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(); ? ? } }
<實(shí)現(xiàn)效果>
頂部導(dǎo)航欄
<布局文件>
<?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("頁(yè)面1"); ? ? ? ? mData.add("頁(yè)面2"); ? ? ? ? mData.add("頁(yè)面3"); ? ? ? ? mData.add("頁(yè)面4"); ? ? ? ? mData.add("頁(yè)面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; ? ? } }
<實(shí)現(xiàn)效果>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android自定義View實(shí)現(xiàn)球形動(dòng)態(tài)加速球
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)球形動(dòng)態(tài)加速球,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06Android TextView實(shí)現(xiàn)跑馬燈效果的方法
這篇文章主要介紹了Android TextView跑馬燈效果實(shí)現(xiàn)方法,涉及Android布局文件中相關(guān)屬性的設(shè)置技巧,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-01-01Android動(dòng)畫 實(shí)現(xiàn)開關(guān)按鈕動(dòng)畫(屬性動(dòng)畫之平移動(dòng)畫)實(shí)例代碼
這篇文章主要介紹了Android動(dòng)畫 實(shí)現(xiàn)開關(guān)按鈕動(dòng)畫(屬性動(dòng)畫之平移動(dòng)畫)實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-11-11Android基于hover組件實(shí)現(xiàn)監(jiān)控鼠標(biāo)移動(dòng)事件的方法
這篇文章主要介紹了Android基于hover組件實(shí)現(xiàn)監(jiān)控鼠標(biāo)移動(dòng)事件的方法,結(jié)合實(shí)例形式分析了hover組件監(jiān)控鼠標(biāo)光標(biāo)在view上變化的操作技巧,需要的朋友可以參考下2017-02-02Android簡(jiǎn)易音樂(lè)播放器實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android簡(jiǎn)易音樂(lè)播放器的實(shí)現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02全面解析Android中對(duì)EditText輸入實(shí)現(xiàn)監(jiān)聽的方法
這篇文章主要介紹了Android中對(duì)EditText輸入實(shí)現(xiàn)監(jiān)聽的方法,包括一個(gè)仿iOS的帶清除功能的ClearEditText輸入框控件的詳細(xì)使用介紹,需要的朋友可以參考下2016-04-04