A customize multiple state layout for Android.

Overview

MultiStateLayout

version ci workflow

🌀 A customize multiple state layout for Android. (中文文档)

Preview

image

Download the sample apk to see more: Sample APK.

Setup

Add the multistatelayout dependency to your library or app's build.gradle file:

dependencies {
  ......
  implementation 'com.airsaid:multistatelayout:$version'
}

Usage

  1. Implement the State and StateProvider interfaces to provide states:
import com.airsaid.multistatelayout.State

class EmptyState : State {

  companion object {
    const val ID = R.id.emptyLayout
  }

  override fun getId() = ID

  override fun getLayoutId() = R.layout.multi_state_empty

  override fun onFinishInflate(stateView: View) {}
}
......
import com.airsaid.multistatelayout.StateProvider

class CommonStateProvider : StateProvider {
  override fun getStates(): MutableList<State> {
    return mutableListOf(EmptyState(), ErrorState(), LoadingState())
  }
}
  1. Add MultiStateLayout to the XML layout:
">
<com.airsaid.multistatelayout.MultiStateLayout
    android:id="@+id/multiStateLayout"
    android:layout_width="match_parent" 
    android:layout_height="match_parent">

    
    <FrameLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent">

        <TextView 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_gravity="center" 
            android:text="I'am content." />
    FrameLayout>
com.airsaid.multistatelayout.MultiStateLayout>
  1. Call the init() method in the code to initialize MultiStateLayout:
multiStateLayout.init(CommonStateProvider())
  1. Switching between states:
multiStateLayout.showContent()
multiStateLayout.showState(LoadingState.ID)
multiStateLayout.showState(EmptyState.ID)

TransitionAnimation

You can set the transition animation when the state is switched via the setTransitionAnimator(@Nullable TransitionAnimator transitionAnimator) method.

Currently existing transition animations are:

  • AlphaTransitionAnimator (default)
  • TranslationTransitionAnimator
  • AlphaTranslationTransitionAnimator

Listener

You can add listeners for state switching through the addStateChangedListener(@NonNull OnStateChangedListener listener) method:

multiStateLayout.addStateChangedListener { state, isShow ->
  Log.d(TAG, "onStateChanged state: $state, isShow: $isShow")
}

More

  • Set the click event of a specific state
class ErrorState : State {
  companion object {
    const val ID = R.id.errorLayout
  }

  private lateinit var callback: () -> Unit?

  override fun getId() = ID

  override fun getLayoutId() = R.layout.multi_state_error

  override fun onFinishInflate(stateView: View) {
    stateView.findViewById<Button>(R.id.reload).setOnClickListener {
      callback.invoke()
    }
  }

  fun setOnReloadListener(callback: () -> Unit) {
    this.callback = callback
  }
}
multiStateLayout.getState<ErrorState>(ErrorState.ID).setOnReloadListener {
  // do something...
}

License

Copyright 2021 Airsaid. https://github.com/airsaid

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
You might also like...
It's an Android library that allows you to use Layout as RadioButton or CheckBox.
It's an Android library that allows you to use Layout as RadioButton or CheckBox.

Android - CompoundLayout It's an Android library that allows you to use Layout as RadioButton or CheckBox. The librarie is Android 14+ compatible. Gra

A pull to refresh layout for android, the RecyclerRefreshLayout is based on the SwipeRefreshLayout. support all the views, highly customizable, code simplicity, etc.  really a practical RefreshLayout!
A pull to refresh layout for android, the RecyclerRefreshLayout is based on the SwipeRefreshLayout. support all the views, highly customizable, code simplicity, etc. really a practical RefreshLayout!

RecyclerRefreshLayout English | 中文版 RecyclerRefreshLayout based on the {@link android.support.v4.widget.SwipeRefreshLayout} The RecyclerRefreshLayout

Easy, flexible and powerful Swipe Layout for Android
Easy, flexible and powerful Swipe Layout for Android

SwipeRevealLayout A layout that you can swipe/slide to show another layout. Demo Overview Drag mode Drag mode normal: Drag mode same_level: Features F

[UNMAINTAINED]: AndroidMosaicLayout is android layout to display group of views as grid consists of different asymmetric patterns (90 different patterns).
[UNMAINTAINED]: AndroidMosaicLayout is android layout to display group of views as grid consists of different asymmetric patterns (90 different patterns).

AndroidMosaicLayout AndroidMosaicLayout is android layout to display group of views in more that 90 different patterns. What is AndroidMosaicLayout? I

Scalable Layout For Android
Scalable Layout For Android

ScalableLayout for Android. Class: com.ssomai.android.scalablelayout.ScalableLayout 한글버전 README.md: https://github.com/ssomai/ScalableLayout/blob/mast

Circular layout for android
Circular layout for android

CircleLayout Circular layout for android. Installlation Add CircleLayout as Android Library to your project. How to add project as Android Library Usa

An Android layout for arranging children along a circle
An Android layout for arranging children along a circle

CircleLayout An Android layout for arranging children along a circle You can customize the following options: cl_centerView: Set a specific view ID to

Android - A layout that arranges its children in relation to a background image
Android - A layout that arranges its children in relation to a background image

ImageLayout A layout that arranges its children in relation to a background image. The layout of each child is specified in image coordinates (pixels)

A layout engine for Android that decouples layouts from the View containers that manage scrolling and view recycling. FreeFlow makes it really easy to create custom layouts and beautiful transition animations as data and layouts change
Comments
Releases(v1.3.0)
Owner
Airsaid
Android software engineer.
Airsaid
GoolgePlusLayout is a custom layout that plays animation on the children views while scrolling as the layout in the Google Plus (android) main page

Google Plus Layout Google Plus Layout is a custom layout that support playing animation on child view(s) in a serialize manner like the the main

Ahmed Nammari 224 Nov 25, 2022
Responsive Layout Gird Configuration using Compose. An adaptive layout

ResponsiveGrid Responsive Grid is most followed layout system by the designer as it adapts to screen size and orientation, ensuring consistency across

null 4 Apr 12, 2022
Allows the easy creation of animated transition effects when the state of Android UI has changed

android-transition Android-Transition allows the easy creation of view transitions that reacts to user inputs. The library is designed to be general e

Kai 615 Nov 14, 2022
ViewStateLayout - Easy way to manage common state templates like loading, empty, error etc.!

ViewStateLayout Easy way to manage common state templates like loading, empty, error etc.! How to Step 1. Add the JitPack repository to your build fil

Kamrul Hasan 7 Dec 15, 2022
An Android Layout which has a same function like https://github.com/romaonthego/RESideMenu

ResideLayout An Android Layout which has a same function like https://github.com/romaonthego/RESideMenu. Can be used on Android 1.6(I haven't try it.)

Yang Hui 392 Oct 12, 2022
A very simple arc layout library for Android

ArcLayout A very simple arc layout library for Android. Try out the sample application on the Play Store. Usage (For a working implementation of this

ogaclejapan 1.4k Dec 26, 2022
Android layout that simulates physics using JBox2D

PhysicsLayout Android layout that simulates physics using JBox2D. Simply add views, enable physics, and watch them fall! See it in action with the sam

John Carlson 689 Dec 29, 2022
An Android demo of a foldable layout implementation. Engineered by Vincent Brison.

Foldable Layout This code is a showcase of a foldable animation I created for Worldline. The code is fully written with java APIs from the Android SDK

Worldline 599 Dec 23, 2022
A 3D Layout for Android,When you use it warp other view,it can became a 3D view,一秒让你的view拥有3D效果!

ThreeDLayout A 3D Layout,When you use it warp other view,it can became a 3D view 中文文档 preview USAGE 1.compile library allprojects { repositories {

androidwing 490 Oct 27, 2022