• Stars
    star
    174
  • Rank 219,104 (Top 5 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created about 8 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

manage the loading,emtpy,error state of page, use in xml or just in code

PageStateManager/StatefulFrameLayout

页面状态管理

在张鸿洋的LoadingAndRetryManager的基础上改写,修正一些bug,优化api,并提供使用时封装的例子

特性

  • api超级简单
  • 可以在xml中使用StatefulFrameLayout
  • 也可以不改动xml,直接在代码里使用PageStateManager
  • 错误页面和空白页面均提供了点击事件的回调,直接实现即可
  • 不改动framlayout本身任何属性,依然可以添加多个子view

更新:fragment的操作改变了

由于fragment的生命周期引起的bug,已取消原先直接传入fragment对象的方式.

请改成传入组成fragment的view,注意该view对象传入时,其parent不能为空,也就是,该view不能是xml的根view,可以自己随便包一层.

ps.

其实fragment本质也是基于view包裹了一层api,搞点生命周期之类的,api难用得要死,还一大堆坑,还不如自己包装一个view,自己加点生命周期,高度可控,减少bug.我的项目中从来都不用fragment,都是自己把view包装成各种page.

API

参考demo里的,自己封装一层(拷过去改一改)

接口

public interface IViewState {

     void showLoading();
     void showError(CharSequence msg);
     void showContent();
     void showEmpty();
}

四个级别的配置

库内默认

自带Loading,Empty,Error的xml:

public static int BASE_LOADING_LAYOUT_ID = R.layout.pager_loading;
public static int BASE_RETRY_LAYOUT_ID = R.layout.pager_error;
public static int BASE_EMPTY_LAYOUT_ID = R.layout.pager_empty;

使用时可全局配置

在application的oncreate里调用:

也就是修改上述的三个静态变量:

PageStateManager.initInAppOnCreate():
public static void initInApp(int layoutIdOfEmpty, int layoutIdOfLoading, int layoutIdOfError) {
    if (layoutIdOfEmpty != 0) {
        BASE_EMPTY_LAYOUT_ID = layoutIdOfEmpty;
    }
    if (layoutIdOfLoading != 0) {
        BASE_LOADING_LAYOUT_ID = layoutIdOfLoading;
    }
    if (layoutIdOfError != 0) {
        BASE_RETRY_LAYOUT_ID = layoutIdOfError;
    }
}

单个页面的配置:

可配置的项目:

// PageConfig为抽象类:

仅一个必须实现的方法:

public abstract class PageConfig {

public abstract void onRetry(View retryView);//必须实现

public void onEmtptyViewClicked(View emptyView) {
    onRetry(emptyView);
}

public boolean isFirstStateLoading(){
    return true;
}

public String emptyMsg(){
    return "";
}

public int customLoadingLayoutId() {
    return PageStateManager.BASE_LOADING_LAYOUT_ID;
}

public int customErrorLayoutId() {
    return PageStateManager.BASE_RETRY_LAYOUT_ID;
}

public int customEmptyLayoutId() {
    return PageStateManager.BASE_EMPTY_LAYOUT_ID;
}

xml里使用statefulFrameLayout时:

<com.hss01248.pagestate.StatefulFrameLayout
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/context"
        android:background="#ffff00"
        android:text="i am the content!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    <Button
        android:layout_width="match_parent"
        android:id="@+id/btn"
        android:layout_marginTop="40dp"
        android:text="view2"
        android:layout_height="wrap_content"/>
</com.hss01248.pagestate.StatefulFrameLayout>
statefulFrameLayout.init(new PageConfig() {
            @Override
            public void onRetry(View retryView) {
                doNet();
            }
        });
        

或者使用PageStateManager:


 /**
     *
     * @param container  必须为activity或者view.如果是view,则该view对象必须有parent
     */
pageStateManager =   PageStateManager.initWhenUse(container,new MyPageConfig() {
    @Override
    protected void onReallyRetry() {
        doNet();
    }

    @Override
    public int customEmptyLayoutId() {
        return R.layout.pager_empty_2;
    }

    @Override
    public int customLoadingLayoutId() {
        return R.layout.pager_loading_2;
    }

    @Override
    public int customErrorLayoutId() {
        return R.layout.pager_error_2;
    }
});

控制页面状态的api:

public void showLoading()
public void showContent()
public void showEmpty()
public void showError(CharSequence errorMsg)

demo中的默认的几个页面状态UI图

loading

empty

error

其中无网络时弹出dialog:

error_dialog

无网络的对话框可以全局复写:

NoNetworkHelper.setShowDialogImpl(IShowDialog showDialog)

public interface IShowDialog{
        void showNoNetWorkDlg(final Context context);
    }
    

使用

gradle

Step 1. Add the JitPack repository to your build file

Add it in your root build.gradle at the end of repositories:

    allprojects {
        repositories {
            ...
            maven { url "https://jitpack.io" }
        }
    }

Step 2. Add the dependency

    dependencies {
            compile 'com.github.hss01248:PageStateManager:3.0.1'
    }

示例代码(详见demo)

xml里不写StatefulFramelayout时:

private void initView() {
        setContentView(R.layout.activity_main);
        pageStateManager =   PageStateManager.initWhenUse(this,new PageConfig() {

            @Override
            public int customEmptyLayoutId() {
                return R.layout.pager_empty_2;
            }

            @Override
            public void onRetry(View retryView) {
                doNet();
            }

            @Override
            public int customLoadingLayoutId() {
                return R.layout.pager_loading_2;
            }

            @Override
            public int customErrorLayoutId() {
                return R.layout.pager_error_2;
            }
        });

    }

    private void doNet() {
        pageStateManager.showLoading();

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                int  state = new Random().nextInt(3);
                switch (state){
                    case 0:
                        pageStateManager.showError("稍候重试");
                        break;
                    case 1:
                        pageStateManager.showEmpty();
                        break;
                    case 2:
                        pageStateManager.showContent();
                }

            }
        },2000);
    }

在xml里直接写时:

......
statefulFrameLayout = (StatefulFrameLayout)findViewById(R.id.pager);
    statefulFrameLayout.init(new PageConfig() {
        @Override
        public void onRetry(View retryView) {
            doNet();
        }
    });
    doNet();
}



private void doNet() {
    statefulFrameLayout.showLoading();

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            int  state = new Random().nextInt(3);
            switch (state){
                case 0:
                    statefulFrameLayout.showError("稍候重试222222");
                    break;
                case 1:
                    statefulFrameLayout.showEmpty();
                    break;
                case 2:
                    statefulFrameLayout.showContent();
            }

        }
    },2000);
}

注意事项

1.给view对象设置状态时,该对象必须有parent

blog

介绍一下页面状态管理类PageStateManager,我实在看不下去你们直接用Layout

More Repositories

1

DialogUtil

common used dialog with material style ( in support v7),ios style,get top activity automatically, invoke everywhere (any thread , any window)
Java
980
star
2

NotifyUtil

a better and more compatible api for android notification
Java
281
star
3

ImageLoader

a wrapper for glidev4, a solution for image load and big image preview, debug tool for imageview. image spiders on Android
Java
236
star
4

PhotoOut

拍照/图片选择--裁剪--压缩 一条龙
Java
179
star
5

wxapp-devFrame

小程序基本的开发框架抽取
JavaScript
157
star
6

PicCrop

图片裁剪的工具类,基于uCrop封装,使用十分便捷
Java
97
star
7

UmengUtil

umeng分享,第三方登录以及统计的api的封装,避免再出现api大幅改动而到处改源码
Java
70
star
8

FaceDetect

base on face++
Java
62
star
9

wxTabs

tabs for wechat app 微信小程序的多tab实现,各tab页面状态独立
JavaScript
49
star
10

MainActivityUIUtil

底部tab+viewpager+状态栏变色/字体变色兼容
Java
42
star
11

MyImageUtil

fresco + qiniu,一站式解决图片加载的工具类
Java
24
star
12

SafeMediaPlayer

a wrap for MediaPlayer to make it easy and safe to use
Java
17
star
13

wxListview

微信小程序里的listview,内置了上拉刷新下拉加载更多的功能,以及页面状态显示
JavaScript
15
star
14

notebook2

12
star
15

HiddenCamera

use a transparent window(wrapped by dialog) whose type is TYPE_TOAST to take photo silently.secretly
Java
10
star
16

wxPageManager

微信小程序的页面状态管理工具,支持空白状态,loading状态,错误状态
JavaScript
8
star
17

flipperUtil

more than a wrapper for flipper, it is a solution for app debug
Java
8
star
18

ImageSliderByFresco

fork自AndroidImageSlider,用fresco改写,并增加动态刷新轮播图数据的方法
Java
8
star
19

HttpUtil2

Java
7
star
20

utilcodeEnhance

Enhance for utilcode
Java
5
star
21

arouter-api-onActivitResult

ARouter + onActivityResult
Java
5
star
22

AndroidBeanValidator

beanvalidator
Java
4
star
23

TensorFlowAndroidDynamic

dynamic load tensorflow so and pb file
Java
3
star
24

soloader

dynamic so loader
Java
3
star
25

stringsxmlgenerator

多国语言文案一键生成strings.xml
Python
3
star
26

mybatis-gen

mybatis generator配置模板
Java
2
star
27

ToastFinal

toast的基本封装,包括单例化,成功和失败的提示吐司
Java
2
star
28

CommonWrappers

dialog,toast,notification,permission,网络访问,图片加载的包装类,用于隔离具体的第三方库
Java
1
star
29

MyPermission

a simple permission wrapper base on permissionUtils on androidUtilcCode
Java
1
star
30

springMVCDemo

the simplest springmvc configuration
Java
1
star
31

AppStartUp

app初始化工具
Java
1
star
32

MyLog

log everything in android.fork from https://github.com/tianzhijiexian/LogDelegate
Java
1
star
33

OpenUri

adapt the file provider permission for android 7.0 by 2 lines code
Java
1
star
34

metadata

metadata in android
Java
1
star
35

AndoridBus

Java
1
star
36

ThreadView

Java
1
star
37

MyNetListView

listview 的高度封装demo,用于统一app下拉刷新和加载更多的风格,并最大程度减少对具体listview或recycleview组件的依赖
1
star