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方法提交,要不然你的所有操作都不会生效,切记.

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

(0)

相关推荐

  • Android Fragment(动态,静态)碎片详解及总结

    Android Fragment(动态,静态)碎片详解 一.Fragment的相关概念(一)Fragment的基础知识 Fragment是Android3.0新增的概念,中文意思是碎片,它与Activity十分相似,用来在一个 Activity中描述一些行为或一部分用户界面.使用多个Fragment可以在一个单独的Activity中建 立多个UI面板,也可以在多个Activity中使用Fragment. Fragment拥有自己的生命 周期和接收.处理用户的事件,这样就不必在Activity写一

  • Android动态添加碎片代码实例

    碎片的创建 要使用碎片先要创建一个碎片,创建一个碎片很简单. 1.新建一个碎片布局,fragment.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" and

  • Android开源框架的SlidingFragment的使用示例

    效果如下: 直接上代码,留着以后用,代码目录结构如下: 其中BaseFragment.java是另外5个Fragment的基类,代码如下: package com.xuliugen.newsclient.fragment.base; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; impor

  • 实例讲解Android Fragment的两种使用方法

    一.第一种方法: (1)Fragment的第一种使用方法是使用fragment加载单独的布局文件:(也就是xml的方式实现) 结构如下: activity_main.xml主要是在一个线性布局中添加两个线性布局 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" androi

  • Android中碎片的使用方法详解

    Fragment的使用 其实碎片很简单,但是网上胡乱充数的博文太多了,以至于我们有时候觉得比较乱,今天就来简单讲解一下碎片的使用. 碎片的使用分为两种,静态添加碎片和动态添加碎片,我们就先来看一下静态添加碎片如何实现. 静态添加碎片 首先,先建两个Layout文件,这就是碎片的布局文件,大家可能也发现了,Android Studio里面可以直接快速建立碎片,就像Activity一样,但是这样会生成很多没用的代码,所以我们还是选择自己创建碎片布局. 两个布局都很简单,里面只有一个居中显示的Text

  • Android 中RxPermissions 的使用方法详解

    Android 中RxPermissions 的使用方法详解 以请求拍照.读取位置权限为例 module的build.gradle: compile 'com.tbruyelle.rxpermissions2:rxpermissions:0.9.4@aar' compile 'io.reactivex.rxjava2:rxjava:2.0.5' AndroidManifest.xml: <uses-permission android:name="android.permission.AC

  • Android中XUtils3框架使用方法详解(一)

    xUtils简介 xUtils 包含了很多实用的android工具. xUtils 支持大文件上传,更全面的http请求协议支持(10种谓词),拥有更加灵活的ORM,更多的事件注解支持且不受混淆影响... xUitls 最低兼容android 2.2 (api level 8) 今天给大家带来XUtils3的基本介绍,本文章的案例都是基于XUtils3的API语法进行的演示.相信大家对这个框架也都了解过, 下面简单介绍下XUtils3的一些基本知识. XUtils3一共有4大功能:注解模块,网络

  • Android 中Context的使用方法详解

    Android 中Context的使用方法详解 概要: Context字面意思是上下文,位于framework package的android.content.Context中,其实该类为LONG型,类似Win32中的Handle句柄.很多方法需要通过 Context才能识别调用者的实例:比如说Toast的第一个参数就是Context,一般在Activity中我们直接用this代替,代表调用者的实例为Activity,而到了一个button的onClick(View view)等方法时,我们用t

  • Android通过json向MySQL中读写数据的方法详解【读取篇】

    本文实例讲述了Android通过json向MySQL中读取数据的方法.分享给大家供大家参考,具体如下: 首先 要定义几个解析json的方法parseJsonMulti,代码如下: private void parseJsonMulti(String strResult) { try { Log.v("strResult11","strResult11="+strResult); int index=strResult.indexOf("[");

  • Android通过json向MySQL中读写数据的方法详解【写入篇】

    本文实例讲述了Android通过json向MySQL中写入数据的方法.分享给大家供大家参考,具体如下: 先说一下如何通过json将Android程序中的数据上传到MySQL中: 首先定义一个类JSONParser.Java类,将json上传数据的方法封装好,可以直接在主程序中调用该类,代码如下 public class JSONParser { static InputStream is = null; static JSONObject jObj = null; static String j

  • Android 中读取Excel文件实例详解

    Android 中读取Excel文件实例详解 最近有个需求需要在app内置数据,新来的产品扔给了我两个Excel表格就不管了(两个表格格式还不统一...),于是通过度娘等方法找到了Android中读取Excel表格文件的一种方法,记录一下. 闲话一下Excel中工作簿和工作表的区别: 工作簿中包含有工作表.工作簿可以由一张或多张工作表组成,一个工作簿就是一个EXCEL表格文件. 好了,开始读取表格文件吧. 前提 首先,我们假设需要读取的表格文件名字为test.xls, 位于assets根目录下.

  • Android 中 Tweened animation的实例详解

    Android 中 Tweened animation的实例详解 Tweened animation有四种类型,下面主要介绍Scale类型. 运行效果如下: Android SDK提供了2种方法:直接从XML资源中读取Animation,使用Animation子类的构造函数来初始化Animation对象,第二种方法在看了Android SDK中各个类的说明就知道如何使用了,下面简要说明从XML资源中读取Animation.XML资源中的动画文件animation.xml内容为: <?xml ve

  • Android 中RecyclerView顶部刷新实现详解

    Android 中RecyclerView顶部刷新实现详解 1. RecyclerView顶部刷新的原理 RecyclerView顶部刷新的实现通常都是在RecyclerView外部再包裹一层布局.在这个外层布局中,还包含一个自定义的View,作为顶部刷新时的指示View.也就是说,外层布局中包含两个child,一个顶部刷新View,一个RecyclerView,顶部刷新View默认是隐藏不可见的.在外层布局中对滑动事件进行处理,当RecyclerView滑动到顶部并继续下滑的时候,根据滑动的距

  • Android中mvp模式使用实例详解

    MVP 是从经典的模式MVC演变而来,它们的基本思想有相通的地方:Controller/Presenter负责逻辑的处理,Model提供数据,View负 责显示.作为一种新的模式,MVP与MVC有着一个重大的区别:在MVP中View并不直接使用Model,它们之间的通信是通过Presenter (MVC中的Controller)来进行的,所有的交互都发生在Presenter内部,而在MVC中View会从直接Model中读取数据而不是通过 Controller. 在MVC里,View是可以直接访问

随机推荐