Andoird开发 - 使用碎片和RadioGroup实现底部导航栏

设置底部的导航栏,点击这一项需要高亮这一项。按照ViewPager里面小白点的方式,新建几个selector,因为使用的是RadioGroup,因此需要通过android_state_checked属性判断是否点击。这里我只用文本为例,因为我没有合适的图片。
drawable文件夹新建background.xml,当enabled属性为true的时候,设置颜色为草绿色,false时为黑色。

filename:background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="@color/grassgreen"/>
    <item android:state_checked="false" android:color="@color/black"/>
</selector>

然后编写activity_main.xml布局文件,主要由两部分,一部分为主界面,为一个碎片,另一部分为一个RadioGroup控件。由于有2个相同的控件,因此可以将属性写成一个style,直接设置控件的style属性即可。

filename:res/styles
<style name="BottomNavItemStyle">
        <item name="android:layout_weight">1</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:background">@color/palegoldenrod</item>
        <item name="android:textColor">@drawable/background</item>
        <item name="android:button">@null</item>
        <item name="android:gravity">center</item>
    </style>

RadioButton还想有个属性是adnroid:drawableTop可以用来设置图片的属性,也是利用selector,哭了哭了,不会用RelativeLayout了,换成vertical的LinearLayout吧。

filename:activity_main.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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.k.androidpractie.MainActivity">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/MainFragment"></FrameLayout>
    <RadioGroup
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="50dp"
        android:id="@+id/BottonNav">
        <RadioButton
            android:id="@+id/Page_1"
            style="@style/BottomNavItemStyle"
            android:text="第一页"/>
        <RadioButton
            android:id="@+id/Page_2"
            style="@style/BottomNavItemStyle"
            android:text="第二页"/>
    </RadioGroup>
</LinearLayout>

然后创建2个Fragment,改一下text

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:layout_gravity="center"
        android:text="第一个"/>
</LinearLayout>

新建两个碎片类,加载一下碎片

public class MainFrag_1 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.main_frag_1,container,false);
        return view;
    }
}

最后在MainActivity.java中编写,其实获取RadioButton实例没用,获取RadioGroup实例然后设置监听,然后通过checkedId判断是哪个RadioButton。
然后好像FragmentTransaction在commit()一次以后得重新获取,不然他报错已commit。
碎片实例可以通过add()方法添加,也可以通过replace()替换。
一个方法,先把所有碎片实例,如果存在,调用hide()方法隐藏。假设碎片实例为a,可以先判断a是否为null,即是否存在,不存在的话new一个,存在的话直接show()。因为有可能界面中需要保存原有的数据,如果每次都new可能会让数据 丢失。

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    FrameLayout MainFragmentFrameLayout;
    RadioButton Page_1_Button,Page_2_Button;
    RadioGroup BottomNavRadioGroup;
    MainFrag_1 mainFrag_1;
    MainFrag_2 mainFrag_2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MainFragmentFrameLayout=findViewById(R.id.MainFragment);
        BottomNavRadioGroup=findViewById(R.id.BottonNav);
        Page_1_Button=findViewById(R.id.Page_1);
        Page_2_Button=findViewById(R.id.Page_2);
        Page_1_Button.setChecked(true);
        //fragmentTransaction.replace(R.id.MainFragment,new MainFrag_1());
        BottomNavRadioGroup.setOnCheckedChangeListener(this) ;
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        FragmentManager fragmentManager=getSupportFragmentManager();
        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
        hideFragment(fragmentTransaction);
        switch(checkedId){
            case R.id.Page_1:
                if (mainFrag_1==null){
                    mainFrag_1=new MainFrag_1();
                    fragmentTransaction.add(R.id.MainFragment,mainFrag_1);
                }else{
                    fragmentTransaction.show(mainFrag_1);
                }
                break;
            case R.id.Page_2:
                if (mainFrag_2==null){
                    mainFrag_2=new MainFrag_2();
                    fragmentTransaction.add(R.id.MainFragment,mainFrag_2);
                }else{
                    fragmentTransaction.show(mainFrag_2);
                }
                break;
        }
        fragmentTransaction.commit();
    }
    public void hideFragment(FragmentTransaction fragmentTransaction){
        if (mainFrag_1!=null){
            fragmentTransaction.hide(mainFrag_1);
        }
        if (mainFrag_2!=null){
            fragmentTransaction.hide(mainFrag_2);
        }
    }
}

然后运行程序就可以看到效果了
在这里插入图片描述

在这里插入图片描述

发布了21 篇原创文章 · 获赞 7 · 访问量 7569

猜你喜欢

转载自blog.csdn.net/qq_41037945/article/details/104183286
今日推荐