通过ListView将ProgressBar放在前面

时间:2016-05-02 00:00:43

标签: android listview android-asynctask

我有一个RelativeLayout我有一个ListView和一个ProgressBarListView低于许多其他元素,ProgressBar在屏幕中居中。

现在,我使用AsyncTask并将数据加载到ListView,我想在执行此操作时显示ProgressBar

当列表为空时,ProgressBar可见。但是当列表已经有一些元素时,当我尝试加载和刷新列表中的数据时,ProgressBar没有显示。这可能是因为当我设置适配器时ListView到达顶部(ProgressBar之上)。

我尝试使用progressbar.bringtofront(),但没有运气。

这是我的布局。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

...

<com.nhaarman.listviewanimations.itemmanipulation.DynamicListView
    android:id="@+id/favourites_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/refresh_button"
    android:divider="@null"
    android:paddingTop="10dp" />

<ProgressBar
    android:id="@+id/refresh_progress"
    style="?android:attr/progressBarStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:visibility="invisible" />

</RelativeLayout>

由于我在.get()中使用了AsyncTask,我更新ProgressBar本身的Activity,而不是AsyncTask中的传统方式更新ProgressBar。以下是我用来显示和隐藏//before i send the asynctask refreshProgress.setVisibility(View.VISIBLE); refreshProgress.bringToFront(); ArrayList<String> response = task.execute(new String[]{url}).get(); ... //after parsing the response and setting the adapter for the listview favoritesAdapter.notifyDataSetChanged(); favoritesAdapter = new FavoritesAdapter(this, main_company, main_symbol, main_price, main_change, main_marketcap); favoritesListView.setAdapter(favoritesAdapter); refreshProgress.bringToFront(); refreshProgress.setVisibility(View.INVISIBLE); 的代码。

ProgressBar

我希望ListView在使用AsyncTask时显示在public class Four { private double [] numbers = {1.0, 2.0, 3.0, 4.0}; public double getContents(int index) { try { return numbers[index]; } catch(ArrayIndexOutOfBoundsException e) { return -1.0; } } } 之上。

谢谢!

3 个答案:

答案 0 :(得分:0)

尝试将DynamicListViewProgressBar包裹在FrameLayout中。由于FrameLayout的子视图是在堆栈中绘制的,因此最近添加的子项(您的ProgressBar)将显示在最顶层。

答案 1 :(得分:0)

我明白了。问题在于Asynctask。当我们使用.get()时,它变为同步并在主线程上等待任务返回。这样就无法打印ProgressBar。我做了一个异步任务(Asynctask =&gt; Asynchronous.Duh。),它就像魔法一样!

答案 2 :(得分:0)

或者ProgressBar的intead你可以使用ProgressDialog。您只需通过Asynctask中的publishProgress()和onProgressUpdate()将数据发布到ProgressDialog。这会容易得多。

class async extends AsyncTask<String,Integer,String>{

    ProgressDialog pd;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        pd=new ProgressDialog(MainActivity.this);
        pd.setTitle("Title");
        pd.setCancelable(false);
        pd.show();
    }

    @Override
    protected String doInBackground(String... params) {
        //Your stuff

        publishProgress(progress);//To update the ProgressDialog(Not neccessary)

        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        pd.dismiss();
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);

        pd.setMessage(values[0]+"% Completed");
    }
}
相关问题