2013-07-14 56 views
0

Android开发中最常见的用例之一是在加载片段数据时显示加载进度和加载消息。在数据加载时显示不确定进度+加载消息片段

主Fragment类及其子类具有默认的空视图,其中包含不确定的进度。但是,如果没有显示加载消息的能力 - 例如,获取数据 -

我想知道您对实现此用例的最佳实践的看法。

在此先感谢。 :)

回答

1

您可以使用AsyncTask加载数据并让它返回指示任务进度的值。您可以使用进度条创建要显示的视图,然后创建asynctask并传递活动上下文和进度条。

public class Loader extends AsyncTask<>{ 

ProgressBar progress; 
Context context; 
public Loader(Context context, ProgressBar progress) 
{ 
this.progress = progress; 
this.context = context; 
} 

public Integer doInBackground() 
{ 
    // do your loading here and determine what percent is done and call publishProgress() 
} 

public void onProgressUpdate(Integer... value) 
{ 
    final Integer progressVal = value; 

    Runnable updateProg = new Runnable(){ 
    public void run(){ 
     this.progress.setProgress(progressVal); 
    }}; 

    Handler main = new Handler(context.getMainLooper()); 
    main.post(updateProg); 

} 
相关问题