想象一个情景:我打开一个activity,然后通过网络请求请求服务器数据,在数据未加载成功之前,为了不让界面显示空白,有时候我们会在界面添加一个加载对话框或是嵌入页面的加载进度条。如果遇到网络请求失败,我们还要显示一个网络连接失败界面提醒用户检查网络连接状态。或是加载成功后服务器没有没有相应的数据,我们则会显示一个“数据为空”来提醒用户...
这些看似与我们显示无关的内容:加载进度条,网络连接失败提醒,数据为空界面。我们都必须得手动的添加到每个activity中,这个重复的代码你会愿意这个干吗?
所以我有一个好的主意:按照上面的逻辑是我们把各种逻辑界面添加到们要显示的activity的内容,现在我们换一个位置,我们把每个activity界面显示内容添加到一个里面包含了各种的逻辑处理界面。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="@layout/include_title" />
<ViewSwitcher
android:id="@+id/profileSwitcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:inAnimation="@android:anim/slide_in_left"
android:outAnimation="@android:anim/slide_out_right" >
<include
android:id="@+id/include_progress_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
layout="@layout/include_progress" />
<LinearLayout
android:id="@+id/include_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:orientation="vertical" />
</ViewSwitcher>
</LinearLayout>
这是我们可以通过ViewStub或include导入逻辑处理界面。上面的id号为:include_content就是我们要把内容添加进入的容器,所以,当我去完成一个activity布局的时候可以不用考虑标题栏或是逻辑处理界面,专心于每个activity的内容。我使用了android 的ViewSwitch控件用来对界面的显示或隐藏的状态切换,值得注意的是viewSwitch下的自控见都必须先隐藏。我还为这个控件添加了动画效果。
下一步是在actvity的是如何使用的,我把layout_common作为每个界面内容的父类,设置显示加载进度条进行等待。switcher.showNext();就可以显示下一个内容界面了。
<span style="white-space:pre"> </span>protected void onCreate(android.os.Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_common);
View layout = View.inflate(this, R.layout.activity_news, null);
ViewGroup include_content= (ViewGroup) findViewById(R.id.include_content);
switcher = (ViewSwitcher) findViewById(R.id.profileSwitcher);
include_content.addView(layout);
<span style="white-space:pre"> </span>switcher.setDisplayedChild(0);
};
以上就是我在数据加载到数据显示到界面的转场处理核心原理,很简单。下面还附上我的具体的小案例
http://download.csdn.net/detail/iwantyousafe/8419377