1、Fragments

Fragment是Activity中用户界面的一个行为或者是一部分,你可以在一个单独的Activity上把多个Fragment组合成为一个多区域的UI,并且可以在多个Activity中再次使用。可以认为Fragment是Activity的一个模块零件,他有自己的生命周期,接受他自己的输入事件,并且可以在Activity运行时添加或者删除。

两个概念:

Fragment、宿主

fragment的生命周期只接受其宿主activity的生命周期的影响。例如,一旦activity被暂停,它里面所有的fragment也会被暂停,一旦activity被销毁,它里面的所有fragment也会被销毁。

2、创建Fragment

要创建一个fragment,必须创建一个fragment的子类(或是继承自他的子类),fragment类的代码看起来很像activity,他与activity一样都有回调函数,例如onCreate,onStart,onPause和onStop,事实上,如果你正在讲一个现成的Android应用转而使用Fragment来实现,可以将代码从activity的回调函数移植到各自的fragment中。

除了基类fragment,还有几个可能会继承的子类:

DialogFragment、ListFragment、PreferenceFragment

3、添加用户界面

fragment常被用作activity用户界面的一部分,并且将本身的布局够见到activity中去。

将fragment添加到activity有两种方式:

1)在activity的布局文件里声明fragment

activity_main.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:orientation="horizontal"> <fragment
android:id="@+id/fragTitle"
android:name="com.wzh.pushdemo.fragment.TitleFragment"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1" /> <fragment
android:id="@+id/fragContent"
android:name="com.wzh.pushdemo.fragment.ContentFragment"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="3" /> </LinearLayout>

2)通过编码将fragment添加到已存在的ViewGroup中

<?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:orientation="horizontal"> <fragment
android:id="@+id/fragTitle"
android:name="com.wzh.pushdemo.fragment.TitleFragment"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1" /> <FrameLayout
android:id="@+id/framLayout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3">
</FrameLayout> </LinearLayout>
public class MainActivity extends Activity {

    TitleFragment titleFragment;
ContentFragment contentFragment;
FrameLayout framLayout; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //通过fragmentManager(Fragment管理器)获取fragment实例
// titleFragment = (TitleFragment) getFragmentManager().findFragmentById(R.id.fragTitle);
// contentFragment = (ContentFragment) getFragmentManager().findFragmentById(R.id.fragContent); framLayout = (FrameLayout) findViewById(R.id.framLayout);
/**
* 通过代码添加Fragment
*/
FragmentManager fm = getFragmentManager();
//开启一个事务
FragmentTransaction ft = fm.beginTransaction(); contentFragment = new ContentFragment(); //添加Fragment
ft.add(R.id.framLayout, contentFragment);
// ft.remove(); //删除
// ft.replace();//替换 //提交事务
ft.commit();
}
}
Fragment家族常用的API Fragment常用的三个类:
android.app.Fragment    主要用于定义Fragment
android.app.FragmentManager 主要用于在Activity中操作
Fragment android.app.FragmentTransaction 保证一些列Fragment操作的原子性,熟悉事务这个词,一定能明白~
a、获取FragmentManage的方式: getFragmentManager() // v4中,getSupportFragmentManager
b、主要的操作都是FragmentTransaction的方法 FragmentTransaction transaction = fm.benginTransatcion();//开启一个事务
  • transaction.add()   往Activity中添加一个
  • Fragment transaction.remove()  从Activity中移除一个Fragment,如果被移除的Fragment没有添加到回退栈,这个Fragment实例将会被销毁。
  • transaction.replace()  使用另一个Fragment替换当前的,实际上就是remove()然后add()的合体
  • transaction.hide()  隐藏当前的Fragment,仅仅是设为不可见,并不会销毁,
  • transaction.show() 显示之前隐藏的Fragment
  • detach()
    会将view从UI中移除,和remove()不同,此时fragment的状态依然由FragmentManager维护。
  • attach()
    重建view视图,附加到UI上并显示。
  • transatcion.commit()
    提交一个事务
commit方法一定要在Activity.onSaveInstance()之前调用。

4、管理Fragments

想要管理activity中的fragment,可以使用FragmentManager,可以通过在activity中调用getFragmentManager()获得。

使用FragmentManger可以做如下事情,包括:

(1)使用findFragmentById()(用于在activity不居中提供有界面的fragment)或者findFragmentByTag()获取activity中存在的fragment(用于有界面或者没有界面的fragment)

(2)使用popBackStack()(模仿用户的back命令)从后台栈弹出fragment

(3)使用addOnBackStackChangedListener()注册一个监听后台栈变化的监听器

public class PopBackActivity extends AppCompatActivity {

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pop_back);
} public void oneClick(View view) {
PopBackFragment p1 = new PopBackFragment("one");
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content, p1);
//把当前fragment添加到Activity栈
ft.addToBackStack(null);
ft.commit(); } public void twoClick(View view) {
//用构造方法传值,有问题
PopBackFragment p1 = new PopBackFragment("two");
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content, p1);
//把当前fragment添加到Activity栈
ft.addToBackStack(null);
ft.commit();
} @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (getFragmentManager().getBackStackEntryCount() == 0) {
finish();
} else {
getFragmentManager().popBackStack();
}
}
return true;
}
}
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="oneClick"
android:text="one" /> <Button
android:id="@+id/button2"
android:layout_width="88dp"
android:layout_height="48dp"
android:onClick="twoClick"
android:text="two" /> <FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="72dp"> </FrameLayout>
</LinearLayout>
public class PopBackFragment extends Fragment {
private String title; public PopBackFragment() {
super();
} public PopBackFragment(String title) {
this.title = title;
} @Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_pop_back, null);
TextView textView = (TextView) view.findViewById(R.id.text);
textView.setText(title);
return view;
}
}

5.Fragment的传参方式:

    public static Fragment newInstance(String arg) {
TitleFragment titleFragment1 = new TitleFragment();
Bundle bundle = new Bundle();
bundle.putString("info", arg);
titleFragment1.setArguments(bundle);
return titleFragment1;
}

例如:

    public void twoClick(View view) {
//用构造方法传值,有问题,在横竖屏切换时数据会丢失,不建议使用
// PopBackFragment p1 = new PopBackFragment("two");
//建议使用
PopBackFragment p1 = PopBackFragment.getInstance("two");
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content, p1);
//把当前fragment添加到Activity栈
ft.addToBackStack(null);
ft.commit();
}
PopBackFragment
    /**
* Fragmen的传参方法
* @param title
* @return
*/
public static PopBackFragment getInstance(String title) {
PopBackFragment p = new PopBackFragment();
Bundle bundle = new Bundle();
bundle.putString("title", title);
p.setArguments(bundle);
return p;
} @Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_pop_back, null);
TextView textView = (TextView) view.findViewById(R.id.text);
textView.setText(getArguments().getString("title"));
return view;
}

Fragment与Activity交互

fragment可以通过getActivity()函数访问Activity,并且很容易的执行类似于查找activity布局中的视图任务:

View listView = getActivity().findViewById(R.id.list);

activity能够调用fragment的函数findFragmentById()或者findFragmentByTag(),从FragmentManager中获取Fragment

   TitleFragment fragment = (TitleFragment) getFragmentManager().findFragmentById(R.id.fragTitle);

fragment与activity共享事件
一个好方法是fragment内部定义一个回调接口,并需要宿主activity实现它。
当activity通过接口接收到回调时,可以在必要时与布局中的其他fragment共享信息。

宿主Activity:

public class MainActivity extends Activity implements TitleFragment.TitleListener {

    private TitleFragment titleFragment;
private ContentFragment contentFragment; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); titleFragment = (TitleFragment) getFragmentManager().findFragmentById(R.id.fragTitle);
contentFragment = (ContentFragment) getFragmentManager().findFragmentById(R.id.fragContent);
} @Override
public void changeValue(String value) {
contentFragment.change(value);
}
}

activity_main.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:orientation="horizontal"> <fragment
android:id="@+id/fragTitle"
android:name="com.wzh.pushdemo.fragment.TitleFragment"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1" /> <fragment
android:id="@+id/fragContent"
android:name="com.wzh.pushdemo.fragment.ContentFragment"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="3" />
</LinearLayout>

触发事件的frag

public class TitleFragment extends Fragment implements View.OnClickListener {

    private TitleListener titleListener;
private Button btnTime;
private Button btnSpace; @Override
public void onAttach(Activity activity) {
super.onAttach(activity);
titleListener = (TitleListener) activity;
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_title, container, false);
btnTime = (Button) view.findViewById(R.id.btnTime);
btnSpace = (Button) view.findViewById(R.id.btnSpace);
btnTime.setOnClickListener(this);
btnSpace.setOnClickListener(this);
return view;
} @Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnTime:
titleListener.changeValue("时间");
break;
case R.id.btnSpace:
titleListener.changeValue("地点");
break;
} } //定义一个回调接口,宿主要实现这个接口
public static interface TitleListener {
public void changeValue(String value);
}
}
frag_title.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="#00ff00"
android:orientation="vertical"
android:padding="10dp"> <Button
android:id="@+id/btnTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="时间" /> <Button
android:id="@+id/btnSpace"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="地点" /> </LinearLayout>

要改变值的frag

public class ContentFragment extends Fragment {
private TextView tvValue; @Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_content, container, false);
tvValue = (TextView) view.findViewById(R.id.tvValue);
return view;
} public void change(String value) {
tvValue.setText(value);
}
}

frag_content.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="#00ddff"
android:orientation="vertical"> <TextView
android:id="@+id/tvValue"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="30dp"
android:text="Hello" /> </LinearLayout>

6、PreferenceFragment

有时候,我们的程序需要提供一些选项功能,能让用户去定制化自己的是用风格。例如,允许用户是否自动保存登录信息,允许用户设定某个页面的刷新时间等等,我们可以使用PreferenceActivity基类去显示给用户一个选项设置的界面,在Android3.0或更高版本上,可以使用PreferenceFragment类去实现这个功能。
下面将展示如何常见和使用PreferenceFragment:
(1)在res文件夹下新疆一个xml文件夹,在xml文件夹下面新建一个文件:preference.xml
(2)在包路径下面新建一个类:Fragment继承PreferenceFragment
(3)从xml文件加载选项addPreferencesFromResource(R.xml.preferences);

7、Fragment懒加载

如果ViewPager的每个Fragment都会拉取网络数据加载,而Fragment默认是加载前两个界面的,这样有可能会出现网络丢包或者网络堵塞情况,所以事先懒加载就有必要了。

实现懒加载的重点是public void setUserVisibleHint(boolean isVisibleToUser)方法,这个方法会优先于onCreate()方法的,即在调用onCreate前,isVisibleToUser已经获取到了,isVisibleToUser为true时表示页面用户能看到,false时看不到,之后可以在用户能看到页面时进行数据加载,不提前进行加载就可以了

@Override
public void onStart() {
super.onStart();
Log.d("TAG", mTagName + " onStart()"); ... if(getUserVisibleHint()) {
pullData();
} }

Fragment以及懒加载的更多相关文章

  1. Android之Viewpager+Fragment实现懒加载

    我们在做应用开发的时候,一个Activity里面可能会以viewpager(或其他容器)与多个Fragment来组合使用.而ViewPager默认会缓存三页数据,即:Viewpager每加载一个Fra ...

  2. Andriod开发技巧——Fragment的懒加载

    我们在做应用开发的时候,一个Activity里面可能会以viewpager(或其他容器)与多个Fragment来组合使用,而如果每个 fragment都需要去加载数据,或从本地加载,或从网络加载,那么 ...

  3. Android ViewPager Fragment使用懒加载提升性能

     Android ViewPager Fragment使用懒加载提升性能 Fragment在如今的Android开发中越来越普遍,但是当ViewPager结合Fragment时候,由于Androi ...

  4. Fragment的懒加载

    我们在做应用开发的时候,一个Activity里面可能会以viewpager(或其他容器)与多个Fragment来组合使用,而如果每个fragment都需要去加载数据,或从本地加载,或从网络加载,那么在 ...

  5. 关于Fragment的懒加载问题

    为了达到界面效果,我们有时需要使用到TabLayout+ViewPager的方式来布局界面,然而ViewPager的adapter总是默认把与当前可见的fragment相邻的两个fragment给加载 ...

  6. Android优化方案之--Fragment的懒加载实现

    一.背景 在Android应用中,ViewPager是我们不可避免使用的一个控件,因为它可以使我们在占用较少空间的同时,增强内容的丰富性,同时以其内部流淌着Google的血液,所以它几乎成了每一个Ap ...

  7. android ViewPager+Fragment之懒加载

    说说写这篇博客的背景吧,前两天去面试,问到一个问题说的是:比如我们首页,是有3个fragment构成的,并且要是实现作用可以滑,那么这个最好的选择就是ViewPager+fragment了,但是我们知 ...

  8. 【Android】Fragment懒加载和ViewPager的坑

    效果 老规矩,先来看看效果 ANDROID和福利两个Fragment是设置的Fragment可见时加载数据,也就是懒加载.圆形的旋转加载图标只有一个,所以,如果当前Fragment正处于加载状态,在离 ...

  9. ViewPager Fragment 懒加载 可见 总结 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

随机推荐

  1. Vue&Element开发框架中增加工作流处理,工作流的各个管理页面的界面处理

    我在起前面的几篇随笔中,大概介绍了工作流的一些场景化处理,包括如何把具体业务表单组件化,并在查看和编辑界面中,动态加载组件内容,以及对于查看申请单的主页面,把审批.取消.发起会签.会签.批示分阅.阅办 ...

  2. webpack 打包样式资源

    webpack 打包样式资源 webpack.config.js配置文件内容为: // 用来拼接绝对路径的方法 const {resolve} = require('path') module.exp ...

  3. 96.n-1位数

    描述 已知w是一个大于10但不大于1000000的无符号整数,若w是n(n≥2)位的整数,则求出w的后n-1位的数. 输入 第一行为M,表示测试数据组数. 接下来M行,每行包含一个测试数据. 输出 输 ...

  4. python openpyxl、RESTful、Webservice接口 基础知识

    最近 在做接口测试的时候,遇到如下问题:如何通过数据驱动去做批量接口测试呢,我们的测试数据放在哪里去维护?下面整理出相关点,供大家参考 1.如何维护接口测试数据:放在excel文件中,通过python ...

  5. 一个简单的golang项目,实验 gitlab-ci-cd Pipelines

    至少两台主机,gitlab + gitlab-runner gitlab + gitlab-runner安装略 项目源码:https://gitee.com/M27149/testgo.git 在自建 ...

  6. 从零搭建vsftpd

    先吐槽一下这个工具,配置繁琐,限制规则复杂,报错信息不够详细,学起来吃力. 准备工作 [root@vsftp-server ~]# mkdir /data/ #创建ftp目录 [root@vsftp- ...

  7. [cf1290D]Coffee Varieties

    思路 统计数的种类数,也等价于统计有多少个数满足其之前没有与其相同的数 将序列以$\frac{k}{2}$为块大小分块,那么即会有$m=\frac{2n}{k}$个块 (关于$k=1$的情况,以1为块 ...

  8. [loj3075]组合数求和

    Subtask1:​​​$m,nd\le 2\times 10^{3}$ 对$M$质因数分解,假设$M=\prod_{i=1}^{k}p_{i}^{\alpha_{i}}$(其中$p_{i}$为素数) ...

  9. AOP声明式事务

    1.spring-dao.xml修改 参考上面工程配置 <?xml version="1.0" encoding="UTF-8"?> <bea ...

  10. 「后端小伙伴来学前端了」Vuex进阶操作,让你的代码更加高效(简称如何学会偷懒 【手动狗头】)

    学妹手机里的美照 前言 前一篇写了Vuex基本使用,用起来还稍稍有些繁琐,代码有很多 冗余的地方,这篇就带着大家用更简单的方式来使用Vuex(其实就是怎么更好的偷懒,用更少的代码来完之前的事情) 进入 ...