Giter Site home page Giter Site logo

hodgeseegle / statelayout Goto Github PK

View Code? Open in Web Editor NEW

This project forked from junixapp/statelayout

0.0 0.0 0.0 1.61 MB

一种无侵入,使用简单,无需修改现有布局,动态切换布局状态(Loading/Error/Empty/Content)的解决方案。

Kotlin 99.17% Shell 0.83%

statelayout's Introduction

StateLayout

Simple way to change your layout state, like loading, empty, error. Strong customizitaion, written by Kotlin.

Feature

  • 对已有布局零侵入,无需修改现有布局
  • 支持对Activity, Fragment, View进行状态切换
  • 支持自定义每种状态的布局
  • 暴露失败状态View点击回调
  • 暂时不支持对ConstraintLayout中的View进行状态切换

ScreenShot

StateLayout StateLayout

Gradle

Download

implementation 'com.lxj:statelayout:最新版本号'

Usage

对Activity/Fragment使用:

val stateLayout = StateLayout(this)
            .wrap(this)
            .showLoading()

对指定View使用:

val layout2 = StateLayout(this)
            .wrap(view)
            .showLoading()

默认是显示内容布局,改变状态的方法:

stateLayout.showLoading()
stateLayout.showContent() //default state
stateLayout.showError()
stateLayout.showEmpty()

在布局中使用:

<com.lxj.statelayout.StateLayout
            android:id="@+id/slInLayout"
            android:layout_marginBottom="40dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/tvInLayout"
                android:gravity="center"
                android:text="测试布局中使用"
                android:background="#9f00"
                android:textColor="#fff"
                android:textSize="20sp"
                android:layout_width="match_parent"
                android:layout_height="150dp"/>
        </com.lxj.statelayout.StateLayout>

自定义每种状态对应的布局:

StateLayout(this)
    .config(loadingLayoutId = R.layout.custom_loading, //自定义加载中布局
            errorLayoutId = R.layout.custom_error, //自定义加载失败布局
            emptyLayoutId = R.layout.custom_empty, //自定义数据位为空的布局
            useContentBgWhenLoading = true, //加载过程中是否使用内容的背景
            enableLoadingShadow = true, //加载过程中是否启用半透明阴影盖在内容上面
            retryAction = { //点击errorView的回调
                Toast.makeText(this, "点击了重试", Toast.LENGTH_SHORT).show()
            })
    .wrap(view)
    .showLoading()

在Fragment中使用的时候需要注意下,要将StateLayout作为Fragment的View返回:

private var stateLayout: StateLayout? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    if (fragmentView==null) {
        fragmentView = inflater.inflate(getLayoutId(), container, false)
        stateLayout = StateLayout(context!!)
                .wrap(fragmentView)
                .showLoading()
    }
    return stateLayout
}

statelayout's People

Contributors

junixapp 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.