Fragment使用(上)

<ImageView

android:id=“@+id/iv_back”

android:layout_width=“35dp”

android:layout_height=“35dp”

android:paddingLeft=“15dp”

android:layout_centerVertical=“true”

android:scaleType=“centerCrop”

android:src=“@drawable/arrow_back_black”

/>

<TextView

android:id=“@+id/tv_title”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_centerInParent=“true”

android:text=“我是标题”

android:textSize=“20sp”/>

ContentFragment

public class ContentFragment extends Fragment {

@Nullable

@Override

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

return inflater.inflate(R.layout.fragment_content, null);

}

}

布局文件:fragment_content

<?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=“我是内容”

android:textSize=“30sp”/>

Test1FragmentActivity

/**

  • @author songzi522

  • 静态使用Fragment

  • 步骤:

  • 1、继承Fragment,重写onCreateView()回调方法,设置Fragment的布局

  • 2、在Activity中声明Fragment,使用方式和View相同

  • 实现案例效果:两个Fragment构成Activity的布局,一个标题Fragment,一个内容Fragment

*/

public class Test1FragmentActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.activity_test1_fragment);

}

}

布局文件:

<?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 savedInstanceState) {

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.g 《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》无偿开源 徽信搜索公众号【编程进阶路】 etId()) {

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-----”);

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值