Fragment实现Tab页面切换

FragmentTestActivity.java
package com.yaxon.superhappygo.learn.fragment;

import android.os.Bundle;
import android.view.View;

import com.yaxon.superhappygo.R;

import java.util.ArrayList;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;

public class FragmentTestActivity extends FragmentActivity implements View.OnClickListener {
    FragmentA fragmentA;
    FragmentB fragmentB;
    FragmentC fragmentC;
    ArrayList<Fragment> FragmentList = new ArrayList();
    private int mFlag = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_fragment);
        findViewById(R.id.btnA).setOnClickListener(this);
        findViewById(R.id.btnB).setOnClickListener(this);
        findViewById(R.id.btnC).setOnClickListener(this);

        mFlag = 1;
        if (fragmentA == null) {
            fragmentA = new FragmentA();
        }
        showFragment(fragmentA);

    }

    public void showFragment(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();

        for (Fragment fragme : FragmentList) {
            fragmentManager.beginTransaction().remove(fragme).commit();
        }
        fragmentManager.beginTransaction()
                .add(R.id.frameLayout, fragment)
                .commit();
        FragmentList.add(fragment);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnA:
                if (mFlag != 1) {
                    mFlag = 1;
                    if (fragmentA == null) {
                        fragmentA = new FragmentA();
                    }
                    showFragment(fragmentA);
                }
                break;
            case R.id.btnB:
                if (mFlag != 2) {
                    mFlag = 2;
                    if (fragmentB == null) {
                        fragmentB = new FragmentB();
                    }
                    showFragment(fragmentB);
                }
                break;
            case R.id.btnC:
                if (mFlag != 3) {
                    mFlag = 3;
                    if (fragmentC == null) {
                        fragmentC = new FragmentC();
                    }
                    showFragment(fragmentC);
                }
                break;
            default:
                break;
        }

    }
}
activity_fragment.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">

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@color/colorAccent"
        >
    </FrameLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_vertical">

        <Button
            android:id="@+id/btnA"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:textColor="#ffffff"
            android:textSize="16sp"
            android:text="FragmentA"
            android:layout_weight="1">
        </Button>

        <Button
            android:id="@+id/btnB"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="#FF0000"
            android:textColor="#ffffff"
            android:textSize="16sp"
            android:text="FragmentB"
            android:layout_weight="1">
        </Button>

        <Button
            android:id="@+id/btnC"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="#16A8EF"
            android:textColor="#ffffff"
            android:textSize="16sp"
            android:text="FragmentC"
            android:layout_weight="1">
        </Button>
    </LinearLayout>
</LinearLayout>
FragmentA.java
package com.yaxon.superhappygo.learn.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class FragmentA extends Fragment {
    private int count;
    public static final String TAG = "FragmentA";
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.i(TAG, "onCreateView count="+count);
        //return super.onCreateView(inflater, container, savedInstanceState);
        TextView tv = new TextView(inflater.getContext());
        tv.setText("FragmentA");
        tv.setTextColor(Color.parseColor("#ff0000"));
        tv.setWidth(100);
        tv.setHeight(50);
        tv.setTextSize(20);
        return tv;

    }

    @Override
    public void onResume() {
        super.onResume();
        count++;
        Log.i(TAG, "onResume count="+count);
    }

    @Override
    public void onPause() {
        super.onPause();
        count++;
        Log.i(TAG, "onPause count="+count);
    }

    @Override
    public void onStop() {
        super.onStop();
        count++;
        Log.i(TAG, "onStop count="+count);
    }
}
FragmentB.java
package com.yaxon.superhappygo.learn.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class FragmentB extends Fragment {
    private int count;
    public static final String TAG = "FragmentB";
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.i(TAG, "onCreateView count="+count);
        //return super.onCreateView(inflater, container, savedInstanceState);
        TextView tv = new TextView(inflater.getContext());
        tv.setText("FragmentB");
        tv.setTextColor(Color.parseColor("#ff0000"));
        tv.setWidth(100);
        tv.setHeight(50);
        tv.setTextSize(20);
        return tv;

    }

    @Override
    public void onResume() {
        super.onResume();
        count++;
        Log.i(TAG, "onResume count="+count);
    }

    @Override
    public void onPause() {
        super.onPause();
        count++;
        Log.i(TAG, "onPause count="+count);
    }

    @Override
    public void onStop() {
        super.onStop();
        count++;
        Log.i(TAG, "onStop count="+count);
    }
}
FragmentC.java
package com.yaxon.superhappygo.learn.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class FragmentC extends Fragment {
    private int count;
    public static final String TAG = "FragmentC";
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.i(TAG, "onCreateView count="+count);
        //return super.onCreateView(inflater, container, savedInstanceState);
        TextView tv = new TextView(inflater.getContext());
        tv.setText("FragmentC");
        tv.setTextColor(Color.parseColor("#ff0000"));
        tv.setWidth(100);
        tv.setHeight(50);
        tv.setTextSize(20);
        return tv;

    }

    @Override
    public void onResume() {
        super.onResume();
        count++;
        Log.i(TAG, "onResume count="+count);
    }

    @Override
    public void onPause() {
        super.onPause();
        count++;
        Log.i(TAG, "onPause count="+count);
    }

    @Override
    public void onStop() {
        super.onStop();
        count++;
        Log.i(TAG, "onStop count="+count);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值