Fragment+Activity之间相互切换

来源:互联网 发布:mysql in or 编辑:程序博客网 时间:2024/05/24 05:38

今天接触到Fragment碎片之间的切换,做个了简易的实现。

activity_main.xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="#E8E8E8"    android:orientation="vertical"    tools:context=".MainActivity" >    <FrameLayout        android:id="@+id/fl_contains"        android:layout_width="fill_parent"        android:layout_height="0dp"        android:layout_weight="1" >    </FrameLayout>    <RadioGroup        android:id="@+id/rg"        android:layout_width="fill_parent"        android:layout_height="60dp"        android:background="#FFFFFF"        android:orientation="horizontal" >        <RadioButton            android:id="@+id/rb_menu"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:button="@null"            android:drawableTop="@drawable/fragment1_selector"            android:gravity="center_horizontal"            android:paddingTop="5dp"            android:text="菜单"            android:textColor="#030303" />        <View            android:layout_width="1dip"            android:layout_height="match_parent"            android:layout_gravity="center_horizontal"            android:background="#E8E8E8" />        <RadioButton            android:id="@+id/rb_find"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:button="@null"            android:drawableTop="@drawable/fragment2_selector"            android:gravity="center_horizontal"            android:paddingTop="5dp"            android:text="发现"            android:textColor="#030303" />         <View            android:layout_width="1dip"            android:layout_height="match_parent"            android:layout_gravity="center_horizontal"            android:background="#E8E8E8" />          <RadioButton            android:id="@+id/rb_smart_service"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:button="@null"            android:drawableTop="@drawable/fragment3_selector"            android:gravity="center_horizontal"            android:paddingTop="5dp"            android:text="服务"            android:textColor="#030303" />           <View            android:layout_width="1dip"            android:layout_height="match_parent"            android:layout_gravity="center_horizontal"            android:background="#E8E8E8" />            <RadioButton            android:id="@+id/rb_my"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:button="@null"            android:drawableTop="@drawable/fragment4_selector"            android:gravity="center_horizontal"            android:paddingTop="5dp"            android:text="我的"            android:textColor="#030303" />    </RadioGroup></LinearLayout>

上面的布局主要在MainActivity展示Fragment切换界面效果,背景为fragment1_selector主要是实现点击按钮后切换图片,方便用户观看当前的功能。
在res文件夹下新建一个drawable文件夹新建fragment1_selector.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/icon_function_press"></item>    <item android:drawable="@drawable/icon_function"></item></selector>

MainActivity.java代码如下:

import com.example.fragment.Fragment1;import com.example.fragment.Fragment2;import com.example.fragment.Fragment3;import com.example.fragment.Fragment4;import com.example.fragmenttest.R;import android.os.Bundle;import android.content.Context;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentTransaction;import android.view.Menu;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;public class MainActivity extends FragmentActivity  implements OnCheckedChangeListener{    private FragmentManager fm;    private FragmentTransaction beginTransaction;    private RadioGroup rg;   //定义按钮组    private RadioButton rb;  //定义按钮    Context context; //    Fragment1 fragment1;    Fragment2 fragment2;    Fragment3 fragment3;    Fragment4 fragment4;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        rg = (RadioGroup) findViewById(R.id.rg);        rg.setOnCheckedChangeListener(this);        rb = (RadioButton) findViewById(R.id.rb_menu);        rb.setChecked(true); // 第一个菜单默认被选择    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    @Override    public void onCheckedChanged(RadioGroup group, int checkedId) {        // TODO Auto-generated method stub        FragmentManager fragmentManager = this.getSupportFragmentManager();        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();        /***** 1.隐藏所有的Fragment ****/        if (fragment1 != null) {            fragmentTransaction.hide(fragment1);        }        if (fragment2 != null) {            fragmentTransaction.hide(fragment2);        }        if (fragment3 != null) {            fragmentTransaction.hide(fragment3);        }        if (fragment4 != null) {            fragmentTransaction.hide(fragment4);        }        /***** 2.根据用户选择的id值,显示某个Fragement *****/        switch (checkedId) {        case R.id.rb_menu:  //点击《菜单》按钮            if (fragment1 == null) {                fragment1 = new Fragment1();                fragmentTransaction.add(R.id.fl_contains, fragment1);            } else {                fragmentTransaction.show(fragment1);            }            break;        case R.id.rb_find:  //《发现》按钮            if (fragment2 == null) {                fragment2 = new Fragment2();                fragmentTransaction.add(R.id.fl_contains, fragment2);            } else {                fragmentTransaction.show(fragment2);            }            break;        case R.id.rb_smart_service: //《智能服务》按钮            if (fragment3 == null) {                fragment3 = new Fragment3();                fragmentTransaction.add(R.id.fl_contains, fragment3);            } else {                fragmentTransaction.show(fragment3);            }            break;        case R.id.rb_my:    //《我的》按钮            if (fragment4 == null) {                fragment4 = new Fragment4();                fragmentTransaction.add(R.id.fl_contains, fragment4);            } else {                fragmentTransaction.show(fragment4);            }            break;        }        fragmentTransaction.commit();    }}

定义一个Fragment1继承于android.support.v4.app.Fragment;

package com.example.fragment;import com.example.fragmenttest.R;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class Fragment1 extends Fragment {    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.fragment1, container, false);        return view;    };}

这里只列举一个Fragment1文件,剩余三个大同小异。

fragment1.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:orientation="vertical"     android:background="#FFF5EE">    <TextView         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Fragment1"        android:textSize="35dp"        android:layout_gravity="center_horizontal"        /></LinearLayout>

以上就是实现Fragment之间相互切换的详细步骤
源码下载

1 0
原创粉丝点击