Android实现底部图标与Fragment的联动实例

所属分类: 软件编程 / Android 阅读数: 28
收藏 0 赞 0 分享

本文介绍了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();

  }


}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android中加入名片扫描功能实例代码

这篇文章主要介绍了Android中加入名片扫描功能实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信发表说说实现拍照、多图上传功能

这篇文章主要为大家详细介绍了Android仿微信发表说说实现拍照、多图上传功能,使用Retrofit2.0技术,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

设置Android系统永不锁屏永不休眠的方法

在进行Android系统开发的时候,有些特定的情况需要设置系统永不锁屏,永不休眠。本篇文章给大家介绍Android 永不锁屏,开机不锁屏,删除设置中休眠时间选项,需要的朋友一起学习吧
收藏 0 赞 0 分享

Android Retrofit 2.0框架上传图片解决方案

这篇文章主要介绍了Android Retrofit 2.0框架上传一张与多张图片解决方案,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义等待对话框

这篇文章主要为大家详细介绍了Android自定义等待对话框的实现方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Window添加View的底层原理

这篇文章主要介绍了Android中Window添加View的底层原理,需要的朋友可以参考下
收藏 0 赞 0 分享

Android调用系统默认浏览器访问的方法

这篇文章主要介绍了Android调用系统默认浏览器访问的方法的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发退出程序的方法汇总

Android程序有很多Activity,比如说主窗口A,调用了子窗口B,子窗口B又调用子窗口C,back返回子窗口B后,在B中如何关闭整个Android应用程序呢? 下面脚本之家小编就给大家介绍android开发退出程序的几种方法,感兴趣的朋友参考下吧
收藏 0 赞 0 分享

Android程序开发中单选按钮(RadioGroup)的使用详解

在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中

这篇文章主要介绍了Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多