Android基础----Fragment

简介:

Fragment(碎片)最初是为了大屏幕的平板设备而设计出来的一款控件,他必须”嵌入”Activity中进行使用,一个Activity可以包含多个Fragment,而一个Fragment也可以被多个Activity复用。当在大屏幕设备上时,可以在一个Activity中同时显示多个Fragment,通过多个Fragment的配合来使得交互更加的友好。

基本用法:

  1. 自定义一个Fragment,继承自Fragment
  2. 重写Fragment中的需要重写的方法,加载布局
  3. 在Activity中加载Fragment

MyFragment.java

public class MyFragment extends Fragment {
    View rootView;
    TextView textView;

    /**
     * 当Fragment创建时被调用
     * @param savedInstanceState
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    /**
     * 当Fragment的视图布局创建时调用
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //加载定义好的Fragment布局文件
        rootView = inflater.inflate(R.layout.fragment_main, container, false);
        //获取布局文件中的控件
        textView = (TextView) rootView.findViewById(R.id.tv);
        //返回布局视图
        return rootView;
    }
}

fragment_main.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">
    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="hello world"
        android:gravity="center"/>
</LinearLayout>

在Activity中加载Fragment的方式有多种,首先介绍一种实际很常用的ViewPager+Fragment方式。

加载方式一:
  1. 在Activity的布局文件中使用ViewPager控件,并在Java代码中进行初始化
  2. 将所有要加载的Fragment放入一个列表中
  3. 为ViewPager添加适配器,并将Fragment的列表作为数据传入

activity_main.xml

<?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">

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpanger"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v4.view.ViewPager>

</RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {
    //声明一个ViewPager
    ViewPager mViewPager;
    //声明要添加的Fragment
    MyFragment myFragment;
    MyFragment secondMyFragment;
    MyFragment thridMyFragment;
    //用于保存Fragment的list
    List<Fragment> contentsList = new ArrayList<>();
    //适配器
    FragmentPagerAdapter adapter;

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mViewPager = (ViewPager) findViewById(R.id.viewpanger);
        myFragment = new MyFragment();
        secondMyFragment = new MyFragment();
        thridMyFragment = new MyFragment();

        //将所有初始化后的Fragment都添加到list中
        contentsList.add(myFragment);
        contentsList.add(secondMyFragment);
        contentsList.add(thridMyFragment);

        //为ViewPager设置适配器
        adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public android.support.v4.app.Fragment getItem(int position) {
                return contentsList.get(position);
            }

            @Override
            public int getCount() {
                return contentsList.size();
            }
        };
        mViewPager.setAdapter(adapter);

    }
}

这样一个ViewPager+Fragment的滑动视图就做好了,可以再加上ViewPager的指示器。做成完整的一套app的主界面布局

方法二:
  1. 在Activity的布局文件中为Fragment设置容器
  2. 在Java代码中获取到fragment的容器布局,并通过FragmentTransaction(Fragment事物管理)来进行Fragment的添加替换删除等操作

activity_second.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
</FrameLayout>

SecondActivity.java

public class SecondActivity extends Activity {
    Fragment fragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        fragment = new MyFragment();

        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.fragment_container, fragment);
        fragmentTransaction.commit();

    }
}

这样就把一个Fragment添加到了Activity中

Fragment生命周期:

因为Fragment是放在Activity中使用的控件,所以其生命周期和Activity有所关联。同时在使用Fragment时要注意其所处Activity当前的生命周期状态,因为虽然fragment在Activity中,但是有时候有些第三方控件不能在onCreateView()中初始化,必须要等在Fragment所在的Activity初识加载完毕时才能初始化。

Fragment和Activity的生命周期对比图:

声明周期对比图

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值