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);
}
}
版权声明:本文为fjut1145171018原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。