此项目kotlin 编写 完美适配Java项目
implementation 'com.zdg.android:stateview:0.0.2'
or
implementation 'com.zdg.android:basestateui:0.0.2'
只需要关注一个 StateManagerView
public class LoadingStateView extends BaseStateView {
public static final String STATE = "loading_state";
@Override
protected int getLayoutId() {
return R.layout.base_loading_layout;
}
@Override
protected void onViewCreated(View stateView) {
stateView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
actionListener(v);
}
});
}
/**
* 该状态的
* @return
*/
@Override
public String getState() {
return STATE;
}
/**
* ShowState.STACK 叠加模式 顾名思义
* ShowState.ONLY 单显模式 显示该状态 会将其他状态和原View gone
* @return
*/
@Override
public ShowState getShowState() {
return ShowState.STACK;
}
}
[关键点]
StateView的点击事件 处理方法:在StateViewView 点击事件只需要调用 actionListener(v)
就能把点击事件传到上层 StateManagerView#onActionListener
StateViewStore.registerState(LoadingStateView.STATE,LoadingStateView.class);
StateViewStore.registerState(ExceptionStateView.STATE,ExceptionStateView.class);
一个套路
wrapper(View)
这个方法就是将原View 绑定到 StateManagerView中 我们需要修改这个View的状态 只需要调用
StateViewChanger mChanger = new StateManagerView.Builder(this).wrapper(findViewById(R.id.tv_hello)).builder();
mChanger.setStateActionListener(new StateActionListener() {
@Override
public void onActionListener(@NotNull String state, @NotNull View view, @Nullable StateProperty stateProperty) {
if (state == ExceptionStateView.STATE) {
if (view.getId()==R.id.btn_report) {
mObserver.showState(CoreStateView.STATE);
startActivity(new Intent(MainActivity.this,SimpleActivity.class));
}
}
}
});
mChanger.showState(LoadingStateView.STATE);
<com.zdg.stateviewmanager.StateManagerView
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--布局-->
</com.zdg.stateviewmanager.StateManagerView>
操作用法和上面一样 只是接入StateView 不一样 这种侵入性强
implementation 'com.zdg.android:basestateui:0.0.1'
public class DemoStateUIActivity extends StateActivity{
setStateActionListener(); // 类似mChanger.setStateActionListener
showState(); // 类似mChanger.showState
hideState(); // 类似mChanger.hideState
}
public class DemoFragment extends StateFragment{
setStateActionListener(); // 类似mChanger.setStateActionListener
showState(); // 类似mChanger.showState
hideState(); // 类似mChanger.hideState
}
[具体方法看Demo]