Android Fragment實(shí)現(xiàn)底部通知欄
Android Fragment實(shí)現(xiàn)底部通知欄,供大家參考,具體內(nèi)容如下
截圖如下:

1. 第一步先要?jiǎng)?chuàng)建fragment(動(dòng)態(tài)注冊(cè))

然后將兩個(gè)勾選取消掉(還有一種是自己手動(dòng)創(chuàng)建)
會(huì)自動(dòng)生成相對(duì)應(yīng)的layout布局,剩下的要根據(jù)自己的需求了
2.在Activity的布局里寫(xiě)好四個(gè)按鈕
這里不是重點(diǎn)…
<?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"
>
<LinearLayout
android:id="@+id/ll_content_part"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</LinearLayout>
<LinearLayout
android:id="@+id/ll_function"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_msg_list"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="13dp"
android:onClick="click"
android:text="msg"/>
<Button
android:id="@+id/btn_contact"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="13dp"
android:onClick="click"
android:text="contact"/>
<Button
android:id="@+id/btn_disc"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="13dp"
android:onClick="click"
android:text="disc"/>
<Button
android:id="@+id/btn_me"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="13dp"
android:onClick="click"
android:text="me"/>
</LinearLayout>
</RelativeLayout>
3.Activity的代碼
其中定義了四個(gè)整型常量記錄了四個(gè)按鈕的狀態(tài),還有一個(gè)當(dāng)前狀態(tài),進(jìn)而判斷當(dāng)前點(diǎn)擊按鈕的狀態(tài),點(diǎn)擊切換文字顏色和圖標(biāo)
每次判斷四個(gè)Fragment的引用是否為空,不為空就不需要每次在new一遍Fragment
replace每次都會(huì)重新初始化fragment,它是先remove掉相同id的fragment,再add當(dāng)前fragment。
add不會(huì)回每次都初始化fragment,一般配合hide()和show()方法
public class MainActivity extends AppCompatActivity {
private Button btn_contact;
private Button btn_disc;
private Button btn_me;
private Button btn_msg_list;
private FragmentManager fragmentManager;
private MsgListFragment msgListFragment;
private ContactFragment contactFragment;
private DiscoveryFragment discoveryFragment;
private MeFragment meFragment;
private final int STATE_MSG =1;
private final int STATE_CON =2;
private final int STATE_DIS =3;
private final int STATE_ME =4;
private int currentState;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
chageButtonColor(currentState,STATE_MSG);
}
private void initData() {
fragmentManager = getFragmentManager();
switchFragment(this.STATE_MSG);
}
private void initView() {
btn_contact = (Button) findViewById(R.id.btn_contact);
btn_disc = (Button) findViewById(R.id.btn_disc);
btn_me = (Button) findViewById(R.id.btn_me);
btn_msg_list = (Button) findViewById(R.id.btn_msg_list);
}
public void click(View view) {
switch (view.getId()){
case R.id.btn_msg_list:
switchFragment(this.STATE_MSG);
break;
case R.id.btn_contact:
switchFragment(this.STATE_CON);
break;
case R.id.btn_disc:
switchFragment(this.STATE_DIS);
break;
case R.id.btn_me:
switchFragment(this.STATE_ME);
break;
}
}
private void switchFragment(int toState) {
if (toState == currentState)
return;
chageButtonColor(currentState,toState);
this.currentState=toState;
FragmentTransaction transaction = this.fragmentManager.beginTransaction();
Fragment fragment=msgListFragment;
switch (toState){
case STATE_DIS:
if (discoveryFragment == null)
discoveryFragment= new DiscoveryFragment();
fragment= discoveryFragment;
break;
case STATE_ME:
if (meFragment == null)
meFragment= new MeFragment();
fragment= meFragment;
break;
case STATE_CON:
if (contactFragment == null)
contactFragment= new ContactFragment();
fragment= contactFragment;
break;
case STATE_MSG:
if (msgListFragment == null)
msgListFragment= new MsgListFragment();
fragment= msgListFragment;
break;
}
transaction.replace(R.id.ll_content_part,fragment);
transaction.commit();
}
private void chageButtonColor(int currentState,int toState){
switch (currentState){
case STATE_DIS:
this.btn_disc.setTextColor(Color.BLACK);
break;
case STATE_ME:
this.btn_me.setTextColor(Color.BLACK);
break;
case STATE_CON:
this.btn_contact.setTextColor(Color.BLACK);
break;
case STATE_MSG:
this.btn_msg_list.setTextColor(Color.BLACK);
break;
}
switch (toState){
case STATE_DIS:
this.btn_disc.setTextColor(Color.GREEN);
break;
case STATE_ME:
this.btn_me.setTextColor(Color.GREEN);
break;
case STATE_CON:
this.btn_contact.setTextColor(Color.GREEN);
break;
case STATE_MSG:
this.btn_msg_list.setTextColor(Color.GREEN);
break;
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android?Fragment實(shí)現(xiàn)頂部、底部導(dǎo)航欄
- Android Fragment源碼分析Add方法
- Android開(kāi)發(fā)之Fragment懶加載的幾種方式及性能對(duì)比
- Android入門(mén)教程之Fragment的具體使用詳解
- Android Fragment使用全解
- Android使用Fragment實(shí)現(xiàn)兼容手機(jī)和平板的程序
- Android Fragment監(jiān)聽(tīng)返回鍵的一種合理方式
- 詳解Android studio 動(dòng)態(tài)fragment的用法
- Android Fragment的具體使用方式詳解
相關(guān)文章
Android開(kāi)發(fā)5:應(yīng)用程序窗口小部件App Widgets的實(shí)現(xiàn)(附demo)
本篇文章主要介紹了android應(yīng)用程序窗口小部件App Widgets的實(shí)現(xiàn),具有一定的參考價(jià)值,有需要的可以了解一下。2016-11-11
Android矢量圖之VectorDrawable類(lèi)自由填充色彩
這篇文章主要介紹了Android矢量圖之VectorDrawable類(lèi)自由填充色彩的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-05-05
Android 調(diào)用notifyDataSetChanged方法失敗解決辦法
這篇文章主要介紹了Android 調(diào)用notifyDataSetChanged方法失敗解決辦法的相關(guān)資料,需要的朋友可以參考下2017-07-07
Android沉浸式狀態(tài)欄的實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android沉浸式狀態(tài)欄的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
Android實(shí)現(xiàn)手勢(shì)劃定區(qū)域裁剪圖片
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)手勢(shì)劃定區(qū)域裁剪圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
Android實(shí)現(xiàn)調(diào)用攝像頭
本文給大家分享的是,在安卓APP開(kāi)發(fā)的過(guò)程中,經(jīng)常會(huì)需要調(diào)用手機(jī)自身攝像頭拍照的代碼,十分的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下。2015-07-07
Android編程實(shí)現(xiàn)讀取本地SD卡圖片的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)讀取本地SD卡圖片的方法,涉及Android針對(duì)文件讀取及判定操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
Android中的sqlite查詢(xún)數(shù)據(jù)時(shí)去掉重復(fù)值的方法實(shí)例
今天小編就為大家分享一篇關(guān)于Android中的sqlite查詢(xún)數(shù)據(jù)時(shí)去掉重復(fù)值的方法實(shí)例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01

