封装一个通用加载状态控件

netstatelayout是一个继承于framelayout的控件,可当做普通的framelayout使用。

github地址:https://github.com/xingliuhua/netStateLayout

效果图:

适用场景

 在activity网络请求经常需要用到加载的一个动画,有这么几种状态: 
1、正在加载中,显示一个动画。 
2、加载成功,则动画消失。 
3、没网络,加载失败,则点击能重新加载。 
封装成一个控件可提高复用性,activity、fragment甚至局部控件都可以使用。 
各个app或者同一个app各界面需要的加载中、网络重试风格各不相同,需支持自定义各状态ui

核心类及方法

INetErrorView

网络错误接口,自定义ui必须实现该接口

public interface INetErrorView {

    void setRetryClickListener(OnRetryClickListener retryClickListener);

    View getView(Context context);

    void hide();

    void show();

    public static interface OnRetryClickListener {
        void onRetryClicked();
    }
}

getView方法为外界提供自定义的ui; 
hide、show方法为隐藏或显示view,可以在方法中加入自己的逻辑 
setRetryClickListener方法为设置“重试”监听器

INetLoadingView

加载中控件的接口

public interface INetLoadingView {
    View getView(Context context);
    void hide();
    void show();
}

NetStateLayout

继承于framelayout,可以当做普通的framelayout使用。

扫描二维码关注公众号,回复: 2532670 查看本文章
public void setNetErrorView(INetErrorView netErrorView){
...
}

public void setNetLoadingView(INetLoadingView netLoadingView){
...
}

public void setContentState(int contentState) {
...
}

public void setOnRetryClickListener(INetErrorView.OnRetryClickListener onRetryClickListener) {
...
}

setContentState方法主要用于切换状态,需要主动去切换。 
setNetErrorView、setNetLoadingView用于代码设置自定义的ui

使用方法

添加依赖

dependencies {
	//other dependencies...
	compile 'com.xingliuhua:libnetstatelayout:1.0'
}

实现INetErrorView接口,sdk中已提供简单的SimpleNetErrorView类,只包含一个简单的。 
实现INetLoadingView,sdk中已提供简单的SimpleNetLoadingView,只显示一个简单的圆形progressBar。 
可以通过xml设置自定义的ui:

<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>

也可以通过代码设置:

 mNetStateLayout.setNetErrorView(new MyNetErrorView());
 mNetStateLayout.setNetLoadingView(new MyNetLoadingView());

切换状态

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);
        }
    };
}

猜你喜欢

转载自blog.csdn.net/xlh2015/article/details/81393102