本文给大家介绍Fragment实现底部导航栏,对Fragment实现底部导航栏相关知识感兴趣的朋友一起学习吧

写这个我们需要写四个fragment的布局,下面只给出其中一个布局的xml.

代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
   <ImageView
       android:id="@+id/touxiang"
       android:src="@mipmap/toupiao"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" />
</LinearLayout>

接下来我们需要写四个相应的Fragment的实现类,同样拷贝4份,改用inflate加载Fragment即可。即,包含四个差不多一样的Fragment

fragment的实现:
public class Fragment1 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_center,null);
        return view;
    }

}

然后是activity的布局:一个Framelayout+RadioGroup包含四个RadioButton,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">
    <FrameLayout
        android:id="@+id/fragment_fragment"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp">
    </FrameLayout>
    <RadioGroup
        android:id="@+id/fragmeng_radioGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/fragment_radioBtn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/radio_button_pic1"
            android:gravity="center"
            android:text="主页" />
        <RadioButton
            android:id="@+id/fragment_radioBtn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/radio_button_pic2"
            android:gravity="center"
            android:text="物业" />
        <RadioButton
            android:id="@+id/fragment_radioBtn3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/radio_button_pic3"
            android:gravity="center"
            android:text="发现" />
        <RadioButton
            android:id="@+id/fragment_radioBtn4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/radio_button_pic4"
            android:gravity="center"
            android:text="我的" />
    </RadioGroup>
</LinearLayout>
最后写最重要的Activity的实现:

各个七零八落的小部件都已经准备到序了,现在只剩下这个主界面实现类把他们融合在一起,实现相应的效果了。FragmentActivity.java 的编写也很简单,直接看代码和注释就可以了,不多解释额:主要包含几个初始化方法、选中处理、隐藏所有Fragment的方法。

public class FragmentActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener{

    private FragmentManager fragmentManager;
    private RadioGroup radioGroup;

    private Fragment fragment1, fragment2, fragment3, fragment4;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);
        initView();
        initData();

    }

    public void initView() {
        radioGroup = findViewById(R.id.fragmeng_radioGroup);

        radioGroup.setOnCheckedChangeListener(this);

    }

    public void initData() {
        //1-创建fragment管理器
        fragmentManager = getFragmentManager();

        radioGroup.check(R.id.fragment_radioBtn1);

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        //2-通过fragment管理器得到fragment事物
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        hideFragmentAll(fragmentTransaction);//隐藏所有framgnet
        switch (checkedId) {
            case R.id.fragment_radioBtn1:
                //3-往事物里面添加fragment
                if (fragment1 == null) {
                    fragment1 = new Fragment1();
                    fragmentTransaction.add(R.id.fragment_fragment, fragment1);
                }
                fragmentTransaction.show(fragment1);

                break;
            case R.id.fragment_radioBtn2:
                if (fragment2 == null) {
                    fragment2 = new Fragment2();
                    fragmentTransaction.add(R.id.fragment_fragment, fragment2);
                }
                fragmentTransaction.show(fragment2);
                break;
            case R.id.fragment_radioBtn3:
                if (fragment3 == null) {
                    fragment3 = new Fragment3();
                    fragmentTransaction.add(R.id.fragment_fragment, fragment3);
                }
                fragmentTransaction.show(fragment3);
                break;
            case R.id.fragment_radioBtn4:
                if (fragment4 == null) {
                    fragment4 = new Fragment4();
                    fragmentTransaction.add(R.id.fragment_fragment, fragment4);
                }
                fragmentTransaction.show(fragment4);
                break;
        }
        //4-提交事物
        fragmentTransaction.commit();
    }

    //隐藏所有framgnet
    public void hideFragmentAll(FragmentTransaction fragmentTransaction) {
        if (fragment1 != null) fragmentTransaction.hide(fragment1);
        if (fragment2 != null) fragmentTransaction.hide(fragment2);
        if (fragment3 != null) fragmentTransaction.hide(fragment3);
        if (fragment4 != null) fragmentTransaction.hide(fragment4);
    }
}
  到这里我们的功能就基本实现了,是不是还挺简单的。
 
 

注意:

Fragment相关类导入的时候是v4包还是app包!我们先导app的包,不对再导v4的包.

以上内容是小白给大家分享的Android程序开发之Fragment实现底部导航栏实例代码,希望对大家有所帮助!

源码下载地址: https://download.csdn.net/download/weixin_42267745/10439958




猜你喜欢

转载自blog.csdn.net/weixin_42267745/article/details/80465731
今日推荐