1 效果图
加载数据时
加载数据完成时
加载数据异常时
2 实现说明
加载前:界面显示异步加载控件,隐藏数据显示控件,加载异常控件
加载成功:根据加载的数据,初始化数据显示控件
加载失败:显示加载异常的控件,异常异步加载控件
中间的加载过程,通过AsyncTask来实现,在AsyncTask中主要实现两个方法
//后台运行,互联网后台数据加载接口
protected Integer doInBackground(String...params)
//数据加载完成,结合数据,进行UI处理
protected void onPostExecute(Integerresult)
3 实现代码
3.1界面部分
加载控件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:id="@+id/async_begin" >
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
style="@android:style/Widget.ProgressBar.Small.Inverse"
android:layout_marginRight="5dp" />
<TextView android:text="加载信息中。。。。。"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:textColor="#000000"
android:layout_height="wrap_content"></TextView>
</LinearLayout>
加载失败控件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="horizontal"
android:id="@+id/async_error"
android:visibility="gone" >
<TextView android:text="网络异常,不能加载数据"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:textColor="#000000"
android:layout_height="wrap_content"></TextView>
<Button android:text=""
android:id="@+id/but_reflesh"
android:background="@drawable/but_reflesh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
</LinearLayout>
数据展示控件:
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:visibility="gone"
android:id="@+id/rl_content"
android:orientation ="vertical"
android:gravity="center_horizontal"
>
<LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout2" android:layout_width="300px">
<ImageView android:src="@drawable/ranklist_myrank" android:id="@+id/imageView2" android:layout_height="wrap_content" android:layout_width="wrap_content"></ImageView>
</LinearLayout>
<RelativeLayout android:background="@drawable/ranklist_itembg" android:layout_height="wrap_content" android:id="@+id/relativeLayout1" android:layout_width="300px">
<LinearLayout android:gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/line_photo" android:background="@drawable/ranklist_photobg" >
<ImageView android:src="@drawable/ranklist_male" android:id="@+id/ranklist_male" android:layout_height="wrap_content" android:layout_width="wrap_content"></ImageView>
</LinearLayout>
<LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/linearLayout4" android:layout_toRightOf="@id/line_photo" >
<TextView android:textColor="#000000" android:text="1. xuwenbing" android:id="@+id/txt_nicename" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:textColor="#000000" android:text="积分为:1234" android:id="@+id/txt_integral" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
<LinearLayout android:gravity="center_vertical" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/linearLayout4" android:layout_alignParentRight="true" >
<ImageView android:src="@drawable/ranklist_goto" android:id="@+id/imageView3" android:layout_height="wrap_content" android:layout_width="wrap_content"></ImageView>
</LinearLayout>
</RelativeLayout>
<LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout3" android:layout_width="wrap_content">
<ImageView android:src="@drawable/ranklist_rank_other" android:id="@+id/imageView4" android:layout_height="wrap_content" android:layout_width="wrap_content"></ImageView>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout5" android:layout_width="300px">
<ListView android:background="@drawable/ranklist_itembg" android:scrollbars="vertical" android:layout_height="300px" android:id="@+id/listView_ranklist" android:layout_width="wrap_content"></ListView>
</LinearLayout>
</LinearLayout>
3.2后台代码
AsyncTask类的生成,和调用:
//AsyncTask
class AsyncLoader_GuessInfo extends AsyncTask<String, Void, Integer>{
@Override
protected Integer doInBackground(String... params) {
int result=0;
try{
//加载数据
if(params[0].length()>0)
model= IntegralDataServiceHelper.GetRank(params[0],ProjectConstant.AppID);
list= IntegralDataServiceHelper.GetTopList(0, 10,ProjectConstant.AppID);
if(list!=null)
result=2;
}
catch(Exception ex){
result=-1;
}
return result;
}
@Override //处理界面
protected void onPostExecute(Integer result) {
Log.i("ExerciseGuess", "onPostExecute(Result result) called");
if( result==2)
LoadAndBandingData();
else
{
LinearLayout async_begin=(LinearLayout)findViewById(R.id.async_begin);
async_begin.setVisibility(View.GONE);
LinearLayout async_error=(LinearLayout)findViewById(R.id.async_error);
async_error.setVisibility(View.VISIBLE);
}
}
}
调用:new AsyncLoader_GuessInfo().execute(account);
异步界面,重试部分实现
//加载刷新按钮事件
Button but_reflesh=(Button)findViewById(R.id.but_reflesh);
but_reflesh.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v){
//显示加载的部分
LinearLayout async_begin=(LinearLayout)findViewById(R.id.async_begin);
async_begin.setVisibility(View.VISIBLE);
//隐藏异步加载失败部分
LinearLayout async_error=(LinearLayout)findViewById(R.id.async_error);
async_error.setVisibility(View.GONE);
//异步加载
new AsyncLoader_GuessInfo().execute(account);
}
});
界面初始化数据部分:
public void LoadAndBandingData()
{
LinearLayout async_begin=(LinearLayout)findViewById(R.id.async_begin);
async_begin.setVisibility(View.GONE);
LinearLayout rl_content=(LinearLayout)findViewById(R.id.rl_content);
rl_content.setVisibility(View.VISIBLE);
TextView txt_nicename =(TextView)findViewById(R.id.txt_nicename);
TextView txt_integral =(TextView)findViewById(R.id.txt_integral);
if(model!=null)
{
txt_nicename.setText(String.valueOf(model.RankNo)+". "+model.UserNiceName);
txt_integral.setText("当前的积分:"+String.valueOf(model.IntegralSum));
}
else
{
txt_nicename.setText("当前还没有注册用户!");
txt_integral.setText("当前的积分:0");
}
ListView listview =(ListView)findViewById(R.id.listView_ranklist);
listview.setAdapter(new RankListAdapter(this, R.layout.sub_ranlist_item,list));
//增加选择事件
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
}
});
本文出处:
http://www.cnblogs.com/macroxu-1982/archive/2011/08/11/2135233.html
分享到:
相关推荐
android自定义ProgressBar(仿淘宝)的加载效果
此demo主要是使用异步线程加载一张网络图片,过程中主要涉及到一些网络请求、java io操作、progressBar的使用,可以对于网络编程可以参考一下。
当我们使用浏览器浏览网页时,总会看到下图页面的样子,上面是一个地址栏,地址栏下面显示加载进度,加载完成后进入页面内容,带颜色的进度条总是少不了的,那样子看起来也舒服,如何实现自定义手机浏览器功能呢?
HttpRequest Document读取xml HtmlParse AsyncTask ListView ProgressBar
android 自定义ProgressBar 字体适配 实现了 进度文字跟随进度效果
这个demo是有listview实现的,里面又ProgressBar提示正在加载的效果,还有listview显示列表数据,当道最地下面的时候,向上滑动页面,会有加载更多的功能及效果,实现分页的另一种效果
各种样式的ProgressBar+源代码,最全的例子集合
AsyncTask实例代码演示Android异步任务.txt AsyncTask实例代码演示Android异步任务.txt
android 实现progressbar的简单自定义动画
android AsyncTask中更新progressBar 。可用于http请求、下载文件等
android progressbar背景
progressBar+多线程举例源码,c#2.0,winform 压缩包里是源码。 具体源码使用说明:http://www.cn-web.com/shtml/article/net/wform/2009/04/17/892.shtml
Android下ProgressBar和旗下两个子控件的使用,具体可参见博客http://www.cnblogs.com/plokmju/p/android_ProgressBar.html
进度条样式在项目中经常可以见到,下面小编给大家分享Android三种方式实现ProgressBar自定义圆形进度条。 Android进度条有4种风格可以使用。 默认值是progressBarStyle。 设置成progressBarStyleSmall后,图标变小...
AndroidProgressBar_Android ProgressBar进度条的几乎全部的用法源码集.rar
pb_progressBar.setTag(String.valueOf(position)); txt_title.setText(Utils.title[position]); pb_progressBar.setProgress(Utils.progress[position]); // 将每个下载项的进度条和下载、暂停、停止...
Android中使用ProgressBar实现进度条,详细了解请移步:http://blog.csdn.net/zxc514257857/article/details/70557872
android progressbar 垂直布局
课堂资源代码(Android-ProgressBar 自定义菊花加载效果)