netstatelayout extend framelayout.you can custom view to show net state: loading,error,ok.
Add the library as dependency to your build.gradle file.
dependencies {
//other dependencies...
compile 'com.xingliuhua:libnetstatelayout:1.0.1'
}
implement INetErrorView and INetLoadingView to custom your UI .of course,you can use SimpleNetErrorView and SimpleNetLoadingView.
public class MyNetErrorView implements INetErrorView {
...
}
public class MyNetLoadingView implements INetLoadingView {
...
}
you can set view by code or xml
- set view by code
mNetStateLayout.setNetErrorView(new MyNetErrorView());
mNetStateLayout.setNetLoadingView(new MyNetLoadingView());
2.set view by xml
<com.xingliuhua.libnetstatelayout.view.NetStateLayout
android:id="@+id/net_state_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:net_error="com.xingliuhua.libnetstatelayout.view.MyNetErrorView"
app:net_loading="com.xingliuhua.libnetstatelayout.view.MyNetLoadingView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="by xml attr"/>
<Button
android:id="@+id/test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="to second activity"/>
</LinearLayout>
</com.xingliuhua.libnetstatelayout.view.NetStateLayout>
public class MainActivity extends AppCompatActivity {
private NetStateLayout mNetStateLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.test).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, SecondActivity.class));
}
});
mNetStateLayout = (NetStateLayout) findViewById(R.id.net_state_layout);
mNetStateLayout.setContentState(NetStateLayout.CONTENT_STATE_SHOW_LOADING);
mNetStateLayout.setOnRetryClickListener(new INetErrorView.OnRetryClickListener() {
@Override
public void onRetryClicked() {
mNetStateLayout.setContentState(NetStateLayout.CONTENT_STATE_HIDE);
}
});
mHandler.sendEmptyMessageDelayed(0, 3000);
}
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
mNetStateLayout.setContentState(NetStateLayout.CONTENT_STATE_SHOW_NET_ERROR);
}
};
}