Android——Fragment
Fragment 静态注册
...
<fragment
android:id="@+id/fragment_static"
android:name="com.example.study_android.fragment.StaticFragment"
android:layout_width="match_parent"
android:layout_height="60dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="主要内容" />
...
public class StaticFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_static, container, false);
}
}
Fragment 动态注册
在某些 Adapter 中返回一个一个 Fragment
public class DynamicFragment extends Fragment {
public static DynamicFragment newInstance(int position,String name, String desc) {
DynamicFragment fragment = new DynamicFragment();
Bundle args = new Bundle();
args.putInt("position", position);
args.putString("name", name);
args.putString("desc", desc);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/*
* container:fragment根据该容器计算宽高
* false:是否将该fragment添加到container容器中
* */
View view = inflater.inflate(R.layout.fragment_dynamic, container, false);
Bundle arguments = getArguments();
if (arguments != null) {
TextView name = view.findViewById(R.id.name);
TextView desc = view.findViewById(R.id.desc);
name.setText(arguments.getString("name"));
desc.setText(arguments.getString("desc"));
}
return view;
}
}
Fragment 生命周期
原文地址:https://blog.csdn.net/weixin_45932565/article/details/143268234
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!