android 使用Fragment 代替ActivityGroup

1.创建activity_main.xml

<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"
    tools:context=".MainActivity" >

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp">

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

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/station_beacons"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:text="设置"
                    android:textColor="#82858b" />
            </LinearLayout>
        </RelativeLayout>

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

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/scan_beancons"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:text="联系人"
                    android:textColor="#82858b" />
            </LinearLayout>
        </RelativeLayout>

    </LinearLayout>

</LinearLayout>

2.在Class  MainActivity extends FragmentActivity implements View.OnClickListener onCreate 方法中添加

station_beacons_layout = findViewById(R.id.station_beacons_layout);
        scan_beancons_layout = findViewById(R.id.scan_beancons_layout);

        station_beacons = (TextView) findViewById(R.id.station_beacons);
        scan_beancons = (TextView) findViewById(R.id.scan_beancons);

        station_beacons_layout.setOnClickListener(this);
        scan_beancons_layout.setOnClickListener(this);

        fragmentManager = getSupportFragmentManager();
        setTabSelection(0);


3.编写 onClick方法
 public void onClick(View v) {
        switch (v.getId()) {
            case R.id.station_beacons_layout:
                setTabSelection(0);
                break;
            case R.id.scan_beancons_layout:
                setTabSelection(1);
                break;
        }

    }


4. setTabSelection() 方法
 
<pre name="code" class="java">private void setTabSelection(int index) {
        clearSelection();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideFragments(transaction);
        switch (index) {
            case 0:
                station_beacons.setTextColor(Color.WHITE);
                if (stationBeaconsFragment == null) {
                    stationBeaconsFragment = new StationBeaconsFragment();
                    transaction.add(R.id.content, stationBeaconsFragment);
                } else {
                    transaction.show(stationBeaconsFragment);
                }
                break;
            case 1:
                scan_beancons.setTextColor(Color.WHITE);
                if (scanBeaconsFragment == null) {
                    scanBeaconsFragment = new ScanBeaconFragment();
                    transaction.add(R.id.content, scanBeaconsFragment);
                } else {
                    transaction.show(scanBeaconsFragment);
                }
                break;
        }
        transaction.commit();
    }

    private void clearSelection() {
        station_beacons.setTextColor(Color.parseColor("#82858b"));
        scan_beancons.setTextColor(Color.parseColor("#82858b"));
    }

    private void hideFragments(FragmentTransaction transaction) {
        if (stationBeaconsFragment != null) {
            transaction.hide(stationBeaconsFragment);
        }
        if (scanBeaconsFragment != null) {
            transaction.hide(scanBeaconsFragment);
        }

    }
 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值