Fragment基础及进阶

1.什么是Fragment?

Fragment是Android3.0后引入的一个新的API,他出现的初衷是为了适应大屏幕的平板电脑, 当然现在他仍然是平板APP UI设计的宠儿,而且我们普通手机开发也会加入这个Fragment, 我们可以把他看成一个小型的Activity,又称Activity片段!想想,如果一个很大的界面,我们 就一个布局,写起界面来会有多麻烦,而且如果组件多的话是管理起来也很麻烦!而使用Fragment 我们可以把屏幕划分成几块,然后进行分组,进行一个模块化的管理!从而可以更加方便的在 运行过程中动态地更新Activity的用户界面!另外Fragment并不能单独使用,他需要嵌套在Activity 中使用,尽管他拥有自己的生命周期,但是还是会受到宿主Activity的生命周期的影响,比如Activity 被destory销毁了,他也会跟着销毁!

2.Fragment的生命周期

1.onAttach() :Fragment与Activity有联系。
2.onCreate():创建Fragment
3.onCreateView():创建Fragment视图,尽量不要做耗时操作
4.onActivityCreated():当Activity中的onCreate方法执行完后调用。
5.onStart():启动。
6.onResume():可见
7.onPause():不可见
8.onStop():停止。
9. onDestroyView() :销毁Fragment视图
10.onDestroy():销毁fragment对象
11.onDetach():Fragment和Activity解除关联的时候调用

3.加载方式&使用步骤

加载方式有两种
一种是静态加载:

创建Fragment

public class MyFragment extends Fragment {
    public MyFragment() {
        // Required empty public constructor
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_my, container, false);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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.MyFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <fragment
        android:id="@+id/my_fragment_id"
        android:name="com.example.day004.fragment.MyFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </fragment>

</LinearLayout>

一种是动态加载:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.main_layout_id,new MyFragment());
        fragmentTransaction.commit();
    }
}

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
         MyFragment myFragment = new MyFragment();
        fragmentTransaction.add(R.id.main_layout_id,myFragment);
        //移除
        fragmentTransaction.remove(myFragment);
        OneFragment oneFragment = new OneFragment();  fragmentTransaction.replace(R.id.main_layout_id,oneFragment);
        //隐藏
        fragmentTransaction.hide(oneFragment);
        fragmentTransaction.commit();
    }
}

4.传值

activity 给 fragment传值

Activity

public class Activity extends AppCompatActivity {
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    private EditText editText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a_2_f);

        //0,取到edit的控件和值
        editText =  findViewById(R.id.et_id);

        //1,在这里动态添加一个fragment
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        //注意这个布局文件,是R.layout.activity_a_2_f xml文件里的线性布局
        fragmentTransaction.add(R.id.linear_layout_id,new ShowContextFragment());
        fragmentTransaction.commit();
    }

    public void click(View view) {
        //取到输入的值
        String string = editText.getText().toString();
        //创建fragment对象
        ShowContextFragment showContextFragment = new ShowContextFragment();
        //创建bundle
        Bundle bundle = new Bundle();
        bundle.putString("key",string);
        //给fragment对象赋值
        showContextFragment.setArguments(bundle);

        //动态修改fragment
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.linear_layout_id,showContextFragment);
        fragmentTransaction.commit();
    }
}

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".A2FActivity">

    <EditText
        android:id="@+id/et_id"
        android:hint="MSG"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send"
        android:onClick="click"
        />

    <LinearLayout
        android:orientation="horizontal"
        android:id="@+id/linear_layout_id"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </LinearLayout>

</LinearLayout>

Fragment

public class ShowContextFragment extends Fragment {

    private TextView textView;
    public ShowContextFragment() {
        // Required empty public constructor
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_show_context, container, false);

        textView = inflate.findViewById(R.id.context_tv_id);
   
        Bundle arguments = getArguments();
        if(arguments != null){
            String key = arguments.getString("key");
            textView.setText(key);
        }
        return inflate;
    }

}

布局中只有一个TextView显示区域,显示传过来的数据内容

未完待续…

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值