Android 碎片Fragment简单总结

2017-01-03  本文已影响227人  JustDo23

时间:2016年5月24日16:44:21

作者:JustDo23

说明:在开发中经常会遇到使用Fragment的情况。由于功能等的需要会对Fragment有一点点深入的理解。现在简单总结记录一下。

01. ViewPager + Fragment

在使用ViewPager嵌套Fragment的时候可能需要有以下的操作

02. setUserVisibleHint(boolean isVisibleToUser)方法

一般情况下,在ViewPager嵌套Fragment使用的时候,ViewPager会在初始化的时候一次性初始化2个Fragment,这两个都会走onCreateView并进入到resume状态。但是用户是只能看到第一个Fragment,第二Fragment虽然没有显示却同样进入到了resume状态。此时再用旧的想法当Fragment对用户可见的时候在onResume()方法中处理某些事情,如网络请求等这种想法就不太科学了。

在这种情况下Fragment中有个setUserVisibleHint(boolean isVisibleToUser)的方法就派上用场了。这个方法是专门设置Fragment显示和不显示的。

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    if (isFirst && isVisibleToUser && TextUtils.isEmpty(nextUrl)) {
        getDataFromServer(ConstantURLs.RESERVE_LIST, true, true);
    }
    super.setUserVisibleHint(isVisibleToUser);
}

ViewPager会在初始化及切换Fragment的时候调用这个方法来设置Fragment的显示与否。ViewPager会在设置Adapter之后立即调用第一个、第二个fragment的setUserVisibleHint(boolean isVisibleToUser)方法设置为false,然后会对第一个fragment再次调用setUserVisibleHint(boolean isVisibleToUser)方法设置为true

需要注意的是setUserVisibleHint(boolean isVisibleToUser)方法会在ViewPager嵌套Fragment这种情况下调用。别的情况可能不会调用。

03. RadioButton + FrameLayout + Fragment

这一种机制和ViewPager嵌套Fragment是两种完全不同的机制。使用RadioButton这种机制更类似于很早以前的TabHost的使用。因为之前的TabHost使用非常复杂而且不好用,所以很多人提出了使用RadioButton + FrameLayout + Fragment这种比较流行的解决方法。

对于这种有一个参考的demo,仿微信导航栏https://github.com/JayFang1993/android-demos

04. onHiddenChanged(boolean hidden)方法

这个方法和setUserVisibleHint(boolean isVisibleToUser)有些类似。但这个方法一看名称就是很明显是显示和隐藏的方法。在上边RadioButton + FrameLayout + Fragment模式中的Fragment中可以使用onHiddenChanged(boolean hidden)方法进行判断,而且setUserVisibleHint(boolean isVisibleToUser)方法是不会执行的。

05. add() show() hide() 以及 replace()

对于上边提到的RadioButton + FrameLayout + Fragment模式中使用的主要是第一种add() show() hide()方式进行Fragment的显示和隐藏。对于Fragment界面的切换还可以使用replace()的方法进行。

对于这两种方法有各自的特点,暂时没有总结到。

06. Fragment中的Context为空现象

在Fragment中使用context最常用的就是使用getActivity()方法进行获取上下文对象。但是有时候总是会出现getActivity()方法获取的对象为空的情况。这里忘记了当时使用的切换方式,应该是使用replace()方式的时候经常出现空的现象。

网上搜索后有提到Activity会被系统回收重建,但是Fragment不会,所以出现Fragment丢失Activity的情况。多数的解决方法是在onSaveInstanceState方法中进行处理。

本人的解决方法是在BaseFragment中定义mContext并在其onCreateView方法中进行获取。简单代码如下:

protected Context mContext;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    if (rootView == null) {
        rootView = inflater.inflate(getContentView(), null);
        mContext = getActivity();
        initView();
    }

    ViewGroup parent = (ViewGroup) rootView.getParent();
    if (parent != null) {
        parent.removeView(rootView);
    }

    return rootView;
}

另外提供一个搜索关键词fragment getactivity() 为空

最后

以上基本都是本人在工作过程中遇到的东西。工作比较忙碌以及本人写记录的习惯不强等原因。本人能力问题等,本片文档写的比较浅,并不很深。还是需要不断的学习努力,不断的提高。

最后附上一个刚看的连接,主要使用友盟相关http://www.jianshu.com/p/850556d33f63

不断完善,继续努力!

补丁

时间:2016年9月12日15:11:48

作者:JustDo23

简述:样式设置相关的代码做一个补充。

01. RadioButton使用的style

<!-- 导航按钮 -->
<style name="MainNavigation">
    <item name="android:layout_width">0dip</item>
    <item name="android:layout_height">match_parent</item>
    <item name="android:layout_weight">1</item>
    <item name="android:button">@null</item>
    <item name="android:background">@null</item>
    <!-- 消除不同版本上的位置错乱问题 -->
    <item name="android:drawablePadding">0dip</item>
    <item name="android:gravity">center</item>
    <item name="android:scaleX">0.8</item>
    <item name="android:scaleY">0.8</item>
    <item name="android:paddingTop">3dip</item>
    <item name="android:textSize">15sp</item>
    <item name="android:textColor">@drawable/selector_text_color_main</item>
</style>

02. 图片使用的selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@mipmap/ic_nav_me_on" android:state_checked="true" android:state_window_focused="false"/>
    <item android:drawable="@mipmap/ic_nav_me_off" android:state_checked="false" android:state_window_focused="false"/>
    <item android:drawable="@mipmap/ic_nav_me_off" android:state_checked="false"/>
    <item android:drawable="@mipmap/ic_nav_me_on" android:state_checked="true"/>

</selector>

03. 小红点

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="oval"
       android:useLevel="false">

    <!--小红点-->
    <gradient
        android:endColor="#F74A28"
        android:startColor="#F74A28"/>

</shape>

04. 飘红带数字

<!-- [融云]飘红 -->
<style name="Little_Red_Dot">
    <item name="android:background">@drawable/rc_unread_count_bg</item>
    <item name="android:gravity">center</item>
    <item name="android:textColor">@android:color/white</item>
    <item name="android:textSize">9sp</item>
    <item name="android:layout_centerVertical">true</item>
    <item name="android:layout_alignParentRight">true</item>
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
</style>
上一篇 下一篇

猜你喜欢

热点阅读