Android中碎片的使用方法详解

 更新时间:2019年06月20日 10:29:05   作者:Kepler  
这篇文章主要介绍了Android中碎片的使用方法详解,其实碎片很简单,但是网上胡乱充数的博文太多了,以至于我们有时候觉得比较乱,今天就来简单讲解一下碎片的使用,需要的朋友可以参考下

Fragment的使用

其实碎片很简单,但是网上胡乱充数的博文太多了,以至于我们有时候觉得比较乱,今天就来简单讲解一下碎片的使用.
碎片的使用分为两种,静态添加碎片和动态添加碎片,我们就先来看一下静态添加碎片如何实现.

静态添加碎片

首先,先建两个Layout文件,这就是碎片的布局文件,大家可能也发现了,Android Studio里面可以直接快速建立碎片,就像Activity一样,但是这样会生成很多没用的代码,所以我们还是选择自己创建碎片布局.

两个布局都很简单,里面只有一个居中显示的TextView,下面贴一下代码.

第一个布局文件:fragment_first.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_light"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="This is the first fragment!"/>
</LinearLayout>

第二个布局文件fragment_second.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_green_light"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="This is the second fragment!"/>
</LinearLayout>

现在布局文件建完了,我们该建立他们对应的Fragment了,也就是后台代码了。新建两个类,分别叫FirstFragment和SecondFragment,都继承于Fragment,需要注意一点,我们教程里面所使用的Fragment全都是android.support.v4.app.Fragment这个包下的,这样更有利于程序的兼容性.

贴一下两个类的代码,也很简单,只是重写了onCreateView方法来加载不同的布局文件.

public class FirstFragment extends Fragment {
private View view;//得到碎片对应的布局文件,方便后续使用
//记住一定要重写onCreateView方法
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_first, container, false);//得到对应的布局文件
return view;
}
}
public class SecondFragment extends Fragment {
private View view;//得到碎片对应的布局文件,方便后续使用
//记住一定要重写onCreateView方法
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_second, container, false);//得到对应的布局文件
return view;
}
}

好,基本的工作我们做完了,现在我们用两个Activity展示如何静态添加碎片和动态添加碎片.

静态添加控件的话,需要使用fragment控件,指定其名称是你刚才创建的Fragment就可以,让我们来看一下.

先贴一下第一个Activity的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="me.worldskills.android.testfragment.MainActivity">
<fragment
android:id="@+id/main_firstfragment"
android:name="me.worldskills.android.testfragment.FirstFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></fragment>
<fragment
android:id="@+id/main_secondfragment"
android:name="me.worldskills.android.testfragment.SecondFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></fragment>
<Button
android:id="@+id/main_btnGoNext"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Go to next page"
android:textAllCaps="false"/>
</LinearLayout>


其中那个按钮是用来跳转到下一个界面的,也就是动态添加碎片案例的Activity,在这里可以忽略.

这里我们看见了,两个fragment分别指定name为FirstFragment和SecondFragment,也就是你刚才创建的两个Fragment,一定要记得加上包名.对了,还有一个问题,就是这样的话是没有预览的,如果想要预览,需要在fragment标签中加上一句代码:

Tools:layout="@layout/布局文件名称"  .

好了,静态添加碎片就完成了,什么?就这么简单,对啊...就这么简单.

动态添加碎片

动态添加碎片我们就不需要用fragment控件了,而是需要用个FrameLayout控件,这是为什么呢,首先我们都知道FrameLayout中的控件,都是从左上角开始显示,不用进行位置控制,动态添加碎片其实就是向容器里面动态添加碎片,而fragment控件只能用来静态绑定一个碎片.

先贴一下第二个Activity的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_second"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/second_btnLoadFirst"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Load First!"
android:textAllCaps="false"/>
<Button
android:id="@+id/second_btnLoadSecond"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Load Second!"
android:textAllCaps="false"/>
<FrameLayout
android:id="@+id/second_fl"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>

上面的两个按钮用来加载不同的碎片,而下面的FrameLayout就是碎片显示的容器.

废话不多说,贴代码:

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);

//点击第一个按钮的时候加载第一个碎片
findViewById(R.id.second_btnLoadFirst).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.second_fl, new FirstFragment());
transaction.commit();
}
});
//点击第二个按钮的时候加载第二个碎片
findViewById(R.id.second_btnLoadSecond).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getSupportFragmentManager().beginTransaction().replace(R.id.second_fl, new SecondFragment()).commit();//简写
}
});
}
}

getSupportFragmentManager方法用来获得一个碎片管理器对象(使用这个方法的时候注意是android.support.v4.app包下的哦),然后通过这个方法开始一个碎片事物对象,这个对象比较关键,可以用来动态添加碎片,调用它的replace方法,会把指定容器里面的其他控件全部清除掉,然后添加新的碎片进去.在这里就是先把R.id.second_f1里面的控件清空,然后添加传入一个FirstFragment进去.

替换完之后一定要记得调用commit方法提交,要不然你的所有操作都不会生效,切记.

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android Studio下无线调试的方法

    Android Studio下无线调试的方法

    这篇文章主要为大家详细介绍了Android Studio平台下无线调试的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • Android Notification通知解析

    Android Notification通知解析

    这篇文章主要针对Android Notification通知进行解析,本文主要介绍的是notification通知的使用方法,感兴趣的小伙伴们可以参考一下
    2016-01-01
  • Android应用内存泄漏优化指南

    Android应用内存泄漏优化指南

    内存泄漏(Memory Leak)是指应用中不再使用的对象因错误引用无法被垃圾回收(GC),导致内存占用持续增长,最终可能引发 OOM(Out Of Memory)崩溃 或 应用卡顿,以下是 Android 内存泄漏的优化方案,涵盖检测工具、常见场景及解决方案,需要的朋友可以参考下
    2025-03-03
  • Android编写2048小游戏

    Android编写2048小游戏

    这篇文章主要为大家详细介绍了利用Android编写一个2048小游戏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-11-11
  • Flutter软键盘的原理浅析

    Flutter软键盘的原理浅析

    大家应该都知道目前Flutter官方是没有自定义键盘的解决方案,下面这篇文章主要给大家介绍了关于Flutter软键盘原理的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2021-10-10
  • Android Recyclerview实现水平分页GridView效果示例

    Android Recyclerview实现水平分页GridView效果示例

    本篇文章主要介绍了Android Recyclerview实现水平分页GridView效果示例,具有一定的参考价值,有兴趣的可以了解一下
    2017-08-08
  • Android实现下拉放大图片松手自动反弹效果

    Android实现下拉放大图片松手自动反弹效果

    这篇文章主要为大家详细介绍了Android实现下拉放大图片松手自动反弹效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-03-03
  • Android快速分析apk工具aapt的使用教程

    Android快速分析apk工具aapt的使用教程

    这篇文章主要介绍了Android快速分析apk工具aapt的使用教程,本文讲解了什么是aapt、主要用法、使用aapt、查看apk的基本信息、查看基本信息、查看应用权限等内容,需要的朋友可以参考下
    2015-04-04
  • Android如何防止apk程序被反编译(尊重劳动成果)

    Android如何防止apk程序被反编译(尊重劳动成果)

    作为Android应用开发者,不得不面对一个尴尬的局面,就是自己辛辛苦苦开发的应用可以被别人很轻易的就反编译出来,天下痛苦之事莫过于此啊,本文会介绍一种防止apk程序被反编译的方法,感兴趣的朋友可以了解下哦
    2013-01-01
  • Android实现复制Assets文件到SD卡

    Android实现复制Assets文件到SD卡

    这篇文章主要为大家详细介绍了Android实现复制Assets文件到SD卡,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-12-12

最新评论