Android实现底部图标与Fragment的联动实例
本文介绍了ndroid实现底部图标与Fragment的联动,分享给大家,希望此文章对各位有所帮助。
效果如下:

1.首先在res下的drawable下新建四个图标的xml,分别把图标的选中和未选中的状态设置好,所有的图片可以放在res下新建的一个drawable-xhdpi目录下,这里仅展示一个图标的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数组,存放四个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的监听事件
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//找到当前选中的图标
RadioButton rb= (RadioButton) group.findViewById(checkedId);
//找到所选图标的标签并转换为整数类型放到下面的方法中
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设为隐藏
for (Fragment fragment:mfragments){
transaction.hide(fragment);
}
//把选中的设为显示
transaction.show(mfragments[position]);
transaction.commit();
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
Kotlin高效实现 Android ViewPager2 顶部导航之动态配置与性能优化指
文章介绍了使用AndroidViewPager2和TabLayout实现高效顶部导航的方法,并提供了优化指南,包括避免不必要的Fragment实例化、动态配置页面、使用Kotlin特性减少冗余代码等,通过这些优化,代码变得更加高效、简洁和易于维护,感兴趣的朋友跟随小编一起看看吧2025-03-03
Android5.0+ CollapsingToolbarLayout使用详解
这篇文章主要为大家详细介绍了Android5.0+ CollapsingToolbarLayout使用,感兴趣的小伙伴们可以参考一下2016-09-09
Android 中自定义Dialog样式的Activity点击空白处隐藏软键盘功能(dialog不消失)
项目中需要开发带有EditText的Dialog显示,要求在编辑完EditText时,点击Dilog的空白处隐藏软键盘。但是Dialog不会消失。下面通过实例代码给大家分享实现方法,需要的的朋友参考下吧2017-04-04
Android中的popupwindow进入和退出的动画效果
这篇文章主要介绍了Android中的popupwindow进入和退出的动画,需要的朋友可以参考下2017-04-04
Android RecyclerView仿新闻头条的频道管理功能
这篇文章主要介绍了Android RecyclerView仿新闻头条的频道管理功能,需要的朋友可以参考下2017-06-06
Android编程滑动效果之Gallery+GridView实现图片预览功能(附demo源码下载)
这篇文章主要介绍了Android编程滑动效果之Gallery+GridView实现图片预览功能,结合实例形式分析了Android通过GridView和Gallery两个控件模仿Gallery图像集图片预览功能,并附带demo源码供读者下载参考,需要的朋友可以参考下2016-02-02


最新评论