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