zoukankan      html  css  js  c++  java
  • 转载 (Android实战)ProgressBar+AsyncTask实现界面数据异步加载

    1 效果图

     

                                 加载数据时

     

                            加载数据完成时

     

              加载数据异常时

     

     

    2 实现说明

      加载前:界面显示异步加载控件,隐藏数据显示控件,加载异常控件

      加载成功:根据加载的数据,初始化数据显示控件

      加载失败:显示加载异常的控件,异常异步加载控件

     

     中间的加载过程,通过AsyncTask来实现,在AsyncTask中主要实现两个方法

     //后台运行,互联网后台数据加载接口

    protected Integer doInBackground(String... params)

     

    //数据加载完成,结合数据,进行UI处理

    protected void onPostExecute(Integer result)

     

    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>

    数据展示控件:

    View Code
    <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 类的生成,和调用:

    View Code
     //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);

    }
    });

    界面初始化数据部分:

    View Code
    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) {
    }
    });
  • 相关阅读:
    poj 1753 -- Flip Game
    hdu 2209 -- 翻纸牌游戏
    文件系统的挂载与卸载挂载
    我的vim配置(一)
    Poj 3687 -- Labeling Balls
    主动激发“onclick”事件;prompt
    this
    函数嵌套
    调用函数时传递的实参个数arguments.length; ,函数定义时的形参个数sum.length
    回调函数,用户定义的排序规则
  • 原文地址:https://www.cnblogs.com/xiao0/p/2174426.html
Copyright © 2011-2022 走看看