}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".fragment.Test1FragmentActivity">
<fragment
android:id="@+id/fragment_title"
android:layout_width=“match_parent”
android:layout_height=“50dp”
android:name=“com.gs.common3.fragment.TitleFragment”
/>
<fragment
android:id="@+id/fragment_content"
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:layout_below="@+id/fragment_title"
android:name=“com.gs.common3.fragment.ContentFragment”
/>
1.1.2、总结
1.2、Fragment动态应用
代码如下:
/**
-
演示Fragment的动态使用
-
-
案例效果:在Activity界面中有两个Fragment 标题和内容
*/
public class Test2FragmentActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test2_fragment);
//1.创建Fragment的管理器对象;
FragmentManager manager = getFragmentManager();
//2.获取Fragment的事务对象并开启事务;
FragmentTransaction transaction = manager.beginTransaction();
//3.调用事务中相应的动态操作Fragment的方法执行;
transaction.add(R.id.title_layout, new TitleFragment());
transaction.add(R.id.content_layout, new ContentFragment());
//4.提交事务;
transaction.commit();
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".fragment.Test2FragmentActivity">
<LinearLayout
android:id="@+id/title_layout"
android:layout_width=“match_parent”
android:layout_height=“50dp”
android:orientation=“vertical”>
<LinearLayout
android:id="@+id/content_layout"
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:orientation=“vertical”>
2、Fragment动态切换
==============
ShopRankFragment
public class ShopRankFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceSt
ate) {
return inflater.inflate(R.layout.fragment_shop_rank,null);
}
}
<?xml version="1.0" encoding="utf-8"?><RelativeLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
android:id="@+id/rl_content"
android:layout_width=“match_parent”
android:layout_height=“match_parent”>
<TextView
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:gravity=“center”
android:text="@string/home_shop"
android:textSize=“30sp”/>
ShareFragment
public class ShareFragment extends Fragment {
@SuppressLint(“InflateParams”)
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_share, null);
}
}
<?xml version="1.0" encoding="utf-8"?><RelativeLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
android:id="@+id/rl_content"
android:layout_width=“match_parent”
android:layout_height=“match_parent”>
<TextView
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:gravity=“center”
android:text="@string/home_share"
android:textSize=“30sp”/>
GiftFragment
public class GiftFragment extends Fragment {
@SuppressLint(“InflateParams”)
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_gift, null);
}
}
<?xml version="1.0" encoding="utf-8"?><RelativeLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
android:id="@+id/rl_content"
android:layout_width=“match_parent”
android:layout_height=“match_parent”>
<TextView
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:gravity=“center”
android:text="@string/home_gift"
android:textSize=“30sp”/>
OrderFragment
public class OrderFragment extends Fragment {
@SuppressLint(“InflateParams”)
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_order, null);
}
}
<?xml version="1.0" encoding="utf-8"?><RelativeLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
android:id="@+id/rl_content"
android:layout_width=“match_parent”
android:layout_height=“match_parent”>
<TextView
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:gravity=“center”
android:text="@string/home_order"
android:textSize=“30sp”/>
Test3FragmentActivity
public class Test3FragmentActivity extends AppCompatActivity implements View.OnClickListener {
private FragmentManager manager;
private FragmentTransaction transaction;
@SuppressLint(“CommitTransaction”)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test3_fragment);
initViews();
manager = getFragmentManager();
transaction = manager.beginTransaction();
transaction.add(R.id.content_layout, new ShopRankFragment());
transaction.commit();
}
private void initViews() {
RadioButton rbShopRank = findViewById(R.id.rb_shop_rank);
RadioButton rbShare = findViewById(R.id.rb_share);
RadioButton rbGift = findViewById(R.id.rb_gift);
RadioButton rbOrder = findViewById(R.id.rb_order);
rbShopRank.setOnClickListener(this);
rbShare.setOnClickListener(this);
rbGift.setOnClickListener(this);
rbOrder.setOnClickListener(this);
}
@SuppressLint(“NonConstantResourceId”)
@Override
public void onClick(View v) {
transaction = manager.beginTransaction();
switch (v.getId()) {
case R.id.rb_shop_rank:
transaction.replace(R.id.content_layout,new ShopRankFragment());
break;
case R.id.rb_share:
transaction.replace(R.id.content_layout,new ShareFragment());
break;
case R.id.rb_gift:
transaction.replace(R.id.content_layout,new GiftFragment());
break;
case R.id.rb_order:
transaction.replace(R.id.content_layout,new OrderFragment());
break;
default:
break;
}
transaction.commit();
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".fragment.Test3FragmentActivity">
<LinearLayout
android:id="@+id/content_layout"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:orientation=“vertical”>
<LinearLayout
android:id="@+id/bottom_layout"
android:orientation=“horizontal”
android:layout_width=“match_parent”
android:layout_height=“120dp”
android:background="#ffffff"
android:layout_alignParentBottom=“true”>
<RadioGroup
android:id="@+id/rg_home"
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:orientation=“horizontal”>
<RadioButton
android:id="@+id/rb_shop_rank"
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:layout_gravity=“center”
android:button="@null"
android:drawableTop="@mipmap/ic_launcher"
android:drawablePadding=“10dp”
android:gravity=“center”
android:text="@string/home_shop"
android:textColor="#B3B3B3"
android:textSize=“15sp” />
<RadioButton
android:id="@+id/rb_share"
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:layout_gravity=“center”
android:button="@null"
android:drawableTop="@mipmap/ic_launcher"
android:drawablePadding=“10dp”
android:gravity=“center”
android:text="@string/home_share"
android:textColor="#B3B3B3"
android:textSize=“15sp” />
<RadioButton
android:id="@+id/rb_gift"
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:layout_gravity=“center”
android:button="@null"
android:drawableTop="@mipmap/ic_launcher"
android:drawablePadding=“10dp”
android:gravity=“center”
android:text="@string/home_gift"
android:textColor="#B3B3B3"
android:textSize=“15sp” />
<RadioButton
android:id="@+id/rb_order"
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:layout_gravity=“center”
android:button="@null"
android:drawableTop="@mipmap/ic_launcher"
android:drawablePadding=“10dp”
android:gravity=“center”
android:text="@string/home_order"
android:textColor="#B3B3B3"
android:textSize=“15sp” />
3、Fragment和Activity的生命周期关联对比
============================
3.1、代码实例:
3.1.1、Test4FragmentActivity
public class Test4FragmentActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = “Test_LifeCycle”;
private FragmentManager manager;
private FragmentTransaction transaction;
@SuppressLint(“CommitTransaction”)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test4_fragment);
Log.i(TAG, “-----Test4FragmentActivity-----onCreate-----”);
manager = getFragmentManager();
transaction = manager.beginTransaction();
transaction.add(R.id.content_layout, new HotSpotFragment());
transaction.commit();
findViewById(R.id.tv_hot_spot).setOnClickListener(this);
findViewById(R.id.tv_top_line).setOnClickListener(this);
}
@SuppressLint(“NonConstantResourceId”)
@Override
public void onClick(View v) {
transaction = manager.beginTransaction();
switch (v.getId()) {
case R.id.tv_hot_spot:
transaction.replace(R.id.content_layout, new HotSpotFragment());
break;
case R.id.tv_top_line:
transaction.replace(R.id.content_layout, new TopLineFragment());
break;
default:
break;
}
transaction.commit();
}
/**
- Activity能够被用户看到时调用
*/
@Override
protected void onStart() {
super.onStart();
Log.i(TAG, “-----Test4FragmentActivity-----onStart-----”);
}
/**
- Activity能够获取用户焦点时调用
*/
@Override
protected void onResume() {
super.onResume();
Log.i(TAG, “-----Test4FragmentActivity-----onResume-----”);
}
/**
- Activity失去用户焦点时调用
*/
@Override
protected void onPause() {
super.onPause();
Log.i(TAG, “-----Test4FragmentActivity-----onPause-----”);
}
/**
- Activity完全被遮挡时调用
*/
@Override
protected void onStop() {
super.onStop();
Log.i(TAG, “-----Test4FragmentActivity-----onStop-----”);
}
/**
- Activity处于停止状态,重新被用户使用 时调用
*/
@Override
protected void onRestart() {
super.onRestart();
Log.i(TAG, “-----Test4FragmentActivity-----onRestart-----”);
}
/**
- Activity被销毁 时调用
*/
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG, “-----Test4FragmentActivity-----onDestroy-----”);
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout
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”
tools:context=".fragment.test4.Test4FragmentActivity">
<LinearLayout
android:id="@+id/top_layout"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:orientation=“horizontal”>
<TextView
android:id="@+id/tv_hot_spot"
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:text="@string/hot_spot"
android:gravity=“center”
android:textSize=“30sp”/>
<TextView
android:id="@+id/tv_top_line"
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:text="@string/top_line"
android:gravity=“center”
android:textSize=“30sp”/>
<LinearLayout
android:id="@+id/content_layout"
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:orientation=“vertical”
android:layout_below="@+id/top_layout"/>
3.1.2、HotSpotFragment
public class HotSpotFragment extends Fragment {
private static final String TAG = “Test_LifeCycle”;
ctivity">
<LinearLayout
android:id="@+id/top_layout"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:orientation=“horizontal”>
<TextView
android:id="@+id/tv_hot_spot"
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:text="@string/hot_spot"
android:gravity=“center”
android:textSize=“30sp”/>
<TextView
android:id="@+id/tv_top_line"
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:text="@string/top_line"
android:gravity=“center”
android:textSize=“30sp”/>
<LinearLayout
android:id="@+id/content_layout"
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:orientation=“vertical”
android:layout_below="@+id/top_layout"/>
3.1.2、HotSpotFragment
public class HotSpotFragment extends Fragment {
private static final String TAG = “Test_LifeCycle”;