FragmentActivity添加Fragment的使用

在一个Activity中添加动态的Fragment

public class MainActivity extends FragmentActivity implements View.OnClickListener {

    FragmentManager manager;
    FragmentTransaction transaction;
    Fragment fragment[];
    HomeFragment f1;
    ContFragment f2;
    ProblemFragmet f3;
    StowFragment f4;
    PersonalFragment f5;
    Button btn_1;
    Button btn_2;
    Button btn_3;
    Button btn_4;
    Button btn_5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_1 = (Button) findViewById(R.id.btn_one);
        btn_2 = (Button) findViewById(R.id.btn_two);
        btn_3 = (Button) findViewById(R.id.btn_three);
        btn_4 = (Button) findViewById(R.id.btn_four);
        btn_5 = (Button) findViewById(R.id.btn_five);

        btn_1.setOnClickListener(this);
        btn_2.setOnClickListener(this);
        btn_3.setOnClickListener(this);
        btn_4.setOnClickListener(this);
        btn_5.setOnClickListener(this);
        buildFragmentView();
    }

    private void buildFragmentView() {
        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        fragment = new Fragment[5];

        f1 = new HomeFragment();
        fragment[0] = f1;
        transaction.add(R.id.fra_one, fragment[0], "HomeFragment");
        transaction.commit();
    }

    public void onClick(View v) {
        transaction = manager.beginTransaction();
        switch (v.getId()) {

            case R.id.btn_one:
                btn_1.setBackgroundResource(R.mipmap.mobile_one_home_page_press_down);
                btn_2.setBackgroundResource(R.mipmap.mobile_one_content_page_button);
                btn_3.setBackgroundResource(R.mipmap.mobile_one_question_page_button);
                btn_4.setBackgroundResource(R.mipmap.mobile_one_stow_page_button);
                btn_5.setBackgroundResource(R.mipmap.mobile_one_details_page_button);
                transaction.replace(R.id.fra_one, fragment[0], "HomeFragment");
                break;
            case R.id.btn_two:

                btn_1.setBackgroundResource(R.mipmap.mobile_one_home_page_button);
                btn_2.setBackgroundResource(R.mipmap.mobile_one_content_page_press_down);
                btn_3.setBackgroundResource(R.mipmap.mobile_one_question_page_button);
                btn_4.setBackgroundResource(R.mipmap.mobile_one_stow_page_button);
                btn_5.setBackgroundResource(R.mipmap.mobile_one_details_page_button);

                if (fragment[1] == null) {
                    f2 = new ContFragment();
                    fragment[1] = f2;
                }
                transaction.replace(R.id.fra_one, fragment[1], "ContFragment");
                break;
            case R.id.btn_three:
                btn_1.setBackgroundResource(R.mipmap.mobile_one_home_page_button);
                btn_2.setBackgroundResource(R.mipmap.mobile_one_content_page_button);
                btn_3.setBackgroundResource(R.mipmap.mobile_one_question_page_press_down);
                btn_4.setBackgroundResource(R.mipmap.mobile_one_stow_page_button);
                btn_5.setBackgroundResource(R.mipmap.mobile_one_details_page_button);

                if (fragment[2] == null) {
                    f3 = new ProblemFragmet();
                    fragment[2] = f3;
                }
                transaction.replace(R.id.fra_one, fragment[2], "ProblemFragmet");
                break;
            case R.id.btn_four:


                btn_1.setBackgroundResource(R.mipmap.mobile_one_home_page_button);
                btn_2.setBackgroundResource(R.mipmap.mobile_one_content_page_button);
                btn_3.setBackgroundResource(R.mipmap.mobile_one_question_page_button);
                btn_4.setBackgroundResource(R.mipmap.mobile_one_stow_page_press_down);
                btn_5.setBackgroundResource(R.mipmap.mobile_one_details_page_button);
                if (fragment[3] == null) {
                    f4 = new StowFragment();
                    fragment[3] = f4;

                }
                transaction.replace(R.id.fra_one, fragment[3], "StowFragment");
                break;
            case R.id.btn_five:

                btn_1.setBackgroundResource(R.mipmap.mobile_one_home_page_button);
                btn_2.setBackgroundResource(R.mipmap.mobile_one_content_page_button);
                btn_3.setBackgroundResource(R.mipmap.mobile_one_question_page_button);
                btn_4.setBackgroundResource(R.mipmap.mobile_one_stow_page_button);
                btn_5.setBackgroundResource(R.mipmap.mobile_one_details_page_press_down);

                if (fragment[4] == null) {
                    f5 = new PersonalFragment();
                    fragment[4] = f5;
                }
                transaction.replace(R.id.fra_one, fragment[4], "PersonalFragment");
                break;
        }
        transaction.commit();
    }

}

布局文件

<?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:orientation="vertical"
    android:gravity="bottom"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/fra_one"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
    </FrameLayout>


        <LinearLayout

            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_margin="1dp"
            android:background="@drawable/aa"
            android:orientation="horizontal">

            <Button
                android:layout_weight="1"
                android:id="@+id/btn_one"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:background="@mipmap/sy" />

            <Button
                android:layout_weight="1"
                android:id="@+id/btn_two"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:background="@mipmap/wz" />

            <Button
                android:layout_weight="1"
                android:id="@+id/btn_three"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:background="@mipmap/wt" />

            <Button
                android:layout_weight="1"
                android:id="@+id/btn_four"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:background="@mipmap/dx" />

            <Button
                android:layout_weight="1"
                android:id="@+id/btn_five"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:background="@mipmap/gr" />
        </LinearLayout>


</LinearLayout>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值