2013-05-02 45 views
2

我想在加载列表像下面的图像之前显示进度条。我的课程使用AsyncTask扩展了SherlockListFragment。我瞪了很多,但没有得到适当的信息来实现进度条。任何人都可以帮助我如何实现像下面的图像进度条。如何在列表片段中使用AsyncTask实现进度条?

在此先感谢。

图像1

enter image description here

以下是我的课

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); 
    } 
} 
} 

以下是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> 

回答

1

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

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> 
+0

感谢您的帮助,我会尝试这一点,但这种装载机我能需要10个图像和你不需要在showLoader中编写runOnUiThread,因为onPreExecute和onPostExecute已经在uiThread上运行。 – 2013-05-02 10:36:48

+0

谢谢我知道runOnUiTread不需要onPreEx实际上我在我的BaseActivity类中使用了这两个方法,这是我的项目中所有屏幕的通用框架。请符合我是否真的有用。试着在iconfinder中准备10张循环流的其他副搜索图像。 – user2342404 2013-05-03 12:31:17