Android實(shí)現(xiàn)底部圖標(biāo)與Fragment的聯(lián)動(dòng)實(shí)例
本文介紹了ndroid實(shí)現(xiàn)底部圖標(biāo)與Fragment的聯(lián)動(dòng),分享給大家,希望此文章對(duì)各位有所幫助。
效果如下:

1.首先在res下的drawable下新建四個(gè)圖標(biāo)的xml,分別把圖標(biāo)的選中和未選中的狀態(tài)設(shè)置好,所有的圖片可以放在res下新建的一個(gè)drawable-xhdpi目錄下,這里僅展示一個(gè)圖標(biāo)的xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:drawable="@drawable/ic_nav_home_press"/> <item android:state_checked="false" android:drawable="@drawable/ic_nav_home"/> <item android:drawable="@drawable/ic_nav_home"/> </selector>
2.在布局中開(kāi)始布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.baway.lizongshu.view.activity.MainActivity">
<FrameLayout
android:id="@+id/framelayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioGroup
android:id="@+id/rg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/fenlei"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="分類"
android:button="@null"
android:checked="true"
android:drawableTop="@drawable/fenlei"
android:gravity="center"
android:textSize="12sp"
android:tag="0"
/>
<RadioButton
android:id="@+id/gouwuche"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="購(gòu)物車"
android:button="@null"
android:drawableTop="@drawable/gouwuche"
android:gravity="center"
android:textSize="12sp"
android:tag="1"
/>
<RadioButton
android:id="@+id/qita"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="其他"
android:button="@null"
android:drawableTop="@drawable/qita"
android:gravity="center"
android:textSize="12sp"
android:tag="2"
/>
<RadioButton
android:id="@+id/wode"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="我的"
android:button="@null"
android:drawableTop="@drawable/wode"
android:gravity="center"
android:textSize="12sp"
android:tag="3"
/>
</RadioGroup>
</LinearLayout>
</LinearLayout>
3.新建四個(gè)Fragment類,這里僅展示一個(gè)
public class FenleiFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fenlei, container, false);
return view;
}
}
4. 主界面中:
public class MainActivity extends AppCompatActivity {
private RadioGroup rg;
private Fragment[] mfragments;
private FragmentManager fm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initview();
initdata();
}
private void initdata() {
//定義一個(gè)Fragment數(shù)組,存放四個(gè)Fragment
mfragments=new Fragment[4];
mfragments[0]=new FenleiFragment();
mfragments[1]=new GouwucheFragment();
mfragments[2]=new QitaFragment();
mfragments[3]=new WodeFragment();
//獲得Fragment管理者
fm = getSupportFragmentManager();
//處理
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.framelayout,mfragments[0],"0");
ft.commit();
}
private void initview() {
rg=(RadioGroup) findViewById(R.id.rg);
//RadioGroup的監(jiān)聽(tīng)事件
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//找到當(dāng)前選中的圖標(biāo)
RadioButton rb= (RadioButton) group.findViewById(checkedId);
//找到所選圖標(biāo)的標(biāo)簽并轉(zhuǎn)換為整數(shù)類型放到下面的方法中
int i = Integer.parseInt(rb.getTag().toString().trim());
showAndHideFragment(i);
}
});
}
//展示和隱藏Fragment的方法
private void showAndHideFragment(int position) {
FragmentTransaction transaction = fm.beginTransaction();
//如果沒(méi)有fragment就在framelayout里面加上
if (!mfragments[position].isAdded()){
transaction.add(R.id.framelayout,mfragments[position],""+position);
}
//把所有的fragment設(shè)為隱藏
for (Fragment fragment:mfragments){
transaction.hide(fragment);
}
//把選中的設(shè)為顯示
transaction.show(mfragments[position]);
transaction.commit();
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)聯(lián)動(dòng)下拉框 下拉列表spinner的實(shí)例代碼
- Android中使用開(kāi)源框架Citypickerview實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)選擇
- Android仿eleme點(diǎn)餐頁(yè)面二級(jí)聯(lián)動(dòng)列表
- Android自定義WheelView地區(qū)選擇三級(jí)聯(lián)動(dòng)
- Android中TabLayout+ViewPager實(shí)現(xiàn)tab和頁(yè)面聯(lián)動(dòng)效果
- 最好用的Android省市區(qū)三級(jí)聯(lián)動(dòng)選擇效果
- Android省市區(qū)三級(jí)聯(lián)動(dòng)控件使用方法實(shí)例講解
- Android實(shí)現(xiàn)兩個(gè)ScrollView互相聯(lián)動(dòng)的同步滾動(dòng)效果代碼
- android-wheel控件實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)效果
- Android Fragment實(shí)現(xiàn)列表和內(nèi)容聯(lián)動(dòng)
相關(guān)文章
Android滾動(dòng)菜單ListView實(shí)例詳解
這篇文章主要為大家詳細(xì)介紹了Android滾動(dòng)菜單ListView實(shí)例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
Android編程實(shí)現(xiàn)AIDL(跨進(jìn)程通信)的方法詳解
這篇文章主要介紹了Android編程實(shí)現(xiàn)AIDL(跨進(jìn)程通信)的方法,結(jié)合實(shí)例形式詳細(xì)分析了Android實(shí)現(xiàn)AIDL(跨進(jìn)程通信)的原理、具體流程與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-06-06
Android開(kāi)發(fā)之自定義view實(shí)現(xiàn)通訊錄列表A~Z字母提示效果【附demo源碼下載】
這篇文章主要介紹了Android開(kāi)發(fā)之自定義view實(shí)現(xiàn)通訊錄列表A~Z字母提示效果,結(jié)合完整實(shí)例形式分析了Android獲取通訊錄列表及采用自定義view排列顯示的相關(guān)操作技巧,需要的朋友可以參考下2017-07-07
Android自定義控件ViewFipper實(shí)現(xiàn)豎直跑馬燈效果
這篇文章主要為大家詳細(xì)介紹了Android自定義控件ViewFipper實(shí)現(xiàn)豎直跑馬燈效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
微信小程序 canvas開(kāi)發(fā)實(shí)例及注意事項(xiàng)
這篇文章主要介紹了微信小程序 wxcanvas開(kāi)發(fā)實(shí)例及注意事項(xiàng)的相關(guān)資料,這里對(duì)微信canvas與H5中的canvas做對(duì)比,并說(shuō)明注意事項(xiàng),需要的朋友可以參考下2016-12-12
Android通過(guò)自定義view實(shí)現(xiàn)刮刮樂(lè)效果詳解
這篇文章主要介紹了如何在Android中利用自定義的view實(shí)現(xiàn)刮刮樂(lè)的效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟上小編一起動(dòng)手試一試2022-03-03
Android自定義控件實(shí)現(xiàn)帶文字提示的SeekBar
這篇文章主要給大家介紹了關(guān)于Android自定義控件實(shí)現(xiàn)帶文字提示的SeekBar的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12
SeekBar拖動(dòng)條的應(yīng)用實(shí)例
這篇文章主要為大家詳細(xì)介紹了SeekBar拖動(dòng)條的應(yīng)用實(shí)例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10

