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

1.首先在res下的drawable下新建四個圖標(biāo)的xml,分別把圖標(biāo)的選中和未選中的狀態(tài)設(shè)置好,所有的圖片可以放在res下新建的一個drawable-xhdpi目錄下,這里僅展示一個圖標(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.在布局中開始布局:
<?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="購物車"
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.新建四個Fragment類,這里僅展示一個
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() {
//定義一個Fragment數(shù)組,存放四個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)聽事件
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();
//如果沒有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();
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)聯(lián)動下拉框 下拉列表spinner的實(shí)例代碼
- Android中使用開源框架Citypickerview實(shí)現(xiàn)省市區(qū)三級聯(lián)動選擇
- Android仿eleme點(diǎn)餐頁面二級聯(lián)動列表
- Android自定義WheelView地區(qū)選擇三級聯(lián)動
- Android中TabLayout+ViewPager實(shí)現(xiàn)tab和頁面聯(lián)動效果
- 最好用的Android省市區(qū)三級聯(lián)動選擇效果
- Android省市區(qū)三級聯(lián)動控件使用方法實(shí)例講解
- Android實(shí)現(xiàn)兩個ScrollView互相聯(lián)動的同步滾動效果代碼
- android-wheel控件實(shí)現(xiàn)三級聯(lián)動效果
- Android Fragment實(shí)現(xiàn)列表和內(nèi)容聯(lián)動
相關(guān)文章
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開發(fā)之自定義view實(shí)現(xiàn)通訊錄列表A~Z字母提示效果【附demo源碼下載】
這篇文章主要介紹了Android開發(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)豎直跑馬燈效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12
微信小程序 canvas開發(fā)實(shí)例及注意事項(xiàng)
這篇文章主要介紹了微信小程序 wxcanvas開發(fā)實(shí)例及注意事項(xiàng)的相關(guān)資料,這里對微信canvas與H5中的canvas做對比,并說明注意事項(xiàng),需要的朋友可以參考下2016-12-12
Android通過自定義view實(shí)現(xiàn)刮刮樂效果詳解
這篇文章主要介紹了如何在Android中利用自定義的view實(shí)現(xiàn)刮刮樂的效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟上小編一起動手試一試2022-03-03
Android自定義控件實(shí)現(xiàn)帶文字提示的SeekBar
這篇文章主要給大家介紹了關(guān)于Android自定義控件實(shí)現(xiàn)帶文字提示的SeekBar的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12

