系统会在片断首次绘制其界面时调用此方法。如要为您的片断绘制界面,您今后方法中返回的 View 必须是片断布局的根视图。若是片断未提供界面,您能够返回 null。android
在Activity的layout文件中声明Fragment,须要特别注意的是 中的android: name属性指定了在layout中实例化的Fragment类bash
每一个片断都须要惟一标识符,重启 Activity 时,系统可以使用该标识符来恢复片断(您也可使用该标识符来捕获片断,从而执行某些事务,如将其移除)。能够经过两种方式为片断提供 ID:app
activity_fragment.xmlide
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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学习.FragmentActivity">
<!--静态加载必须指定id或tag-->
<fragment
android:id="@+id/ft_load_static"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.fr.myapplication.Fragment学习.BlankFragment"/>
</androidx.constraintlayout.widget.ConstraintLayout>
复制代码
fragment_blank.xml布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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学习.BlankFragment">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/app_name" />
<Button
android:id="@+id/bt_click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
复制代码
FragmentActivity学习
public class FragmentActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
final TextView textView = findViewById(R.id.textView);
Button button = findViewById(R.id.bt_click);
button.setText("点击");
button.setOnClickListener(new View.OnClickListener() {
@SuppressLint("SetTextI18n")
@Override
public void onClick(View v) {
textView.setText("Activity内点击");
}
});
}
}
复制代码
BlankFragmentui
public class BlankFragment extends Fragment {
public BlankFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//layout布局文件转换成View对象
/*
Inflate the layout for this fragment
resource: Fragment须要加载的布局文件
root: 加载layout的父ViewGroup
attachToRoot: false, 不返回父ViewGroup
*/
View view = inflater.inflate(R.layout.fragment_blank, container, false);
TextView textView = view.findViewById(R.id.textView);
textView.setText("静态加载Fragment");
return view;
}
}
复制代码
使用代码将Fragment添加到一个Activity layout中this
根据用户的交互状况,对Fragment进行添加、移除、替换,以及执行其余动做,提交给Activity的每一套变化被称做一个事务。spa
注意:每一个事务都是您想要同时执行的一组更改。可使用 add()、remove() 和 replace() 等方法,为给定事务设置您想要执行的全部更改。而后,如要将事务应用到 Activity,必须调用 commit()code
若是容许用户经过Back按键返回到前一个Fragment状态,调用commit()以前能够加入addToBackStack()方法
动态加载Fragment只需在Activity中写以下代码便可:
//须要动态加入的Fragment
MoveFragment moveFragment = new MoveFragment();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.add(R.id.frame, moveFragment);
transaction.addToBackStack(null);
transaction.commit();
复制代码
Activity中代码
MoveFragment moveFragment = new MoveFragment();
Bundle bundle = new Bundle();
bundle.putString("name","Activity向Fragment传递数据");
moveFragment.setArguments(bundle);
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.add(R.id.frame,moveFragment,"fragment_move");
transaction.addToBackStack(null);
transaction.commit();
复制代码
Fragment中代码
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//layout布局文件转换成View对象
/*
Inflate the layout for this fragment
resource: Fragment须要加载的布局文件
root: 加载layout的父ViewGroup
attachToRoot: false, 不返回父ViewGroup
*/
View view = inflater.inflate(R.layout.fragment_blank, container, false);
TextView textView = view.findViewById(R.id.textView);
String text = getArguments() != null ? getArguments().getString("name") : null;
textView.setText(text);
return view;
}
复制代码
调用FragmentManager的findFragmentById()或findFragmentByTag()方法获取Fragment
Activity中代码
FragmentManager fm = getSupportFragmentManager();
Fragment fragmentById = fm.findFragmentById(R.id.ft_load_static);
BlankFragment fragment = (BlankFragment) fragmentById;
fragment.setTransfer("Activity向静态加载的Fragment发送消息");
复制代码
Fragment中代码
private String transfer;
public String getTransfer() {
return transfer;
}
public void setTransfer(String transfer) {
this.transfer = transfer;
}
view.findViewById(R.id.receive).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "Fragment接收消息:" + getTransfer(), Toast.LENGTH_SHORT).show();
}
});
复制代码
1.Fragment中定义接口
public interface MyListener{
void send(String text);
}
复制代码
2.onAttach中
@Override
public void onAttach(@NonNull Context context) {
listener = (MyListener) context;
super.onAttach(context);
}
复制代码
3.onCreateView中调用接口发送消息
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_blank, container, false);
listener.send("Fragment向Activity传递数据");
return view;
}
复制代码
实现Fragment中定义的接口来接收数据
@Override
public void send(String text) {
Toast.makeText(this,"来自Fragment:"+ text,Toast.LENGTH_SHORT).show();
}
复制代码