Android 如何在列表片段中使用AsyncTask实现progressbar?
我想在加载列表之前显示progressbar,如下图所示。我的类使用AsyncTask扩展了SherlockListFragment。我看了很多东西,但没有得到实施progressbar的合适信息。谁能帮我实现progressbar,如下图所示 提前感谢。Android 如何在列表片段中使用AsyncTask实现progressbar?,android,android-progressbar,Android,Android Progressbar,我想在加载列表之前显示progressbar,如下图所示。我的类使用AsyncTask扩展了SherlockListFragment。我看了很多东西,但没有得到实施progressbar的合适信息。谁能帮我实现progressbar,如下图所示 提前感谢。图1 下面是我的课 public class Residential extends SherlockListFragment { @Override public View onCreateView(LayoutInflater infl
图1 下面是我的课
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);
}
}
}
公共类住宅扩展SherlockListFragment{
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、已保存的包){
视图=充气机。充气(右布局。住宅清单,容器,假);
resListviewId=(ListView)view.findViewById(android.R.id.list);
projectList=newarraylist();
新建LoadProjects().execute();
返回视图;
}
public void onStart(){
super.onStart();
}
//网络运营的内部类
私有类LoadProjects扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
返回null;
}
@凌驾
受保护的void onPostExecute(字符串结果){
ListAdapter projAdapter=新的SimpleAdapter(getSherlockActivity(),
项目清单,R.布局图.住宅清单项目,
新字符串[]{PROJ_ID,PROJ_NAME},新int[]
{R.id.projectId,R.id.projectName});
//更新用户界面
setListAdapter(ProjaAdapter);
}
}
}
下面是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>
试试这个。它可能对你有用
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();
}
});
}
}
公共类住宅扩展SherlockListFragment
{
创建视图上的公共视图(布局、充气机、视图组容器、已保存的包)
{
视图=充气机。充气(右布局。住宅清单,容器,假);
resListviewId=(ListView)view.findViewById(android.R.id.list);
projectList=newarraylist();
新建LoadProjects().execute();
返回视图;
}
public void onStart(){
super.onStart();
}
私有类LoadProjects扩展异步任务{
@凌驾
受保护的void onPreExecute(){
showLoader(“加载…”);
super.onPreExecute();
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
返回null;
}
@凌驾
受保护的void onPostExecute(字符串结果){
hideLoader();
ListAdapter projAdapter=新的SimpleAdapter(getSherlockActivity(),
项目清单,R.布局图.住宅清单项目,
新字符串[]{PROJ_ID,PROJ_NAME},新int[]
{R.id.projectId,R.id.projectName});
//更新用户界面
setListAdapter(ProjaAdapter);
}
}
公共void showLoader(最终字符串msg)
{
runOnUiThread(新的Runnable()
{
@凌驾
公开募捐
{
如果(对话框==null)
dialog=新建对话框(此,R.style.Theme\u对话框为半透明);
setContentView(R.layout.loading);
对话框。可设置可取消(true);
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()
{
@凌驾
公开募捐
{
if(animationDrawable!=null)
animationDrawable.start();
}
});
}
});
}
受保护的void hideLoader()
{
runOnUiThread(新的Runnable()
{
@凌驾
公开募捐
{
if(dialog!=null&&dialog.isShowing())
dialog.dismise();
}
});
}
}
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>
感谢您的帮助,我将尝试此方法,但对于此加载程序,我需要10个图像,您不需要在showLoader中写入RunnuiThread,因为onPreExecute和onPostExecute已经在uiThread上运行。谢谢,我知道RunnuiTRead不需要在onPreEx上运行。。。实际上,我在我的BaseActivity类中使用了这两种方法,它是我项目中所有屏幕的公共框架。请告诉我它是否真的有用。在iconfinder中准备10张循环流其他副搜索的图像。