Android - Fragment(二)加载Fragment

简介: Fragment加载方法 加载方法有两种,在xml文件中注册,或者是在Java代码中加载。 xml中注册 例如在fragment_demo.xml中定义 1 2 6 7 13 14 20 21 com.

Fragment加载方法

加载方法有两种,在xml文件中注册,或者是在Java代码中加载。

xml中注册

例如在fragment_demo.xml中定义

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6 
 7     <fragment
 8         android:id="@+id/main_fragment_up"
 9         android:name="com.rust.fragment.FirstFragment"
10         android:layout_width="match_parent"
11         android:layout_height="0dp"
12         android:layout_weight="1" />
13 
14     <fragment
15         android:id="@+id/main_fragment_bottom"
16         android:name="com.rust.fragment.SecondFragment"
17         android:layout_width="match_parent"
18         android:layout_height="0dp"
19         android:layout_weight="1" />
20 
21 </LinearLayout>

com.rust.fragment.SecondFragment 就是Fragment子类

在 SecondFragment.java 里复写onCreateView方法,并返回定义好的view

activity中直接加载即可

setContentView(R.layout.fragment_demo);

Java代码中加载

① 准备好Fragment xml布局文件

② 新建一个类,继承自Fragment;在这个类中找到Fragment布局文件

③ 在Activity中使用FragmentManager来操作Fragment

④ 别忘了commit

先自定义一个布局文件 fragment_first.xml 

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout 
 3 xmlns:android="http://schemas.android.com/apk/res/android"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="vertical"
 7     android:background="#0011ff" >
 8 
 9 <!-- <Button 
10     android:id="@+id/btn_fragment1_1"
11     android:layout_width="wrap_content" 
12     android:layout_height="wrap_content"
13     android:text="@string/btn_fragment1"
14     android:textSize="16sp"
15        />
16 <EditText
17     /> -->
18 
19 </LinearLayout>

新建一个类 FirstFragment.java ,继承自Fragment。复写onCreateView方法。在onCreateView方法中,可以操作Fragment上的控件。

 1 @Override
 2     public View onCreateView(LayoutInflater inflater, ViewGroup container,    Bundle savedInstanceState) {
 3         View rootView = inflater.inflate(R.layout.fragment_first, container,false);
 4 
 5 //    fragment_first是自定义好的布局
 6 
 7 //    如果此Fragment上放了控件,比如Button,Edittext等。可以在这里定义动作
 8 
 9         btn_fragment1_send = (Button) rootView.findViewById(R.id.btn_fragment1_1);
10 
11 //...
12         return rootView;
13     }

准备一个位置给Fragment,比如在 activity_main.xml 中用Framelayout来占位。

1     <FrameLayout
2 
3         android:id="@+id/layout_container1"
4         android:layout_width="match_parent"
5         android:layout_height="0dp"
6         android:layout_weight="4" >
7 
8     </FrameLayout>

在 MainActivity.java 里,先获得FragmentManager,得到FragmentTransaction。Fragment的添加删除等操作由FragmentTransaction来完成。

f1 = new FirstFragment();    //    获取实例

f2 = new SecondFragment();    //

FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();

fragmentTransaction.add(R.id.layout_container1,f1);    //    添加

fragmentTransaction.replace(R.id.layout_container1,f1);    //    替换

// 或者也可以写成

fragmentTransaction.replace(R.id.layout_container1,new FirstFragment());

//                fragmentTransaction.addToBackStack(null);    //添加到返回栈,这样按返回键的时候能返回已添加的fragment

fragmentTransaction.commit();    //别忘了commit

//    移除操作 getFragmentManager().beginTransaction().remove(f1).commit();

相比与xml中注册,代码加载更为灵活一些。个人较为喜欢动态加载。

 

目录
相关文章
|
XML Android开发 数据格式
Android -- Fragment动态注册
Android -- Fragment动态注册
278 0
|
API Android开发 数据安全/隐私保护
解决android webview 加载http url 失败 net::ERR_CLEARTEXT_NOT_PERMITTED 错误
解决android webview 加载http url 失败 net::ERR_CLEARTEXT_NOT_PERMITTED 错误
2916 0
|
Java Android开发
Android面试题经典之Glide取消加载以及线程池优化
Glide通过生命周期管理在`onStop`时暂停请求,`onDestroy`时取消请求,减少资源浪费。在`EngineJob`和`DecodeJob`中使用`cancel`方法标记任务并中断数据获取。当网络请求被取消时,`HttpUrlFetcher`的`cancel`方法设置标志,之后的数据获取会返回`null`,中断加载流程。Glide还使用定制的线程池,如AnimationExecutor、diskCacheExecutor、sourceExecutor和newUnlimitedSourceExecutor,其中某些禁止网络访问,并根据CPU核心数动态调整线程数。
520 2
|
12月前
|
Android开发 开发者 容器
android FragmentManager 删除所有Fragment 重建
通过本文,我们详细介绍了如何使用 `FragmentManager`删除所有Fragment并重建。通过理解和应用这些步骤,可以在实际开发中更灵活地管理Fragment,满足各种应用场景的需求。希望本文能帮助开发者更好地掌握Fragment管理技巧,提高应用开发效率和代码质量。
246 8
|
Android开发 UED
Android 中加载 Gif 动画
【10月更文挑战第20天】加载 Gif 动画是 Android 开发中的一项重要技能。通过使用第三方库或自定义实现,可以方便地在应用中展示生动的 Gif 动画。在实际应用中,需要根据具体情况进行合理选择和优化,以确保用户体验和性能的平衡。可以通过不断的实践和探索,进一步掌握在 Android 中加载 Gif 动画的技巧和方法,为开发高质量的 Android 应用提供支持。
|
缓存 前端开发 Android开发
Android实战之如何截取Activity或者Fragment的内容?
本文首发于公众号“AntDream”,介绍了如何在Android中截取Activity或Fragment的屏幕内容并保存为图片。包括截取整个Activity、特定控件或区域的方法,以及处理包含RecyclerView的复杂情况。
301 3
|
Android开发
Android基础知识:什么是Fragment?与Activity的区别是什么?
Android基础知识:什么是Fragment?与Activity的区别是什么?
3129 54
|
存储 缓存 Java
Android项目架构设计问题之优化业务接口数据的加载效率如何解决
Android项目架构设计问题之优化业务接口数据的加载效率如何解决
176 0
|
Java Android开发 Kotlin
Android项目架构设计问题之要在Glide库中加载网络图片到ImageView如何解决
Android项目架构设计问题之要在Glide库中加载网络图片到ImageView如何解决
200 0
|
XML API 开发工具
Android Bitmap 加载与像素操作
Android Bitmap 加载与像素操作
178 2

热门文章

最新文章