# StateView
[](https://travis-ci.org/nukc/StateView)
[ ](https://bintray.com/nukc/maven/StateView/_latestVersion)
[](https://android-arsenal.com/details/1/4255)
[中文](https://github.com/nukc/StateView/blob/master/README-zh.md)
StateView is an invisible, zero-sized View that can be used to lazily inflate loadingView/emptyView/retryView at runtime.

![]()
## Installation
add the dependency to your build.gradle:
```groovy
compile 'com.github.nukc.stateview:library:1.5.4'
// animator providers
compile 'com.github.nukc.stateview:animations:1.0.1'
```
##Usage
Can be directly used in java.
```java
mStateView = StateView.inject(Activity activity);
```
```java
mStateView = StateView.inject(ViewGroup parent);
mStateView = StateView.inject(ViewGroup parent, boolean hasActionBar);
```
```java
// if view is not ViewGroup, StateView will be inject to view.getPatent()
mStateView = StateView.inject(View view);
mStateView = StateView.inject(View view, boolean hasActionBar);
```
```java
mStateView = StateView.wrap(View view);
```
Or include the StateView widget in your layout.
```xml
```
To switch the state view:
- ```mStateView.showEmpty();```
- ```mStateView.showLoading();```
- ```mStateView.showRetry();```
- ``` mStateView.showContent();```
To listen the retry click:
```java
mStateView.setOnRetryClickListener(new StateView.OnRetryClickListener() {
@Override
public void onRetryClick() {
//do something, no need to call showLoading()
}
});
```
To customize view:
- Global settings way: create a new layout (layout's name must be ```base_empty```/```base_retry```/```base_loading```).
- Single page setting: create a new layout whit different name, and set resource in java.
```java
setEmptyResource(@LayoutRes int emptyResource)
setRetryResource(@LayoutRes int retryResource)
setLoadingResource(@LayoutRes int loadingResource)
```
use setOnInflateListener to set message, like AnimatorActivity.java#L28
## Custom Attribute
```xml
```
## Animation
set:
```java
// provider default is null, no animation
// if need, set a AnimatorProvider
setAnimatorProvider(AnimatorProvider provider)
```
animation can custom, can also compile ```animations```
```groovy
compile 'com.github.nukc.stateview:animations:1.0.1'
```
```animations``` library has:
- ```FadeScaleAnimatorProvider```
- ```FlipAnimatorProvider```
- ```SlideAnimatorProvider```
if want a custom animation,implements ```AnimatorProvider```
```java
public class FadeScaleAnimatorProvider implements AnimatorProvider {
@Override
public Animator showAnimation(View view) {
AnimatorSet set = new AnimatorSet();
set.playTogether(
ObjectAnimator.ofFloat(view, "alpha", 0f, 1f),
ObjectAnimator.ofFloat(view, "scaleX", 0.1f, 1f),
ObjectAnimator.ofFloat(view, "scaleY", 0.1f, 1f)
);
return set;
}
@Override
public Animator hideAnimation(View view) {
AnimatorSet set = new AnimatorSet();
set.playTogether(
ObjectAnimator.ofFloat(view, "alpha", 1f, 0f),
ObjectAnimator.ofFloat(view, "scaleX", 1f, 0.1f),
ObjectAnimator.ofFloat(view, "scaleY", 1f, 0.1f)
);
return set;
}
}
```
## License
The MIT License (MIT)
Copyright (c) 2016 Nukc
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.