第三方开源库:页面状态切换布局

StateFrameLayout

StateFrameLayout:https://github.com/Vanish136/StateFrameLayout
demo:http://git.oschina.net/libraryDemo/StateFrameLayout01

gradle

compile 'com.lwkandroid:StateFrameLayout:1.0.1'

XML

<com.lwkandroid.stateframelayout.StateFrameLayout
    android:id="@+id/stateLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:enableContentAnim="true"   //是否在展示内容布局的时候开启动画(200msAlpha动画)
    app:emptyLayoutResId="@layout/layout_empty"   //这里指定空数据布局
    app:loadingLayoutResId="@layout/layout_loading" //这里指定加载过程的布局
    app:netErrorLayoutResId="@layout/layout_net_error" //这里指定网络错误的布局
    >

    <!--在这里定义内容布局,内容布局只能有一个-->
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/holo_green_dark">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="我是内容"
            android:textAppearance="?android:textAppearanceLarge"/>

    </FrameLayout>

</com.lwkandroid.stateframelayout.StateFrameLayout>

备注: 此外,对于空数据和网络错误的状态提供了重试接口,需要在空数据或网络错误布局中指定触发的view对应的id:android:id=”@id/id_sfl_empty_retry”或android:id=”@id/id_sfl_net_error_retry”

五种状态:

  1. StateFrameLayout.INIT:初始状态,所有布局隐藏,默认切换的状态
  2. StateFrameLayout.LOADING:只显示请求Loading布局
  3. StateFrameLayout.EMPTY:只显示空数据布局
  4. StateFrameLayout.NET_ERROR:只显示网络错误布局
  5. StateFrameLayout.SUCCESS:只显示请求成功后的内容布局
stateFrameLayout = (StateFrameLayout) findViewById(R.id.stateFrameLayout);
stateFrameLayout.setOnEmptyRetryListener(new StateFrameLayout.OnEmptyRetryListener() {
    @Override
    public void onEmptyRetry() {
        Toast.makeText(context,"数据为空,重新请求数据",Toast.LENGTH_SHORT).show();
    }
});
stateFrameLayout.setOnNetErrorRetryListener(new StateFrameLayout.OnNetErrorRetryListener() {
    @Override
    public void onNetErrorRetry() {
        Toast.makeText(context,"网路错误,重新请求数据",Toast.LENGTH_SHORT).show();
    }
});
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (position){
            case 0:
                stateFrameLayout.changeState(StateFrameLayout.LOADING);
                break;
            case 1:
                stateFrameLayout.changeState(StateFrameLayout.EMPTY);
                break;
            case 2:
                stateFrameLayout.changeState(StateFrameLayout.NET_ERROR);
                break;
            case 3:
                stateFrameLayout.changeState(StateFrameLayout.SUCCESS);
                break;
        }
    }
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值