如何在列表片段使用AsyncTask的实施进度? [英] How to implement progressbar using AsyncTask in list fragment?

查看:182
本文介绍了如何在列表片段使用AsyncTask的实施进度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要像加载下面的图片列表前显示进度。我班扩展SherlockListFragment与AsyncTask的。我瞪大眼睛很多,但没有得到落实进度合适的信息。任何人可以帮助我如何实现进度像下面的图片。

在此先感谢。

图像1

以下是我的类

 公共类住宅扩展SherlockListFragment {@覆盖
公共查看onCreateView(LayoutInflater吹气,ViewGroup中的容器中,包保存){    查看查看= inflater.inflate(R.layout.residential_list,集装箱,FALSE);
    resListviewId =(ListView控件)view.findViewById(android.R.id.list);
    projectList =新的ArrayList<&HashMap的LT;字符串,字符串>>();    新LoadProjects()执行();    返回视图。
}公共无效调用onStart(){
    super.onStart();}//为网络操作内部类
私有类LoadProjects扩展的AsyncTask<字符串,字符串,字符串> {    @覆盖
    在preExecute保护无效(){
        super.on preExecute();
    }    @覆盖
    保护字符串doInBackground(字符串... PARAMS){
        返回null;
    }    @覆盖
    保护无效onPostExecute(字符串结果){        ListAdapter projAdapter =新SimpleAdapter(getSherlockActivity()
                projectList,R.layout.residential_list_item,
                新的String [] {PROJ_ID,PROJ_NAME},新的INT []
                        {R.id.projectId,R.id.projectName});
        //更新UI
        setListAdapter(projAdapter);
    }
}
}

以下是progressbar.xml

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / projectLinearLayout
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=横向
    机器人:重力=CENTER_HORIZONTAL
    机器人:填充=10dp
    机器人:知名度=水涨船高>    <进度
        机器人:ID =@ + ID / projectprogressBar
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_gravity =center_vertical/>    <的TextView
        机器人:ID =@ + ID / projectProgressTxtvw
        机器人:文字=@字符串/载入
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT/>< / LinearLayout中>


解决方案

试试这个。它可能对你有用。

 公共类住宅扩展SherlockListFragment
{公共查看onCreateView(LayoutInflater吹气,ViewGroup中的容器中,包保存)
{
查看查看= inflater.inflate(R.layout.residential_list,集装箱,FALSE);
resListviewId =(ListView控件)view.findViewById(android.R.id.list);
projectList =新的ArrayList<&HashMap的LT;字符串,字符串>>();新LoadProjects()执行();返回视图。
}
公共无效调用onStart(){
super.onStart();}
私有类LoadProjects扩展的AsyncTask<字符串,字符串,字符串> {@覆盖
在preExecute保护无效(){
   showLoader(正在加载...);
    super.on preExecute();
}@覆盖
保护字符串doInBackground(字符串... PARAMS){
    返回null;
}@覆盖
保护无效onPostExecute(字符串结果){           hideLoader();
    ListAdapter projAdapter =新SimpleAdapter(getSherlockActivity()
            projectList,R.layout.residential_list_item,
            新的String [] {PROJ_ID,PROJ_NAME},新的INT []
                    {R.id.projectId,R.id.projectName});
    //更新UI
    setListAdapter(projAdapter);
}
}
公共无效showLoader(最终弦乐味精)
{
runOnUiThread(新的Runnable()
{
@覆盖
公共无效的run()
    {
如果(对话== NULL)
    对话框=新的对话框(这一点,R.style.Theme_Dialog_Translucent);
dialog.setContentView(R.layout.loading);
dialog.setCancelable(真);
dialog.show();
ImageView的imgeView =(ImageView的)dialog.findViewById(R.id.imgeView);
TextView的tvLoading =(TextView中)dialog.findViewById(R.id.tvLoading);
如果(msg.length()大于0)
    tvLoading.setText(MSG);
imgeView.setBackgroundResource(R.anim.frame);
animationDrawable =(AnimationDrawable)imgeView.getBackground();
imgeView.post(新的Runnable()
{
 @覆盖
 公共无效的run()
 {
   如果(animationDrawable!= NULL)
    animationDrawable.start();
 }
});
 }
 });
 }
保护无效hideLoader()
{
    runOnUiThread(新的Runnable()
    {
        @覆盖
        公共无效的run()
        {
            如果(对话= NULL&放大器;!&安培; dialog.isShowing())
                dialog.dismiss();
        }
    });
}
}

frame.xml:

 <动画列表的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
<项目机器人:可绘制=@绘制/ loader1机器人:时间=50>< /项目>
<项目机器人:可绘制=@绘制/ loader2机器人:时间=50>< /项目>
<项目机器人:可绘制=@绘制/ loader3机器人:时间=50>< /项目>
<项目机器人:可绘制=@绘制/ loader4机器人:时间=50>< /项目>
<项目机器人:可绘制=@绘制/ loader5机器人:时间=50>< /项目>
<项目机器人:可绘制=@绘制/ loader6机器人:时间=50>< /项目>
<项目机器人:可绘制=@绘制/ loader7机器人:时间=50>< /项目>
<项目机器人:可绘制=@绘制/ loader8机器人:时间=50>< /项目>
<项目机器人:可绘制=@绘制/ loader9机器人:时间=50>< /项目>
<项目机器人:可绘制=@绘制/ loader10机器人:时间=50>< /项目>
<项目机器人:可绘制=@绘制/ loader11机器人:时间=50>< /项目>
<项目机器人:可绘制=@绘制/ loader12机器人:时间=50>< /项目>
< /动漫列表>

Loading.xml:

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:ID =@ + ID / llPopup
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:背景=#000
机器人:比重=中心
机器人:方向=垂直
机器人:填充=20dp>
< ImageView的
机器人:ID =@ + ID / imgeViews
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT/>
<的TextView
机器人:ID =@ + ID / tvLoading
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:单线=真
机器人:文字=载入中...
机器人:文字颜色=#FFFFFF
机器人:TEXTSIZE =18dp/>
< / LinearLayout中>

I want to display progressbar before loading list like following image. my class extended SherlockListFragment with AsyncTask. I goggled lot but not got suitable information for implementing progressbar . can anybody help me how to implement progressbar like following image.

Thanks in advance.

image 1

following is the my class

public class Residential extends SherlockListFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle saved) {

    View view = inflater.inflate(R.layout.residential_list,container, false);
    resListviewId = (ListView)view.findViewById(android.R.id.list);
    projectList = new ArrayList<HashMap<String,String>>();

    new LoadProjects().execute();

    return view;
}

public void onStart() {
    super.onStart();

}

//inner class for network operation
private class LoadProjects extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params) {
        return null;
    }

    @Override
    protected void onPostExecute(String result) {

        ListAdapter projAdapter = new SimpleAdapter(getSherlockActivity(),
                projectList, R.layout.residential_list_item, 
                new String[] {PROJ_ID,PROJ_NAME}, new int[] 
                        {R.id.projectId,R.id.projectName});
        //updating the UI
        setListAdapter(projAdapter);
    }
}
}

Following is the progressbar.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/projectLinearLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
    android:gravity="center_horizontal"
    android:padding="10dp"
    android:visibility="gone">

    <ProgressBar
        android:id="@+id/projectprogressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical" />

    <TextView 
        android:id="@+id/projectProgressTxtvw"
        android:text="@string/Loading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

解决方案

Try this. It may useful to you.

public class Residential extends SherlockListFragment 
{

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle saved) 
{
View view = inflater.inflate(R.layout.residential_list,container, false);
resListviewId = (ListView)view.findViewById(android.R.id.list);
projectList = new ArrayList<HashMap<String,String>>();

new LoadProjects().execute();

return view;
}
public void onStart() {
super.onStart();

}
private class LoadProjects extends AsyncTask<String, String, String> {

@Override
protected void onPreExecute() {
   showLoader("Loading...");
    super.onPreExecute();
}

@Override
protected String doInBackground(String... params) {
    return null;
}

@Override
protected void onPostExecute(String result) {

           hideLoader();
    ListAdapter projAdapter = new SimpleAdapter(getSherlockActivity(),
            projectList, R.layout.residential_list_item, 
            new String[] {PROJ_ID,PROJ_NAME}, new int[] 
                    {R.id.projectId,R.id.projectName});
    //updating the UI
    setListAdapter(projAdapter);
}
}
public void showLoader(final String msg)
{
runOnUiThread(new Runnable() 
{ 
@Override
public void run() 
    {
if(dialog == null)
    dialog = new Dialog(this, R.style.Theme_Dialog_Translucent);
dialog.setContentView(R.layout.loading);
dialog.setCancelable(true);
dialog.show();
ImageView imgeView = (ImageView) dialog.findViewById(R.id.imgeView);
TextView tvLoading = (TextView)dialog.findViewById(R.id.tvLoading);
if(msg.length() > 0)
    tvLoading.setText(msg);
imgeView.setBackgroundResource(R.anim.frame);
animationDrawable = (AnimationDrawable) imgeView.getBackground();
imgeView.post(new Runnable()
{
 @Override
 public void run() 
 {
   if(animationDrawable != null)
    animationDrawable.start();
 }
});
 }
 });
 }
protected void hideLoader()
{
    runOnUiThread(new Runnable() 
    {
        @Override
        public void run()
        {
            if(dialog != null && dialog.isShowing())
                dialog.dismiss();
        }
    });
}
}

frame.xml:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/loader1" android:duration="50"></item>
<item android:drawable="@drawable/loader2" android:duration="50"></item>
<item android:drawable="@drawable/loader3" android:duration="50"></item>
<item android:drawable="@drawable/loader4" android:duration="50"></item>
<item android:drawable="@drawable/loader5" android:duration="50"></item>
<item android:drawable="@drawable/loader6" android:duration="50"></item>
<item android:drawable="@drawable/loader7" android:duration="50"></item>
<item android:drawable="@drawable/loader8" android:duration="50"></item>
<item android:drawable="@drawable/loader9" android:duration="50"></item>
<item android:drawable="@drawable/loader10" android:duration="50"></item>
<item android:drawable="@drawable/loader11" android:duration="50"></item>
<item android:drawable="@drawable/loader12" android:duration="50"></item>
</animation-list>

Loading.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llPopup"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000"
android:gravity="center"
android:orientation="vertical"
android:padding="20dp" >
<ImageView
android:id="@+id/imgeViews"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tvLoading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="Loading..."
android:textColor="#FFFFFF"
android:textSize="18dp" />
</LinearLayout>

这篇关于如何在列表片段使用AsyncTask的实施进度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆