android 网络异常提示

  1. 整体设计思路
    当一个app当中有多个activity在共同的位置提示无网络环境(或者其他的内容),且进行刷新进一步操作时,就可以在同一个主activity里面实现这部分操作,其他activity继承这个主activity。
  2. 具体实现

主activity网络环境提示basenet_fragment.xml片段,将共同的提示部分直接写在主activity的布局当中

<include
            android:id="@+id/no_connection_view"
            android:layout_width="match_parent"
            android:layout_height="34dp"
            android:layout_below="@id/main_title"
            layout="@layout/activity_base_nonet_layout" />

具体布局activity_base_nonet_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/connect_error_bg"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="6dp"
        android:layout_marginRight="6dp"
        android:background="@drawable/common_error_ic" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:lines="1"
        android:text="@string/connect_error_text"
        android:textColor="@color/noconnect_text_color"
        android:textSize="12dp" />

    <TextView
        android:id="@+id/retry_tv"
        android:layout_width="wrap_content"
        android:layout_height="22dp"
        android:layout_marginRight="8dp"
        android:paddingLeft="2dp"
        android:paddingRight="2dp"
        android:minWidth="54dp"
        android:background="@drawable/retry_selector"
        android:clickable="true"
        android:gravity="center|center_vertical"
        android:text="@string/manageapp_downloadstate_retry"
        android:textColor="@color/white"
        android:textSize="12dp" />

</LinearLayout>

整体类图

类图解析:
ConnectChangeReceiver监听网络状态的变化,如果有网络状态变化,通过OnConnectChangedListener实现类也就是BaseNetAppFragmentActivity处理变化的后续动作。
其中,BaseNetAppFragmentActivity实现了OnConnectChangedListener这个接口,而ConnectChangeReceiver监听器通过回调OnConnectChangedListener的onConnectChange()方法,去通知BaseNetAppFragmentActivity进行相应的操作。

/**
* --------------
* 欢迎转载 | 转载请注明
* --------------
* 如果对你有帮助,请点击|顶|
* --------------
* 请保持谦逊 | 你会走的更远
* --------------
* @author css
* @github https://github.com/songsongbrother
* @blog http://blog.csdn.net/xiangxi101
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值