Code Monkey home page Code Monkey logo

multiplestatusview's Introduction

API Library version License

一个支持多种状态的自定义View,可以方便的切换到:

  • 加载中视图
  • 错误视图
  • 空数据视图
  • 网络异常视图
  • 内容视图

apk下载

使用

dependencies {
    implementation 'com.classic.common:multiple-status-view:1.7'
}

示例

<com.classic.common.MultipleStatusView
    android:id="@+id/multiple_status_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:loadingView="@layout/custom_loading_view"
    app:emptyView="@layout/custom_empty_view"
    app:errorView="@layout/custom_error_view"
    app:noNetworkView="@layout/custom_no_network_view"
    app:contentView="@layout/main_content"/>
MultipleStatusView multipleStatusView = (MultipleStatusView) findViewById(R.id.multiple_status_view);

//显示加载中视图
multipleStatusView.showLoading();
// mMultipleStatusView.showLoading("自定义提示内容");
// mMultipleStatusView.showLoading(R.string.custom_hint_content, "占位符1", "占位符2", "...");
// multipleStatusView.showLoading(R.layout.xxx, layoutParams);
// multipleStatusView.showLoading(customView, layoutParams);

//显示空视图
multipleStatusView.showEmpty();
// mMultipleStatusView.showEmpty("自定义提示内容");
// mMultipleStatusView.showEmpty(R.string.custom_hint_content, "占位符1", "占位符2", "...");
// multipleStatusView.showEmpty(R.layout.xxx, layoutParams);
// multipleStatusView.showEmpty(customView, layoutParams);

//显示错误视图
multipleStatusView.showError();
// mMultipleStatusView.showError("自定义提示内容");
// mMultipleStatusView.showError(R.string.custom_hint_content, "占位符1", "占位符2", "...");
// multipleStatusView.showError(R.layout.xxx, layoutParams);
// multipleStatusView.showError(customView, layoutParams);

//显示无网络视图
multipleStatusView.showNoNetwork();
// mMultipleStatusView.showNoNetwork("自定义提示内容");
// mMultipleStatusView.showNoNetwork(R.string.custom_hint_content, "占位符1", "占位符2", "...");
// multipleStatusView.showNoNetwork(R.layout.xxx, layoutParams);
// multipleStatusView.showNoNetwork(customView, layoutParams);

//显示内容视图
multipleStatusView.showContent();
// multipleStatusView.showContent(R.layout.xxx, layoutParams);
// multipleStatusView.showContent(customView, layoutParams);

//设置重试视图点击事件
multipleStatusView.setOnRetryClickListener(onRetryClickListener);

//设置视图状态切换监听事件
mMultipleStatusView.setOnViewStatusChangeListener(OnViewStatusChangeListener);

/**
* 获取当前view的状态
*      MultipleStatusView.STATUS_LOADING   //当前为加载中视图
*      MultipleStatusView.STATUS_EMPTY     //当前为空视图
*      MultipleStatusView.STATUS_ERROR     //当前为错误视图
*      MultipleStatusView.STATUS_NO_NETWORK//当前为无网络视图
*      MultipleStatusView.STATUS_CONTENT   //当前为内容视图
*/
int viewStatus = multipleStatusView.getViewStatus();

MultipleStatusView 继承自 RelativeLayout,所以内容视图也可以直接写在 MultipleStatusView 内部

<com.classic.common.MultipleStatusView
    android:id="@+id/multiple_status_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:emptyView="@layout/custom_empty_view"
    app:errorView="@layout/custom_error_view"
    app:loadingView="@layout/custom_loading_view"
    app:noNetworkView="@layout/custom_no_network_view">

    <ImageView
        android:id="@+id/image"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="24dp"
        android:layout_marginRight="16dp"
        android:src="@drawable/test"/>

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/image"
        android:layout_toRightOf="@id/image"
        android:text="内容视图111111"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/image"
        android:layout_toRightOf="@id/image"
        android:text="内容视图222222"/>

</com.classic.common.MultipleStatusView>

注意事项

必须按如下规则设置对应视图根节点的 view id,否则会导致试图重叠
必须按如下规则设置对应视图根节点的 view id,否则会导致试图重叠
必须按如下规则设置对应视图根节点的 view id,否则会导致试图重叠

1. 如果使用自定义属性

app:emptyView="@layout/..."
app:errorView="@layout/..."
app:loadingView="@layout/..."
app:noNetworkView="@layout/..."

需要设置:

  • 加载中视图的id必须为:loading_view
  • 空视图的id必须为:empty_view
  • 错误视图的id必须为:error_view
  • 无网络视图的id必须为:no_network_view
  • 自定义提示文本的id必须为:status_hint_content

2. 如果需要点击某个 view 进行重试, 需要设置:

  • 空视图内对应的view id:empty_retry_view
  • 错误视图内对应的view id:error_retry_view
  • 无网络视图内对应的view id:no_network_retry_view

3. 使用 new 关键字创建自定义视图时,请设置 id

TextView tv = new TextView(getApplicationContext());
tv.setId(Utils.generateViewId());
tv.setText(text);

更多使用方法详见 demo 示例:

贡献者

感谢以下人员贡献的代码

关于

License

Copyright 2015 classic

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.

multiplestatusview's People

Contributors

lindroy avatar qyxxjd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

multiplestatusview's Issues

显示内容视图,存在bug

显示内容视图的时候,会将MultipleStatusView下的不在mOtherIds列表内的所有childView的visibale状态置为Visiable,如果原来视图有部分是需要隐藏的就会全部显示出来

一点改进建议

根据mContentView = findViewById(R.id.content_view);去设置内容视图有点不便,用起来还得把内嵌视图最外层命名为content_view,繁琐切容易重复,可以考虑内嵌视图只支持一个子视图,然后mContentView = this.getChildAt(0);去处理

显示视图有BUG

一开始显示loading_view,后 再显示empty_view, loading_view没有关闭, 两个view重叠了. 没有关闭view的方法

然后在fragment应用 外层是viewpager+tablayout 滑动几次就会空白

MultipleStatusView 嵌套TwinklingRefreshLayout



<com.classic.common.MultipleStatusView
android:id="@+id/include_multiple_status_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:emptyView="@layout/custom_empty_view"
app:errorView="@layout/custom_error_view"
app:loadingView="@layout/custom_loading_view"
app:noNetworkView="@layout/custom_no_network_view"

    >

    <!--scrollview布局-->
    <com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
        android:id="@+id/include_refreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!--列表布局-->
        <android.support.v7.widget.RecyclerView
            android:id="@+id/include_recyclerview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/headview"
            android:background="#fff"
            android:focusableInTouchMode="false"
            android:overScrollMode="never"
            android:scrollbars="none" />

    </com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout>
</com.classic.common.MultipleStatusView>

然后在fragment应用 外层是viewpager+tablayout 滑动几次就会空白

同一个页面中多次使用会互相干扰

在同一个界面中会出现干扰问题,如下所示:
1.当我在页面中放入一个时,效果如下
image

2.当放入两个时,效果如下
image

这两个contentView的内容高度都是自适应的,高度值是不一样的,如果多次使用时,所有的contentView的高度都会和最后一个contentView的高度保持一致,而不是使用自己的高度,所以出现上面显示不全的现象。

关于loading

加载中不是简单的loading,而是加载xml做动画的话,是得自己集成MultipleStatusView重写showLoading方法么?

能动态添加 布局吗

这个好像只能通过xml来设置布局,能通过代码动态添加 空 异常 内容 加载 布局吗?

无法获取点击事件

在节点下面添加类似recyclerview的控件,在错误界面无法监听到重新加载的回掉

视图全局配置

对于整个app都公用同样一套布局的时候,可以增加一个全局配置布局的方法,app加载时一次性配置

感谢!

这一天心情也抑郁,看到表情包icon看笑了,心情也变好了,感谢

少了一个请求状态

比如说登陆时,应该在上面显示一个登陆中的状态,但并不完全隐藏登陆的信息。再如请求网络的时候的,用加载中好像就不太匹配。

空指针异常

平时测试都没有什么问题,但是在后台却收集到了很多空指针异常的问题;
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup)' on a null object reference
at com.classic.common.MultipleStatusView.inflateView(MultipleStatusView.java)
at com.classic.common.MultipleStatusView.showEmpty(MultipleStatusView.java)
at com.classic.common.MultipleStatusView.showEmpty(MultipleStatusView.java)

MultipleStatusView是直接下载布局里面的,通过butterknife来绑定的控件;
求解答;

加载视图可以禁止触摸吗?

作者你好,我在项目中遇到这样的需求:在空视图时也可以下拉刷新,所以MultipleStatusView是放在下拉刷新库里面的,但是这样话在加载视图时也可以下拉刷新,这样会频繁触发网络请求。所以可以在加载数据显示加载视图的时候禁止其他操作吗?

支持四个参数的构造方法

 public RelativeLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) 

这个构造方法能支持一下嘛?
我在继承MultipleStatusView时,定义一个style

<style name="BaseMultipleStatusView" parent="MultipleStatusView">
        <item name="emptyView">@layout/empty_view</item>
        <item name="errorView">@layout/error_view</item>
        <item name="loadingView">@layout/layout_loading_view</item>
        <item name="noNetworkView">@layout/layout_net_offline</item>
    </style>

这样就可以放到第四个参数里。(当然也可以在xml中定义style引用这个)
要是能支持第四个参数,直接在继承类中加入就更方便了,烦请大佬指点!!!

关于实现思路

好早前看过一次代码,就是viewgroup添加几个view,再去判断对应的状态,其它隐藏。
显然又加了一个层次,我考虑能否在Activity或者Fragment里面用代码依据对应的网络请求动态地去改变window的内容,不必这样一个层次,作者有时间可以讨论下。

引用库报错,使用不了

在实例代码中build显示以下错误:
Error:Unable to resolve dependency for ':app@debug/compileClasspath': Could not resolve com.classic.common:multiple-status-view:1.4. <a href="openFile:C:/Users/Leu/Desktop/MultipleStatusView-master/app/build.gradle">Open File</a><br><a href="Unable to resolve dependency for &#39;:app@debug/compileClasspath&#39;: Could not resolve com.classic.common:multiple-status-view:1.4.">Show Details</a>
在自己的代码中引用也出了类似的错。
网上搜索之后是这个原因:
http://www.jcodecraeer.com/a/anzhuokaifa/Android_Studio/2017/1026/8646.html
可是其他的库都是可以用的,就这个库不行。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.