[木木方文安卓學習筆記六]ViewPager+RadioGroup實現底部導航欄
新建一個xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="[http://schemas.android.com/apk/res/android](http://schemas.android.com/apk/res/android)">
<item
android:id="@+id/navigation_home"
android:icon="@drawable/ic_home_black_24dp"
android:title="@string/app_home" />
<item
android:id="@+id/navigation_telephoneBook"
android:icon="@drawable/ic_dashboard_black_24dp"
android:title="@string/app_telephoneBook" />
<item
android:id="@+id/navigation_work"
android:icon="@drawable/ic_notifications_black_24dp"
android:title="@string/app_work" />
<item
android:id="@+id/navigation_personal"
android:icon="@drawable/ic_dashboard_black_24dp"
android:title="@string/app_personal"/>
</menu>
單個button選中新建一個home_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="[http://schemas.android.com/apk/res/android](http://schemas.android.com/apk/res/android)">
<item android:state_checked="true" android:drawable="@mipmap/home_selected"/>
<item android:drawable="@mipmap/home_normal"/>
</selector>
在activity_main中佈局
<RadioGroup
android:id="@+id/main_nav_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/main_nav_home"
style="@style/main_navi_radiobutton"
android:drawableTop="@drawable/home_selector"
android:textColor="@color/colorRed"
android:checked="true"
android:text="@string/app_home"/>
<RadioButton
android:id="@+id/main_nav_telephone"
style="@style/main_navi_radiobutton"
android:drawableTop="@drawable/telephone_selector"
android:textColor="@color/colorTextGray"
android:text="@string/app_telephoneBook"/>
<RadioButton
android:id="@+id/main_nav_work"
style="@style/main_navi_radiobutton"
android:drawableTop="@drawable/work_selector"
android:textColor="@color/colorTextGray"
android:text="@string/app_work"/>
<RadioButton
android:id="@+id/main_nav_personal"
style="@style/main_navi_radiobutton"
android:drawableTop="@drawable/personal_selector"
android:textColor="@color/colorTextGray"
android:text="@string/app_personal"/>
</RadioGroup>
在style
<style name="main_navi_radiobutton">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">50dp</item>
<item name="android:layout_weight">1</item>
<item name="android:button">@null</item>
<item name="android:gravity">center</item>
<item name="android:drawablePadding">5dp</item>
<item name="android:textSize">10sp</item>
<item name="android:background">@android:color/transparent</item>
<item name="android:paddingTop">5dp</item>
<item name="android:paddingBottom">5dp</item>
</style>
在java中
private void initBottomNav() {
pager = (ViewPager) findViewById(R.id.vPager);
radioGroup = (RadioGroup) findViewById(R.id.main_nav_radiogroup);
fragments = new ArrayList<Fragment>();
homeFragment = new HomeFragment();
telephoneBookFragment = new TelephoneBookFragment();
workFragment = new WorkFragment();
personalFragment = new PersonalFragment();
fragments.add(homeFragment);
fragments.add(telephoneBookFragment);
fragments.add(workFragment);
fragments.add(personalFragment);
rb_Home = (RadioButton) findViewById(R.id.main_nav_home);
rb_telephoneBook = (RadioButton) findViewById(R.id.main_nav_telephone);
rb_work = (RadioButton) findViewById(R.id.main_nav_work);
rb_personal = (RadioButton) findViewById(R.id.main_nav_personal);
findViewById(R.id.main_nav_home).setOnClickListener(this);
findViewById(R.id.main_nav_telephone).setOnClickListener(this);
findViewById(R.id.main_nav_work).setOnClickListener(this);
findViewById(R.id.main_nav_personal).setOnClickListener(this);
pager.setAdapter(new FragmentPagerAdapter(this.getSupportFragmentManager()) {
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
});
pager.addOnPageChangeListener(this);
}
實現方法
@Override
public void onClick(View view) {
// 加這句
// Toast.makeText(MainActivity.this, "測試!", Toast.LENGTH_SHORT).show();
switch (view.getId()) {
case [R.id](http://R.id).main_nav_home:
pager.setCurrentItem(0);
break;
case [R.id](http://R.id).main_nav_telephone:
pager.setCurrentItem(1);
break;
case [R.id](http://R.id).main_nav_work:
pager.setCurrentItem(2);
break;
case [R.id](http://R.id).main_nav_personal:
pager.setCurrentItem(3);
break;
default:
break;
}
setTabSate();
}
private void setTabSate() {
setHomeState();
setTelephoneBookState();
setWorkState();
setPersonalState();
}
private void setHomeState() {
if (rb_Home.isChecked()) {
rb_Home.setTextColor(ContextCompat.getColor(this, R.color.colorRed));
}else {
rb_Home.setTextColor(ContextCompat.getColor(this, R.color.colorTextGray));
}
}
private void setTelephoneBookState() {
if (rb_telephoneBook.isChecked()) {
rb_telephoneBook.setTextColor(ContextCompat.getColor(this, R.color.colorRed));
}else {
rb_telephoneBook.setTextColor(ContextCompat.getColor(this, R.color.colorTextGray));
}
}
private void setWorkState() {
if (rb_work.isChecked()) {
rb_work.setTextColor(ContextCompat.getColor(this, R.color.colorRed));
}else {
rb_work.setTextColor(ContextCompat.getColor(this, R.color.colorTextGray));
}
}
private void setPersonalState() {
if (rb_personal.isChecked()) {
rb_personal.setTextColor(ContextCompat.getColor(this, R.color.colorRed));
}else {
rb_personal.setTextColor(ContextCompat.getColor(this, R.color.colorTextGray));
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
setTabSate();
}
相關文章
- Flutter仿閒魚底部導航欄實現Flutter
- Flutter學習筆記--仿閒魚底部導航欄帶有中間凸起圖示Flutter筆記
- 快速實現底部導航欄及未讀訊息
- Android 使用BottomNavigationView實現底部導航欄AndroidNavigationView
- Android BottomNavigationView,底部導航欄的簡單實現AndroidNavigationView
- bootstrap導航欄學習boot
- Android 底部導航欄 (底部 Tab) 最佳實踐|掘金技術徵文Android
- Tablayout實現導航欄TabLayout
- 安卓導航抽屜 Navigation Drawer 實現沉浸通知欄安卓Navigation
- 僅2步實現 拜拜 漢堡導航欄效果~ 全新底部導航互動(滑動隱藏)
- [譯]在底部導航欄裡使用 ProviderIDE
- Android 安卓手機及平板虛擬鍵盤遮住底部導航欄問題Android安卓
- vue2.0實現底部導航切換效果Vue
- 定製化你的ReactNative底部導航欄React
- 直播小程式原始碼,配置tabbar底部導航欄原始碼tabBar
- weui+swiper 滑動底部導航欄切換UI
- Halcon 學習筆記(4):導航首頁筆記
- 安卓中如何實現滑動導航安卓
- 安卓開發:安卓底部選單欄的實現,RadioGroup 和Fragment安卓Fragment
- jQuery實現吸頂動畫導航欄jQuery動畫
- flutter 自定義tab導航-頂部導航-底部導航Flutter
- iOS 記一次導航欄平滑過渡的實現iOS
- 純css實現固定在網頁底部選單導航CSS網頁
- iOS 超Easy實現 漸變導航欄iOS
- 商城側欄導航效果實現詳解
- iOS逆向之分析微信導航欄實現iOS
- Javascript 學習 筆記六JavaScript筆記
- Flutter 底部導航詳解Flutter
- Redis 學習筆記(六)Redis 如何實現訊息佇列Redis筆記佇列
- 直播系統程式碼,flutter手寫一個底部導航欄Flutter
- 直播app原始碼,全屏並且去掉底部虛擬導航欄APP原始碼
- APP中,用mui做的底部導航欄,引用外部圖示APPUI
- 【前端】javascript實現導航欄筋斗雲效果特效前端JavaScript特效
- Bootstrap 4 實現導航欄右側對齊boot
- JS如何實現導航欄的智慧浮動JS
- 安卓初學基礎學習筆記安卓筆記
- 安卓學習筆記之Activity(一)安卓筆記
- HexMap學習筆記(六)——河流筆記