第13天Fragment案例1:底部导航Fragment的hide和show切换(****)

第13天Fragment案例1:底部导航Fragment的hide和show切换(****)

Fragment底部切换

一.效果

在这里插入图片描述

二.思路

四个radiobutton+4个fragent+动态hide和show指定的Fragment

三.代码

(1)创建4个Fragment,此处省略:
(2)activity_main.xml:

<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"
    tools:context=".MainActivity"
    android:orientation="vertical">
    <FrameLayout
        android:id="@+id/frame_layout"
        android:layout_weight="10"
        android:layout_width="match_parent"
        android:layout_height="0dp">
    </FrameLayout>
    <RadioGroup
        android:id="@+id/rg"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal">
        <RadioButton
            android:checked="true"
            android:layout_gravity="center"
            android:textAlignment="center"
            android:drawableTop="@drawable/selector1"
            android:id="@+id/rb1"
            android:text="微信"
            android:button="@null"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />
        <RadioButton
            android:layout_gravity="center"
            android:textAlignment="center"
            android:drawableTop="@drawable/selector1"
            android:id="@+id/rb2"
            android:text="微信"
            android:button="@null"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />

        <RadioButton
            android:layout_gravity="center"
            android:textAlignment="center"
            android:drawableTop="@drawable/selector1"
            android:id="@+id/rb3"
            android:text="微信"
            android:button="@null"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />

        <RadioButton
            android:layout_gravity="center"
            android:textAlignment="center"
            android:drawableTop="@drawable/selector1"
            android:id="@+id/rb4"
            android:text="微信"
            android:button="@null"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />
    </RadioGroup>
</LinearLayout>

(3)MainActivity.java:

public class MainActivity extends AppCompatActivity {
    //视图
    private MyViewPager vp;
    private RadioGroup rg;
    private RadioButton rb1,rb2,rb3,rb4;
    //数据
    private ArrayList<Fragment> fragmentArrayList=new ArrayList<>();
    //记录当前正在显示的Fragment
    private Fragment currentFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //TODO 去除Bar
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);//这句话一定要放到中间
        setContentView(R.layout.activity_main);
        initview();//找组件
        initdata();//初始化数据
        initlistener();//事件监听
    }

    private void initlistener() {
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
           public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(rb1.getId()==checkedId){
                    replaceFragemnt(fragmentArrayList.get(0));
                }else  if(rb2.getId()==checkedId){
                    replaceFragemnt(fragmentArrayList.get(1));
                }else if(rb3.getId()==checkedId){
                    replaceFragemnt(fragmentArrayList.get(2));
                }else if(rb4.getId()==checkedId){
                    replaceFragemnt(fragmentArrayList.get(3));
                }
           }
        });

    }
    private void initdata() {
        fragmentArrayList.add(new Fragment1());
        fragmentArrayList.add(new Fragment2());
        fragmentArrayList.add(new Fragment3());
        fragmentArrayList.add(new Fragment4());
    }
    private void initview() {
        vp=findViewById(R.id.vp);
        rg=findViewById(R.id.rg);
        rb1=findViewById(R.id.rb1);
        rb2=findViewById(R.id.rb2);
        rb3=findViewById(R.id.rb3);
        rb4=findViewById(R.id.rb4);
    }
    //TODO 替换Fragment的优化
    public void replaceFragemnt(Fragment fragment){
        //TODO 1:获得管理者
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        //TODO 2:开启事务
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        //TODO 3:替换功能
         //隐藏当前正在显示的fragment
            if(currentFragment!=null){
                fragmentTransaction.hide(currentFragment);
            }
        //判断要添加的fragment时候被添加过
        if(fragment.isAdded()){//被添加过
            //显示传过来
            fragmentTransaction.show(fragment);
        }else{//没有添加过
            //添加传过来的
            fragmentTransaction.add(R.id.frame_layout,fragment);
        }
        //TODO 4:提交
        fragmentTransaction.commit();
        //更新当前正在显示的Fragment
        currentFragment=fragment;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_34178710/article/details/85059375