Fragment基础知识
Fragment 介绍
答:Fragment是Android3.0后引入的一个新的API,他出现的初衷是为了适应大屏幕的平板电脑, 当然现在他仍然是平板APP UI设计的宠儿,而且我们普通手机开发也会加入这个Fragment, 我们可以把他看成一个小型的Activity,又称Activity片段!想想,如果一个很大的界面,我们 就一个布局,写起界面来会有多麻烦,而且如果组件多的话是管理起来也很麻烦!而使用Fragment 我们可以把屏幕划分成几块,然后进行分组,进行一个模块化的管理!从而可以更加方便的在 运行过程中动态地更新Activity的用户界面!另外Fragment并不能单独使用,他需要嵌套在Activity 中使用,尽管他拥有自己的生命周期,但是还是会受到宿主Activity的生命周期的影响,比如Activity 被destory销毁了,他也会跟着销毁!。
Fragment为什么被称为第五大组件?
Fragment的使用次数是不输于其他四大组件的,而且Fragment有自己的生命周期,比Activity更加节省内存。
Fragment的优势有以下几点:
模块化(Modularity):我们不必把所有代码全部写在Activity中,而是把代码写在各自的Fragment中。
可重用(Reusability):多个Activity可以重用一个Fragment。
可适配(Adaptability):根据硬件的屏幕尺寸、屏幕方向,能够方便地实现不同的布局,这样用户体验更好。
Fragment 应用
如何创建Fragment
创建Fragment对象:(右击新建Fragment一步搞定)
加载Fragment的两种方式
静态加载
自动生成的java文档
/**
* A simple {@link Fragment} subclass.
*/
public class FirstFragment extends Fragment {
private static final String TAG = "FirstFragment";
public FirstFragment() {
// 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_first, container, false);
}
自动生成的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:gravity="center"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!--一定要注意的是:name属性是fragment的全限定名-->
<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>
以上是静态加载内容.
动态加载
FragmentManager 介绍
为了兼容的效果,我们选择getSupportFragmentManager()方法.
getFragmentManager()方法已经废弃.
1.一定要注意,如果用getSupportFragmentManager()这个方法,就整个项目全部用,
2.不要在项目里交叉使用getFragmentManager()方法.
3.否则你得请神仙来找错了.
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();
// 第一个参数是布局文件的id
// 第二个参数是碎片
fragmentTransaction.add(R.id.fl,new FirstFragment());
// 把事务提交
fragmentTransaction.commit();
}
add,remove,replace,hide 方法
package com.example.day004;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.example.day004.fragment.MyFragment;
import com.example.day004.fragment.OneFragment;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//1,创建fragment的管理对象
FragmentManager supportFragmentManager = getSupportFragmentManager();
//2,获取fragment的事物对象,并开启事务
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
//3,调用事务中相应的方法,来操作fragment
//add方法参数,第一个要放入的容器(布局的Id),第二个是fragment对象
MyFragment myFragment = new MyFragment();
fragmentTransaction.add(R.id.main_layout_id,myFragment);
//移除一个Fragment
fragmentTransaction.remove(myFragment);
OneFragment oneFragment = new OneFragment(); //实例化一个Fragment对象
//replace(替换一个布局)执行过程是先 remove 然后在 add.
fragmentTransaction.replace(R.id.main_layout_id,oneFragment);
//隐藏一个Fragment
fragmentTransaction.hide(oneFragment);
//4,提交事务
fragmentTransaction.commit();
}
}
Fragment的生命周期
文字描述版本的生命周期.
Fragment 传值介绍
Activity布局文件
<?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"
tools:context=".ShowActivity"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入点啥"
android:id="@+id/edittext"
android:layout_marginTop="10dp"
android:background="@android:drawable/edit_text"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交"
android:layout_marginTop="10dp"
android:id="@+id/button"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/textview"
android:layout_marginTop="10dp"></FrameLayout>
</LinearLayout>
Activity代码
public class ShowActivity extends AppCompatActivity {
private EditText edittext;
private Button button;
private FrameLayout textview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
//0,取到edit的控件和值
edittext = findViewById(R.id.edittext);
button = findViewById(R.id.button);
textview = findViewById(R.id.textview);
//1,在这里动态添加一个fragment
FragmentManager supportFragmentManager = getSupportFragmentManager();
final FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
//注意这个布局文件,是R.layout.activity_a_2_f xml文件里的线性布局
fragmentTransaction.add(R.id.textview,new ShowFragment());
fragmentTransaction.commit();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager supportFragmentManager1 = getSupportFragmentManager();
FragmentTransaction fragmentTransaction1 = supportFragmentManager1.beginTransaction();
//创建fragment对象
ShowFragment showFragment = new ShowFragment();
//创建bundle
Bundle bundle = new Bundle();
bundle.putString("name",edittext.getText().toString().trim());
//给fragment对象赋值
showFragment.setArguments(bundle);
//动态修改fragment
fragmentTransaction1.replace(R.id.textview,showFragment);
fragmentTransaction1.commit();
}
});
}
Fragment布局文件
<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=".ShowFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment"
android:id="@+id/textview1111"/>
</FrameLayout>
Fragment布局文件中添加一个id即可
Fragment代码
/**
* A simple {@link Fragment} subclass.
*/
public class ShowFragment extends Fragment {
public ShowFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflate = inflater.inflate(R.layout.fragment_show, container, false);
TextView textView = inflate.findViewById(R.id.textview1111);
Bundle arguments = getArguments();
if (arguments!=null){
String name = arguments.getString("name");
textView.setText(name);
}
return inflate;
}
}