底部RadioButton实现Fragment切换

来源:互联网 发布:安卓新闻推荐 知乎 编辑:程序博客网 时间:2024/06/07 18:30

Fragment间的切换经常使用在规模较大的项目中,下面是常见的方式:
首先新建 FragmentController.java

public class FragmentController {    int containerId;    ArrayList<Fragment> fragments;    private FragmentManager fm;    private static FragmentController controller;    private FragmentController(int containerId, Activity activity) {        this.containerId = containerId;        fm = activity.getFragmentManager();        initFragment();    }    public static FragmentController getInstance(int containerId, Activity activity) {        if (controller == null) {            controller = new FragmentController(containerId, activity);        }        return controller;    }    private void initFragment() {        fragments = new ArrayList<Fragment>();        fragments.add(new FirstFragment());        fragments.add(new SecondFragment());        fragments.add(new ThirdFragment());        FragmentTransaction ft = fm.beginTransaction();        for(Fragment fragment : fragments) {            ft.add(containerId, fragment);        }        ft.commit();    }    public void showFragment(int position) {        hideFragment();        FragmentTransaction ft = fm.beginTransaction();        ft.show(fragments.get(position));        ft.commit();    }    public void hideFragment() {        FragmentTransaction ft = fm.beginTransaction();        for(Fragment fragment : fragments) {            if(fragment != null)                ft.hide(fragment);        }        ft.commit();    }}

使用FragmentTransaction的add(),show(),hide()方法使Fragment不必在切换时重新创建,记得方法之后要提交commit()事务。

activity.main的布局:

<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:orientation="vertical" >    <FrameLayout         android:id="@+id/container"        android:layout_width="match_parent"            android:layout_height="0dp"        android:layout_weight="1"    >    </FrameLayout>    <RadioGroup         android:id="@+id/rg_button"        android:layout_height="48dp"        android:layout_width="match_parent"        android:orientation="horizontal"        android:gravity="center_vertical"        >        <RadioButton             android:id="@+id/rb_home"                            android:drawableTop="@drawable/tabbar_home"            style="@style/rb_activity_style"            android:checked="true"            android:text="首页"            />        <RadioButton             android:id="@+id/rb_message"            android:drawableTop="@drawable/tabbar_message_center"            style="@style/rb_activity_style"            android:text="消息"            />        <RadioButton             android:id="@+id/rb_discover"            android:drawableTop="@drawable/tabbar_discover"            style="@style/rb_activity_style"            android:text="发现"            />    </RadioGroup></LinearLayout>

FrameLayout就是一会儿Fragment的容器。

新建三个Fragment及其布局,这里只写第一个Fragment,另外两个是一样的:

public class FirstFragment extends Fragment{    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,            Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.first_fragment, null);        return view;    }}

布局很简单

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >            <TextView         android:layout_width="match_parent"        android:layout_height="match_parent"        android:textSize="20sp"        android:text="第一个fragment"        /></LinearLayout>

然后在MainActivity中绑定RadioButton和布局

public class MainActivity extends Activity {    FragmentController controller;    RadioGroup radioGroup;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        final FragmentController controller = FragmentController.getInstance(R.id.container, this);        controller.showFragment(0);        radioGroup = (RadioGroup) findViewById(R.id.rg_button);        radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {            @Override            public void onCheckedChanged(RadioGroup group, int checkedId) {                switch (checkedId) {                case R.id.rb_home:                    controller.showFragment(0);                    break;                case R.id.rb_message:                    controller.showFragment(1);                    break;                case R.id.rb_discover:                    controller.showFragment(2);                    break;                }            }        });    }}

默认显示第一个Fragment。

end

1 0
原创粉丝点击