Android自定义View之多状态布局

效果图~

《Android自定义View之多状态布局》 效果图

对于大多数的App而言,多状态的页面都是必备的。一般加载数据的时候都会出现:加载中、加载成功、加载异常、无数据等等情况

一般在搭建项目的时候,就会先把这个页面写好,方便日后的开发。

分析

实现多状态布局View的时候,一般都是把里面的第一个子View当做内容View。 其他各种状态要显示的View则在
</LinearLayout>
</merge>

如图;

《Android自定义View之多状态布局》 状态图

参数获取

通过传入CustomStateOptions对象控制状态,方便设置

public class CustomStateOptions implements Serializable {
    @DrawableRes
    private int imageRes;
    private boolean isLoading;
    private String message;
    private String buttonText;
    private View.OnClickListener buttonClickListener;
    
    ....

控制状态

控制状态和提供API的代码就不贴了,状态控制就是一堆if else语句,只要细心一些都不会出错的。

API的提供一般都是看自己项目的需求,没有必要一蹴而就,因为后期再根据需求添加也不是什么难事。

查看全部代码github地址

使用布局

xml

<com.fssoft.statuslib.StatusAclululuView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:stfAnimationEnabled="true">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/title"/>
    <TextView
        android:gravity= "center"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="这里是视图" />
</LinearLayout>
</com.fssoft.statuslib.StatusAclululuView>

调用

 aclululu = (StatusAclululuView) findViewById(R.id.activity_main);
 默认提供页面
 aclululu.showError(listener);
 aclululu.showEmpty(listener);
 aclululu.showLoading();
 aclululu.showContent();
 aclululu.showOffline(listener);
 自定义页面
 aclululu.showCustom(new CustomStateOptions().image(R.drawable.ic_launcher).buttonText("自定义").message("12345,beast").buttonClickListener(listener));

下载查看全部代码github地址

    原文作者:ac噜噜噜
    原文地址: https://www.jianshu.com/p/22c2baffca17
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞