Fragment页面的切换

        在android日常开发中,经常会用到点击底部导航栏,然后多个Fragment互相切换的功能。之前做毕设的时候用到了这个功能,今天有空整理了一下,并且做了androidStudio的demo,供大家一起讨论学习。闲话不多说,直接上图,大约就是这个样子(欢迎转载,请说明来处)Demo下载地址:http://download.csdn.net/detail/sinat_27681957/9492952



主文件用来控制Ftagment的切换:

public class MainActivity extends AppCompatActivity {
    private Fragment[] mFragments;
    private RadioGroup bottomRg;
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        init();
    }

    private void init(){
        mFragments = new Fragment[3];
        fragmentManager = getSupportFragmentManager();
        mFragments[0] = fragmentManager.findFragmentById(R.id.fragment_main);
        mFragments[1] = fragmentManager.findFragmentById(R.id.fragment_bbs);
        mFragments[2] = fragmentManager.findFragmentById(R.id.fragment_user);
        fragmentTransaction = fragmentManager.beginTransaction()
                .hide(mFragments[0]).hide(mFragments[1]).hide(mFragments[2]);
        fragmentTransaction.show(mFragments[0]).commit();
        setFragmentIndicator();
    }

    //切换fragment
    private void setFragmentIndicator() {

        bottomRg = (RadioGroup) findViewById(R.id.bottomRg);
        bottomRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                fragmentTransaction = fragmentManager.beginTransaction()
                        .hide(mFragments[0]).hide(mFragments[1])
                        .hide(mFragments[2]);
                switch (checkedId) {
                    case R.id.rbOne:
                        fragmentTransaction.show(mFragments[0]).commit();
                        Log.e("1", "1");
                        break;

                    case R.id.rbTwo:
                        fragmentTransaction.show(mFragments[1]).commit();
                        Log.e("2", "2");
                        break;

                    case R.id.rbThree:
                        fragmentTransaction.show(mFragments[2]).commit();
                        Log.e("3", "3");
                        break;

                    default:
                        break;
                }
            }
        });
    }
}
主布局文件里面放置3个fragment:

<?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"
    android:orientation="vertical" >

<!-- 上边主页面 -->

<fragment
    android:id="@+id/fragment_main"
    android:name="com.lei.doctorofhand.fragment.MainFragment"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="20" />

<fragment
android:id="@+id/fragment_bbs"
android:name="com.lei.doctorofhand.fragment.HomeFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="20" />

<fragment
android:id="@+id/fragment_user"
android:name="com.lei.doctorofhand.fragment.UserFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="20" />

    <!-- 分割线 -->
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="@color/bgcolor"/>

    <!-- 底部菜单页面 -->
<RadioGroup
android:id="@+id/bottomRg"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:background="@color/footer_bg_normal"
android:orientation="horizontal" >

<RadioButton
    android:id="@+id/rbOne"
    style="@style/rg_btn_style"
    android:checked="true"
    android:text="首页"
    android:textColor="@color/black"
    android:drawableTop="@drawable/rb_one_btn_selector"/>

<RadioButton
    android:id="@+id/rbTwo"
    style="@style/rg_btn_style"
    android:text="房间"
    android:textColor="@color/black"
    android:drawableTop="@drawable/rb_two_btn_selector"/>

<RadioButton
    android:id="@+id/rbThree"
    style="@style/rg_btn_style"
    android:text="设置"
    android:textColor="@color/black"
    android:drawableTop="@drawable/rb_three_btn_selector"/>
</RadioGroup>

</LinearLayout>
MainFragment:

public class MainFragment extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_main,container,false);
        return view;
    }

HomeFragment:

public class HomeFragment extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_home, container, false);
        return view;

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }
    private void initView() {

    }
}
UserFragment:

public class UserFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        
        return inflater.inflate(R.layout.fragment_user, container, false);
    }

}
fragment_main.xml:

<?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"
    android:background="@color/bgwhite"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="首页"/>

</LinearLayout>
Fragment_home.xml:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/bgwhite"
    android:orientation="vertical"
    tools:context="com.askdoctor.ui.FragmentBBS" >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="房间"/>
</LinearLayout>
Fragment_user.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/bgwhite"
    android:orientation="vertical" >

    
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="用户"/>

</RelativeLayout>

好了差不多了,Demo下载地址:http://download.csdn.net/detail/sinat_27681957/9492952



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值