Code Monkey home page Code Monkey logo

statuslayoutmanager's Introduction

StatusLayoutManager

Release API

切换不同的数据状态布局,包含加载中、空数据和出错状态。

特征

  1. 不会增加布局层数
  2. 提供一套可配置的默认状态布局
  3. 布局懒加载
  4. 重试按钮统一回调
  5. 支持自定义状态布局

预览

下载 demo 体验

status_layout_manager.gif

下载

  1. 在项目的 build.gradle 中添加:
allprojects {
    repositories {
	    ...
	    maven { url 'https://jitpack.io' }
    }
}
  1. 添加依赖
dependencies {
    implementation 'com.github.Bakumon:StatusLayoutManager:1.0.4'
}

使用

快速使用

使用 StatusLayoutManager#Builder 创建 StatusLayoutManager 对象:

statusLayoutManager = new StatusLayoutManager.Builder(recyclerView)
    .build();

在合适的时机显示对应的状态布局:

// 加载中
statusLayoutManager.showLoadingLayout();
// 空数据
statusLayoutManager.showEmptyLayout();
// 加载失败
statusLayoutManager.showErrorLayout();
// 加载成功,显示原布局
statusLayoutManager.showSuccessLayout();

以上可以满足大多数场景。

配置默认布局

以下 API 提供修改默认布局的方法,具体说明见注释。

statusLayoutManager = new StatusLayoutManager.Builder(recyclerView)

    // 设置默认加载中布局的提示文本
    .setDefaultLoadingText("l拼命加载中...")

    // 设置默认空数据布局的提示文本
    .setDefaultEmptyText("空白了,哈哈哈哈")
    // 设置默认空数据布局的图片
    .setDefaultEmptyImg(R.mipmap.ic_launcher)
    // 设置默认空数据布局重试按钮的文本
    .setDefaultEmptyRetryText("retry")
    // 设置默认空数据布局重试按钮的文本颜色
    .setDefaultEmptyRetryTextColor(getResources().getColor(R.color.colorAccent))
    // 设置默认空数据布局重试按钮是否显示
    .setDefaultEmptyRetryVisible(false)

    // 设置默认出错布局的提示文本
    .setDefaultErrorText(R.string.app_name)
    // 设置默认出错布局的图片
    .setDefaultErrorImg(R.mipmap.ic_launcher)
    // 设置默认出错布局重试按钮的文本
    .setDefaultErrorRetryText("重试一波")
    // 设置默认出错布局重试按钮的文本颜色
    .setDefaultErrorRetryTextColor(getResources().getColor(R.color.colorPrimaryDark))
    // 设置默认出错布局重试按钮是否显示
    .setDefaultErrorRetryVisible(true)

    // 设置默认布局背景,包括加载中、空数据和出错布局
    .setDefaultLayoutsBackgroundColor(Color.WHITE)
    .build();

自定义默认布局

statusLayoutManager = new StatusLayoutManager.Builder(recyclerView)
    // 设置加载中布局
    .setLoadingLayout(inflate(R.layout.layout_loading))
    // 设置空数据布局
    .setEmptyLayout(inflate(R.layout.layout_empty))
    // 设置出错布局
    .setErrorLayout(inflate(R.layout.layout_error))

    // 设置加载中布局
    .setLoadingLayout(R.layout.layout_loading)
    // 设置空数据布局
    .setEmptyLayout(R.layout.layout_empty)
    // 设置出错布局
    .setErrorLayout(R.layout.layout_error)

    // 设置空数据布局重试按钮 ID
    .setEmptyRetryID(R.id.tv_empty)
    // 设置出错布局重试按钮 ID
    .setErrorRetryID(R.id.tv_error)
    .build();

显示自定义状态布局

statusLayoutManager#showCustomLayout()有几个重载方法,下面以参数最多的为例介绍:

/**
 * 显示自定义状态布局
 *
 * @param customLayoutID 自定义布局 ID
 * @param clickViewID        重试按钮 ID
 * @return 自定义状态布局
 */
statusLayoutManager.showCustomLayout(R.layout.layout_custome, R.id.tv_customer, R.id.tv_customer1);

其中 clickViewID 参数,表示想要添加点击事件的 View 的 id。

点击监听

statusLayoutManager = new StatusLayoutManager.Builder(recyclerView)

    // 设置重试事件监听器
    .setOnStatusLayoutClickListener(new OnStatusLayoutClickListener() {
            @Override
            public void onEmptyChildClick(View view) {

            }

            @Override
            public void onErrorChildClick(View view) {

            }

            @Override
            public void onCustomerChildClick(View view) {

            }
        }
    })
    .build();

也可以使用 OnStatusLayoutClickListener 默认的实现类,像下面这样:

statusLayoutManager = new StatusLayoutManager.Builder(recyclerView)
    // 设置重试事件监听器
    .setOnStatusChildClickListener(new DefaultOnStatusChildClickListener() {
        @Override
        public void onEmptyChildClick(View view) {

        }

        @Override
        public void onErrorChildClick(View view) {

        }
    })
    .build();

statuslayoutmanager's People

Contributors

bakumon avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.