欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Fragment+ViewPager實(shí)現(xiàn)底部導(dǎo)航欄

 更新時(shí)間:2018年06月25日 10:34:35   作者:crazy_yyyyy  
這篇文章主要為大家詳細(xì)介紹了使用Fragment+ViewPager實(shí)現(xiàn)底部導(dǎo)航欄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

前幾天準(zhǔn)備寫一個(gè)小程序,一直認(rèn)為fragment實(shí)現(xiàn)底部導(dǎo)航欄,是很容易的事情,可是卻遇到了前所未有的問題,先給大家貼出來我出錯(cuò)的界面布局代碼:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 android:background="@color/theme_color" 
 android:clipToPadding="true" 
 android:fitsSystemWindows="true" > 
 
 <include 
  android:id="@+id/actionbar_activity_main" 
  layout="@layout/actionbar" /> 
 
 <RelativeLayout 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:layout_below="@id/actionbar_activity_main" 
  android:background="@color/white" > 
 
  <RadioGroup 
   android:id="@+id/rg_menu_activity_main" 
   android:layout_width="fill_parent" 
   android:layout_height="55dp" 
   android:layout_alignParentBottom="true" 
   android:background="@color/theme_color" 
   android:gravity="center" 
   android:orientation="horizontal" 
   android:weightSum="3" > 
 
   <RadioButton 
    android:id="@+id/rb_main_tab_menu1" 
    style="@style/rbt_bottom" 
    android:checked="true" 
    android:drawableTop="@drawable/radiobt_main_home" 
    android:gravity="center" 
    android:paddingTop="12dp" 
    android:text="@string/home_text" 
    android:textColor="@color/white" /> 
 
   <RadioButton 
    android:id="@+id/rb_main_tab_menu2" 
    style="@style/rbt_bottom" 
    android:checked="false" 
    android:drawableTop="@drawable/radiobt_main_my" 
    android:gravity="center" 
    android:paddingTop="12dp" 
    android:text="@string/my_text" 
    android:textColor="@color/white" /> 
 
   <RadioButton 
    android:id="@+id/rb_main_tab_menu3" 
    style="@style/rbt_bottom" 
    android:checked="false" 
    android:drawableTop="@drawable/radiobt_main_setting" 
    android:gravity="center" 
    android:paddingTop="12dp" 
    android:text="@string/setting_text" 
    android:textColor="@color/white" /> 
  </RadioGroup> 
 
  <android.support.v4.view.ViewPager 
   android:id="@+id/vp_activity_main" 
   android:layout_width="fill_parent" 
   android:layout_height="fill_parent" 
   android:layout_above="@id/rg_menu_activity_main" /> 
 </RelativeLayout> 
 
</RelativeLayout> 

這個(gè)布局實(shí)現(xiàn)的效果如下:


只看效果,大家覺得挺好的,但是重要的問題來了。

下面的radioButton點(diǎn)了沒反應(yīng),我檢查了一下xml文件,再檢查了一下java代碼,沒有問題啊,我開始方了,,,然后網(wǎng)上搜,好像沒有人遇到這個(gè)問題,,檢查好幾遍之后,問旁邊的同學(xué),他說 既然設(shè)置了監(jiān)聽,但是沒有反應(yīng),那肯定是有一個(gè)東西把這個(gè)事件消費(fèi)掉了,我想起來了之前看的事件分發(fā)機(jī)制,,建議大家對事件分發(fā)機(jī)制不懂的小伙伴趕緊看看,面試和日常寫代碼都要用到,特別是面試,面試官特別喜歡問。

回歸正題,大家看我的 xml文件,我把viewpager寫在了Radiogroup的下面,并且,layout_height = "fill_parent"     這樣我的viewpager就消費(fèi)掉了我的radiobutton的點(diǎn)擊事件,其實(shí)之后我覺得,是我的邏輯不正確,我應(yīng)該順序的寫下來,而不是只實(shí)現(xiàn)功能,這樣我看自己的代碼可以看懂,可是給別人可能會造成誤解。

好了,開始說一下,實(shí)現(xiàn)底部導(dǎo)航的整個(gè)流程,實(shí)現(xiàn)的界面還如上:(在studio中寫的)

actionbar.xml  上面自定義的 actionbar  系統(tǒng)自帶的actionbar在java代碼中去掉了

代碼功能:  左邊返回鍵 中間顯示的文字 右邊的 更多 鍵   上圖中沒有顯示出來,是因?yàn)槲覜]有讓 左右的鍵顯示出來

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="fill_parent" 
 android:layout_height="@dimen/actionBar_height" 
 android:background="@color/theme_color" 
 > 
 <RelativeLayout 
  android:id="@+id/rl_back_actionbar" 
  android:layout_width="@dimen/actionBar_back" 
  android:layout_height="fill_parent" 
  android:visibility="invisible"> 
  <ImageView 
   android:layout_width="22dp" 
   android:layout_height="20dp" 
   android:layout_centerVertical="true" 
   android:layout_marginLeft="12dp" 
   android:background="@drawable/back" 
   android:contentDescription="@string/app_name"/> 
 </RelativeLayout> 
 <TextView 
  android:id="@+id/tv_title_actionbar" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_centerInParent="true" 
  android:text="@string/TimeRobot" 
  android:textColor="#ffffff" 
  android:textSize="@dimen/actionBar_title_text_size" 
  /> 
 <RelativeLayout 
  android:id="@+id/rl_more_actionbar" 
  android:layout_width="wrap_content" 
  android:layout_height="fill_parent" 
  android:layout_alignParentRight="true" 
  android:visibility="invisible"> 
  <ImageView 
   android:layout_width="25dp" 
   android:layout_height="25dp" 
   android:layout_centerVertical="true" 
   android:layout_marginRight="12dp" 
   android:background="@drawable/more" 
   android:contentDescription="@string/app_name"/> 
 </RelativeLayout> 
 
 
</RelativeLayout> 

activity_main.xml 文件 
中間的部分color資源,是我引用的自動(dòng)的,大家可以自己選擇

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:background="@color/theme_color" 
 android:clipToPadding="true" 
 tools:context=".MainActivity"> 
 <!-- 引入 actionbar--> 
 <include 
  android:id="@+id/actionbar_activity_main" 
  layout= "@layout/actionbar"/> 
 <android.support.v4.view.ViewPager 
  android:id="@+id/vp_activity_main" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:layout_below="@+id/actionbar_activity_main" 
  android:layout_alignParentRight="true" 
  android:layout_alignParentEnd="true" 
  android:background="@color/white"/> 
 
  <RadioGroup 
   android:id="@+id/rg_main_radioGroup" 
   android:layout_width="fill_parent" 
   android:layout_height="58dp" 
   android:layout_alignParentBottom="true" 
   android:background="@color/theme_color" 
   android:gravity="center" 
   android:orientation="horizontal" 
   android:weightSum="3"> 
   <RadioButton 
    android:id="@+id/rb_main_tab_menu1" 
    android:layout_weight="1" 
    android:layout_width="0dp" 
    android:layout_height="fill_parent" 
    android:checked="true" 
    android:drawableTop="@drawable/radio_bt_plan" 
    android:gravity="center" 
    android:button="@null" 
    android:paddingTop="8dp" 
    android:text="@string/home_text" 
    android:textColor="@color/white"/> 
   <RadioButton 
    android:id="@+id/rb_main_tab_menu2" 
    android:layout_weight="1" 
    android:layout_width="0dp" 
    android:layout_height="fill_parent" 
    android:checked="false" 
    android:drawableTop="@drawable/radio_bt_time" 
    android:gravity="center" 
    android:button="@null" 
    android:paddingTop="8dp" 
    android:text="@string/timeGroup" 
    android:textColor="@color/white"/> 
   <RadioButton 
    android:id="@+id/rb_main_tab_menu3" 
    android:layout_weight="1" 
    android:layout_width="0dp" 
    android:layout_height="fill_parent" 
    android:checked="false" 
    android:drawableTop="@drawable/radio_bt_my" 
    android:gravity="center" 
    android:button="@null" 
    android:paddingTop="8dp" 
    android:textColor="@color/white" 
    android:text="@string/My"/> 
  </RadioGroup> 
</RelativeLayout> 

注意一下,radiobutton中的一個(gè)屬性是  drawableTop屬性, 這個(gè)后面引用的drawable資源是 實(shí)現(xiàn) 點(diǎn)擊改變r(jià)adiobutton狀態(tài)的布局文件,我給大家貼出來 radio_bt_plan.xml文件的代碼,其他的只要新建,copy代碼,改顯示的圖片即可。
還有,對于初次用studio的伙伴,這里要注意了,new的時(shí)候,drawable --->右鍵  ---->  new ----> new resource file (即 出來的第一個(gè)) 而不是XML

radio_bt_plan.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
 <item android:drawable="@drawable/planning_press" 
  android:state_enabled="true" 
  android:state_focused="true" 
  android:state_pressed="false"/> 
 <item android:drawable="@drawable/planning_press" 
  android:state_enabled="true" 
  android:state_pressed="true" 
  /> 
 <item android:drawable="@drawable/planning_press" 
  android:state_checked="true" 
  android:state_enabled="true" 
  /> 
 <item android:drawable="@drawable/planning"/> 
 
</selector> 

接下來就是  MainActivity.java文件了

public class MainActivity extends FragmentActivity { 
 private TextView title_text; 
 private ViewPager vp_main_tab; 
 private List<Fragment> fragmentList = null; 
 private FragmentPagerAdapter mAdapter = null; 
 
 private MyFragment myFragment; 
 private TimeGroupFragment timeGroupFragment; 
 private PlanningFragment planningFragment; 
 
 private RadioButton rb_main_tab_menu1; 
 private RadioButton rb_main_tab_menu2; 
 private RadioButton rb_main_tab_menu3; 
 private RadioGroup rg_main_group; 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  requestWindowFeature(Window.FEATURE_NO_TITLE); 
  setContentView(R.layout.activity_main); 
  init(); 
  rg_main_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
 
   @Override 
   public void onCheckedChanged(RadioGroup group, int checkedId) { 
    switch (checkedId) { 
     case R.id.rb_main_tab_menu1: 
      vp_main_tab.setCurrentItem(0); 
      initActionbar(0); 
      break; 
     case R.id.rb_main_tab_menu2: 
      vp_main_tab.setCurrentItem(1); 
      initActionbar(1); 
      break; 
     case R.id.rb_main_tab_menu3: 
      vp_main_tab.setCurrentItem(2); 
      initActionbar(2); 
      break; 
     default: 
      break; 
    } 
 
   } 
  }); 
  initViewPage(); 
 } 
 private void initActionbar(int currentPage) { 
  switch (currentPage) { 
   case 0: 
    title_text.setText(getResources().getString(R.string.TimeRobot)); 
    break; 
   case 1: 
    title_text.setText(getResources().getString(R.string.timeGroup)); 
    break; 
   case 2: 
    title_text.setText(getResources().getString(R.string.My)); 
    break; 
   default: 
    break; 
  } 
 } 
 private void init() { 
  title_text = (TextView) findViewById(R.id.tv_title_actionbar); 
  vp_main_tab = (ViewPager) findViewById(R.id.vp_activity_main); 
  fragmentList = new ArrayList<Fragment>(); 
  rg_main_group = (RadioGroup) findViewById(R.id.rg_main_radioGroup); 
  rb_main_tab_menu1 = (RadioButton) findViewById(R.id.rb_main_tab_menu1); 
  rb_main_tab_menu2 = (RadioButton) findViewById(R.id.rb_main_tab_menu2); 
  rb_main_tab_menu3 = (RadioButton) findViewById(R.id.rb_main_tab_menu3); 
 
  myFragment = new MyFragment(); 
  timeGroupFragment = new TimeGroupFragment(); 
  planningFragment = new PlanningFragment(); 
 
  fragmentList.add(myFragment); 
  fragmentList.add(timeGroupFragment); 
  fragmentList.add(planningFragment); 
 
 
 } 
 
 private void initViewPage() { 
  android.support.v4.app.FragmentManager fm = getSupportFragmentManager(); 
  mAdapter = new FragmentPagerAdapter(fm) { 
 
   @Override 
   public int getCount() { 
    return fragmentList == null ? 0 : fragmentList.size(); 
   } 
 
   @Override 
   public android.support.v4.app.Fragment getItem(int position) { 
    return fragmentList.get(position); 
   } 
  }; 
  vp_main_tab.setAdapter(mAdapter); 
  vp_main_tab.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { 
 
   @Override 
   public void onPageSelected(int position) { 
    initActionbar(position); 
    switch (position) { 
     case 0: 
      rb_main_tab_menu1.setChecked(true); 
      break; 
     case 1: 
      rb_main_tab_menu2.setChecked(true); 
      break; 
     case 2: 
      rb_main_tab_menu3.setChecked(true); 
      break; 
     default: 
      break; 
    } 
   } 
 
   @Override 
   public void onPageScrolled(int arg0, float arg1, int arg2) { 
   } 
 
   @Override 
   public void onPageScrollStateChanged(int state) { 
   } 
  }); 
 } 
} 

這就是整個(gè)是實(shí)現(xiàn)經(jīng)過,如有什么不足的地方,還請多多指教;

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android自定義View實(shí)現(xiàn)水波紋引導(dǎo)動(dòng)畫

    Android自定義View實(shí)現(xiàn)水波紋引導(dǎo)動(dòng)畫

    這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)水波紋動(dòng)畫引導(dǎo),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • Jetpack Compose 雙指拖拽實(shí)現(xiàn)詳解

    Jetpack Compose 雙指拖拽實(shí)現(xiàn)詳解

    這篇文章主要為大家介紹了Jetpack Compose 雙指拖拽實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • android Launcher AppWidget添加步驟介紹

    android Launcher AppWidget添加步驟介紹

    大家好,本篇文章主要講的是android Launcher AppWidget添加步驟介紹,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2022-01-01
  • Android自定義加載框效果

    Android自定義加載框效果

    這篇文章主要為大家詳細(xì)介紹了Android自定義加載框效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Android單例模式的幾種方法總結(jié)

    Android單例模式的幾種方法總結(jié)

    這篇文章主要介紹了 Android單例模式的幾種方法總結(jié)的相關(guān)資料,這里有簡單模式,復(fù)雜模式,線程鎖式及雙層鎖式的方法整理,需要的朋友可以參考下
    2017-07-07
  • android webview獲取html代碼和根據(jù)id獲取value實(shí)例

    android webview獲取html代碼和根據(jù)id獲取value實(shí)例

    這篇文章主要介紹了android webview獲取html代碼和根據(jù)id獲取value實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • Android仿支付寶微信支付密碼界面彈窗封裝dialog

    Android仿支付寶微信支付密碼界面彈窗封裝dialog

    這篇文章主要介紹了Android仿支付寶微信支付密碼界面彈窗封裝dialog的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-03-03
  • Android使用Sensor感應(yīng)器實(shí)現(xiàn)線程中刷新UI創(chuàng)建android測力計(jì)的功能

    Android使用Sensor感應(yīng)器實(shí)現(xiàn)線程中刷新UI創(chuàng)建android測力計(jì)的功能

    這篇文章主要介紹了Android使用Sensor感應(yīng)器實(shí)現(xiàn)線程中刷新UI創(chuàng)建android測力計(jì)的功能,實(shí)例分析了Android使用Sensor感應(yīng)器實(shí)現(xiàn)UI刷新及創(chuàng)建測力器的技巧,需要的朋友可以參考下
    2015-12-12
  • Android 頁面多狀態(tài)布局管理的開發(fā)

    Android 頁面多狀態(tài)布局管理的開發(fā)

    頁面多狀態(tài)布局是開發(fā)中常見的需求,即頁面在不同狀態(tài)需要顯示不同的布局,這篇文章主要介紹了Android 頁面多狀態(tài)布局管理的開發(fā),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • Android四大組件之廣播BroadcastReceiver詳解

    Android四大組件之廣播BroadcastReceiver詳解

    Android開發(fā)的四大組件分別是:活動(dòng)(activity),用于表現(xiàn)功能;服務(wù)(service),后臺運(yùn)行服務(wù),不提供界面呈現(xiàn);廣播接受者(Broadcast Receive),勇于接收廣播;內(nèi)容提供者(Content Provider),支持多個(gè)應(yīng)用中存儲和讀取數(shù)據(jù),相當(dāng)于數(shù)據(jù)庫,本篇著重介紹廣播組件
    2021-11-11

最新評論