Android 在ListView的顶部显示进度条

Android 在ListView的顶部显示进度条,android,android-layout,android-listview,android-linearlayout,Android,Android Layout,Android Listview,Android Linearlayout,问一个最简单的问题,我已经搜索了很多与我的问题相关的东西,但没有找到任何解决方案。我在Android应用程序中有一个布局,其中我将listview与progressbar一起使用。在AsyncTask类中执行的所有任务。在加载列表之前,我想在活动中心显示进度条。谁能告诉我哪里出了问题,或者这个解决方案需要做哪些更改。提前谢谢 <ProgressBar android:id="@+id/progressBar1" android:layout_width="wrap_cont

问一个最简单的问题,我已经搜索了很多与我的问题相关的东西,但没有找到任何解决方案。我在Android应用程序中有一个布局,其中我将
listview
progressbar
一起使用。在AsyncTask类中执行的所有任务。在加载列表之前,我想在活动中心显示进度条。谁能告诉我哪里出了问题,或者这个解决方案需要做哪些更改。提前谢谢

<ProgressBar
    android:id="@+id/progressBar1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_gravity="center_horizontal"
    android:visibility="invisible" />

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="fill_parent" >
</ListView>
我是如何尝试的

<ProgressBar
    android:id="@+id/progressBar1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_gravity="center_horizontal"
    android:visibility="invisible" />

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="fill_parent" >
</ListView>
活动\u main:

<ProgressBar
    android:id="@+id/progressBar1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_gravity="center_horizontal"
    android:visibility="invisible" />

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="fill_parent" >
</ListView>

<ProgressBar
    android:id="@+id/progressBar1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_gravity="center_horizontal"
    android:visibility="invisible" />

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="fill_parent" >
</ListView>
main活动:

<ProgressBar
    android:id="@+id/progressBar1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_gravity="center_horizontal"
    android:visibility="invisible" />

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="fill_parent" >
</ListView>
public class GettingAlerts extends ListActivity {

// Initialization
IPAddress ipAddressObject = new IPAddress();
public static String GETTING_ALERTS_URL = "http://"
        + IPAddress.IP_Address.toString()
        + "/MyServices/Alerts/AlertService.svc/gettingAllAlerts";

// TAGS
public static String TAG_NAME = "GetAllAlertsResult";
public static String TAG_ALERT_TITLE = "alertTitle";
public static String TAG_ALERT_DESCRIPTION = "alertDescription";
public static String TAG_ALERT_OWNER = "alertOwner";
public static String TAG_ALERT_DEADLINE = "alertDeadline";
static String Serv_Response = "";

ArrayList<Alerts> alertsList;
ProgressBar pg;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.alert_activity_list);

    pg = (ProgressBar) findViewById(R.id.progressBar1);
    // requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    // setProgressBarIndeterminateVisibility(true);

    alertsList = new ArrayList<Alerts>();
    ListView lv = getListView();

    // setProgressBarIndeterminateVisibility(false);

    new GettingAlertsList().execute();
}

private class GettingAlertsList extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub

        // LinearLayout linlaHeaderProgress = (LinearLayout)
        // findViewById(R.id.linlaHeaderProgress);
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(GETTING_ALERTS_URL);
        HttpResponse httpResponse = null;
        try {
            httpResponse = httpClient.execute(httpGet);

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        HttpEntity httpEntity = httpResponse.getEntity();
        try {
            Serv_Response = EntityUtils.toString(httpEntity);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            if (Serv_Response != null) {
                JSONObject jsonObj = new JSONObject(Serv_Response);
                JSONArray quiz = jsonObj.getJSONArray(TAG_NAME);

                for (int i = 0; i < quiz.length(); i++) {
                    JSONObject c = quiz.getJSONObject(i);
                    String alert_title = c.getString(TAG_ALERT_TITLE);
                    String alert_owner = c.getString(TAG_ALERT_OWNER);
                    String alert_desc = c.getString(TAG_ALERT_DESCRIPTION);
                    String alert_dealdine = c.getString(TAG_ALERT_DEADLINE);

                    Alerts alertObject = new Alerts();
                    alertObject.setAlertTitle(alert_title);
                    alertObject.setAlertDescription(alert_desc);
                    alertObject.setAlertDeadline(alert_dealdine);
                    alertObject.setAlertOwner(alert_owner);

                    alertsList.add(alertObject);
                }

            }

        } catch (JSONException e) {
            // TODO: handle exception
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);

        CustomAlertListAdapter adapter = new CustomAlertListAdapter(
                GettingAlerts.this, alertsList);
        setListAdapter(adapter);

        pg.setVisibility(View.GONE);
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        pg.setVisibility(View.VISIBLE);
    }

}

}
公共类GettingAlerts扩展ListActivity{
//初始化
IPAddress ipAddressObject=新IPAddress();
获取警报的公共静态字符串\u URL=“http://”
+IPAddress.IP_Address.toString()
+“/MyServices/Alerts/AlertService.svc/gettingAllAlerts”;
//标签
公共静态字符串TAG_NAME=“GetAllAllertsResult”;
公共静态字符串标记\u ALERT\u TITLE=“alertTitle”;
公共静态字符串标记\u ALERT\u DESCRIPTION=“alertDescription”;
公共静态字符串标记\u ALERT\u OWNER=“alertOwner”;
公共静态字符串TAG\u ALERT\u DEADLINE=“alertDeadline”;
静态字符串Serv_Response=“”;
ArrayList alertsList;
ProgressBar pg;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.alert\u活动\u列表);
pg=(ProgressBar)findViewById(R.id.progressBar1);
//requestWindowFeature(Window.FEATURE\u不确定\u进度);
//SetProgressBarInDeterminateVibility(真);
alertsList=新的ArrayList();
ListView lv=getListView();
//SetProgressBarInDeterminateVibility(假);
新建GettingAlertsList().execute();
}
私有类GettingAlertsList扩展了异步任务{
@凌驾
受保护的Void doInBackground(Void…参数){
//TODO自动生成的方法存根
//线性布局linlaHeaderProgress=(线性布局)
//findViewById(R.id.linlaHeaderProgress);
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpGet HttpGet=新的HttpGet(获取警报\u URL);
HttpResponse HttpResponse=null;
试一试{
httpResponse=httpClient.execute(httpGet);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
HttpEntity HttpEntity=httpResponse.getEntity();
试一试{
Serv_Response=EntityUtils.toString(httpEntity);
}捕获(解析异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
试一试{
if(服务响应!=null){
JSONObject jsonObj=新的JSONObject(Serv_响应);
JSONArray quick=jsonObj.getJSONArray(标记名称);
对于(int i=0;i
图像

<ProgressBar
    android:id="@+id/progressBar1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_gravity="center_horizontal"
    android:visibility="invisible" />

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="fill_parent" >
</ListView>

<ProgressBar
    android:id="@+id/progressBar1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_gravity="center_horizontal"
    android:visibility="invisible" />

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="fill_parent" >
</ListView>

如您所见,进度条位于活动顶部。在加载列表之前,我希望活动的
center
中有progressbar

更改可见性以显示/隐藏进度条。您可能需要设置以更改某些属性,如样式:

<ProgressBar
    android:id="@+id/progressBar1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_gravity="center_horizontal"
    android:visibility="invisible" />

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="fill_parent" >
</ListView>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <RelativeLayout
        android:id="@+id/progress_bar_wrapper"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@null"
        android:visibility="gone">

        <ProgressBar
            android:id="@+id/progress_bar"
            android:layout_width="100dp"
            android:layout_height="30dp"
            android:progress="0"
            android:layout_centerHorizontal="true"
            android:layout_centerInParent="true"
            android:layout_centerVertical="true"/>

    </RelativeLayout>

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</RelativeLayout>

我假设您当前使用了
LinearLayout
,而是以XML格式获取
RelativeLayout
,更改小部件的顺序,并从ProgressBar中删除
android:layout\u alignParentTop=“true”

<ProgressBar
    android:id="@+id/progressBar1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_gravity="center_horizontal"
    android:visibility="invisible" />

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="fill_parent" >
</ListView>
<RelativeLayout
  ...
  ...>

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="fill_parent" >
    </ListView>

    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"/>

</RelativeLayout>

试试这个

<ProgressBar
    android:id="@+id/progressBar1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_gravity="center_horizontal"
    android:visibility="invisible" />

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="fill_parent" >
</ListView>
删除
android:layout\u alignParentTop=“true”
android:layout\u gravity=“center\u horizontal”
并添加
android:layout\u centerInParent=“true”

<ProgressBar
    android:id="@+id/progressBar1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_gravity="center_horizontal"
    android:visibility="invisible" />

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="fill_parent" >
</ListView>


您是否考虑过使用
ProgressDialog
来代替?谢谢@MikeM的回复。你不认为progressbar(圆形)是解决这个问题的合适方法吗?为什么你要求子布局只显示/隐藏progressbar?@PareshMayani display/hide??这将使进度条居中,并覆盖列表,直到需要时加载。可见/消失。不需要子布局,因为它增加了布局层次。为什么不能使用LinearLayout?如果它是c,为什么要使用LinearLayout