StateView一个轻量级的控件, 继承自 View
, 吸收了 ViewStub
的一些特性, 初始状态下是不可见的, 不占布局位置, 占用内存少。 当进行操作显示空/重试/加载视图后, 该视图才会被添加到布局中。所以当用不到显示视图的时候, 占用内存是很少的。 由于目前只有3个视图, 所以至多只会添加3次, 相对于其他具有同样功能的一些控件, StateView更具有优势。
compile 'com.github.nukc.stateview:library:0.3.2'
添加到布局:
<com.github.nukc.stateview.StateView android:id="@+id/stateView" android:layout_width="match_parent" android:layout_height="match_parent"/>
mStateView.showEmpty();
mStateView.showLoading();
mStateView.showRetry();
显示内容: mStateView.showContent();
//实质就是setVisibility(GONE);
设置重试点击事件:
mStateView.setOnRetryClickListener(new StateView.OnRetryClickListener() { @Override public void onRetryClick() { //do something, no need to call showLoading() //不需要调用showLoading()方法, StateView自会调用 } });
或直接在代码中使用:
mStateView = StateView.inject(Activity activity); //如果有actionbar/toolbar mStateView = StateView.inject(activity, true);
mStateView = StateView.inject(ViewGroup parent); mStateView = StateView.inject(ViewGroup parent, boolean hasActionBar);
//可用于在Fragment的onCreateView中 mStateView = StateView.inject(View view); mStateView = StateView.inject(View view, boolean hasActionBar);
进一步测试, 增加Sample; 删除没必要的方法, 考虑到注入不应该遮挡工具栏, 为此增加inject方法
增加静态方法: inject(View view),该参数view必须是viewGroup,可用于在Fragment中
增加静态方法: inject(Activity activity),用于把StateView添加到DecorView中;
inject(ViewGroup parent),用于添加到ViewGroup中
修复显示LoadingView后还能触摸下层的View
修复issues #2
更新gradle和library版本, 增加一个私有方法showView。
修复v0.1.0版本中当使用layout_below的时候addView可能无法正常显示的问题。
测试Sample得知:
<com.github.nukc.stateview.StateView android:id="@+id/stateView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="100dp" tools:visibility="gone" />
使用 android:layout_below="@+id/ll" 的话 , addView有时会无法正常显示,有时却正常。在寻找问题。。 3个按钮错乱多按几次,有几率会出现不显示的情况。
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.