gpt4 book ai didi

android - 如何在列表 fragment 中使用 AsyncTask 实现进度条?

转载 作者:行者123 更新时间:2023-11-29 01:53:59 25 4
gpt4 key购买 nike

我想在加载列表之前显示进度条,如下图所示。我的类(class)用 AsyncTask 扩展了 SherlockListFragment。我看了很多,但没有得到实现 progressbar 的合适信息。任何人都可以帮助我如何实现如下图所示的进度条。

提前致谢。

图片 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>

最佳答案

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

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

框架.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>

加载.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>

关于android - 如何在列表 fragment 中使用 AsyncTask 实现进度条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16334089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com