Android 加载 ListView 时的 ProgressBar(使用 AsyncTask)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12280336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:18:38  来源:igfitidea点击:

ProgressBar while loading ListView (using AsyncTask)

androidandroid-layoutandroid-ui

提问by ohadinho

I'm trying to show a progress bar when loading a custom ListView and afterwards - hide it. I'm using ASync task, but for some reason - The content view is not set and the previous layout view is stuck until all the list view is loaded.

我试图在加载自定义 ListView 时显示进度条,然后将其隐藏。我正在使用 ASync 任务,但出于某种原因 - 未设置内容视图,并且在加载所有列表视图之前之前的布局视图被卡住。

Here is my code:

这是我的代码:

private ListView listViewGameResults;   
    protected View dialogLayout;
    protected ArrayList<Game> listGames;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.adresults);
        GameResultsLoader gameResultsLoader = new GameResultsLoader();              
        gameResultsLoader.execute();        
    }

    private class GameResultsLoader extends AsyncTask<Void, Void, Void> {       
        private GameResultsAdapter adapter;

        public GameResultsLoader() {            
        }

        @Override
        protected void onPreExecute() { 
        }

        @Override
        protected Void doInBackground(Void... params) { 
            try {
            listGames = GameResultsCache.getInstance().getGameResults();
            adapter = new GameResultsAdapter(getBaseContext(), listGames);
            listViewGameResults = (ListView)findViewById(R.id.listViewGameResults);
            } 
            catch (Exception e) {
                // TODO Auto-generated catch block
                finish();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void res) {
            listViewGameResults.setAdapter(adapter);
            listViewGameResults.setDivider(null);
            listViewGameResults.setDividerHeight(0);
            ProgressBar pb = (ProgressBar)findViewById(R.id.progressbar_loading);
            pb.setVisibility(View.GONE);            
        }
    }

ProgressBar and ListView in my layout:

我的布局中的 ProgressBar 和 ListView:

<ProgressBar
       style="?android:attr/progressBarStyle"
       android:layout_centerInParent="true"
       android:id="@+id/progressbar_loading"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" />
    <ListView
         android:id="@+id/listViewGameResults"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:layout_marginTop="1dip"
         android:layout_below="@+id/upperstrip"
         android:layout_above="@+id/ivDownStrip" />

回答by Dilip

You need to set default visibility of progressBargone.and onPreExecute()set Visible and onPostExecute()set gone.

您需要设置消失的默认可见性progressBaronPreExecute()设置可见并onPostExecute()设置消失。

<ProgressBar
       style="?android:attr/progressBarStyle"
       android:layout_centerInParent="true"
       android:id="@+id/progressbar_loading"
       android:visibility="gone"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" />
    <ListView
         android:id="@+id/listViewGameResults"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:layout_marginTop="1dip"
         android:layout_below="@+id/upperstrip"
         android:layout_above="@+id/ivDownStrip" />

your Activity should look like this

你的活动应该是这样的

public class demo extends Activity{
    private ListView listViewGameResults;   
        protected View dialogLayout;
        protected ArrayList<Game> listGames;
    progressBar progress;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState); 
            setContentView(R.layout.adresults);
            progress=(ProgressBar)findViewByid(R.id.progressbar_loading);
            GameResultsLoader gameResultsLoader = new GameResultsLoader(this);              
            gameResultsLoader.execute();        
        }
    }

Use one separate class for AsyncTask

使用一个单独的类 AsyncTask

public class GameResultsLoader extends AsyncTask<Void, Void, Void> {       
        private GameResultsAdapter adapter;
        Demo demo;
        public GameResultsLoader(Demo demo) {   
        this.demo=demo;         
        }

        @Override
        protected void onPreExecute() { 
        demo.progress.setvisibility(View.Visible);
        }

        @Override
        protected Void doInBackground(Void... params) { 
            try {
            listGames = GameResultsCache.getInstance().getGameResults();
            adapter = new GameResultsAdapter(getBaseContext(), listGames);
            listViewGameResults = (ListView)findViewById(R.id.listViewGameResults);
            } 
            catch (Exception e) {
                // TODO Auto-generated catch block
                finish();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void res) {
            listViewGameResults.setAdapter(adapter);
            listViewGameResults.setDivider(null);
            listViewGameResults.setDividerHeight(0);
            ProgressBar pb = (ProgressBar)findViewById(R.id.progressbar_loading);
            demo.progress.setVisibility(View.GONE);            
        }
    }

回答by Pankaj Savaliya

if your using asnkTask that is work on main Thread so application is much more load.so better solution is using Service.class. downlaod this exampleCheck this link all are solution are there and download it: you have to also get list from relaunch this application. https://github.com/PankajSavaliyaGit/Upload-List

如果您使用在主线程上工作的 asnkTask,那么应用程序的负载要大得多。所以更好的解决方案是使用 Service.class。 下载此示例检查此链接是否有解决方案并下载它:您还必须从重新启动此应用程序中获取列表。 https://github.com/PankajSavaliyaGit/Upload-List

回答by Obeda Al Ale

The first thing you can do is put Progress bar code after OnCreatemethod. But the best thing is to create a simple method for Progress bar and only call it in OnCreatemethod.

您可以做的第一件事是在OnCreate方法之后放置进度条码。但最好的办法是为 Progress bar 创建一个简单的方法,并且只在OnCreate方法中调用它。

The following code worked fine for me:

以下代码对我来说很好用:

private void showDialog() {
    progressDialog = new ProgressDialog(this);
    progressDialog.setTitle("Have a nice time");
    progressDialog.setMessage("Please wait while showing Titles :) ");
    progressDialog.setCancelable(true);
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    progressDialog.show();
}

And then call this method in OnCreate

然后调用这个方法 OnCreate

showDialog();

You can stop Progress when the data is ready to view using:

当数据准备好查看时,您可以停止 Progress 使用:

progressDialog.dismiss();

I hope this is helpful.

我希望这是有帮助的。

回答by NaserShaikh

you have to write code to start progressDialog in preExecute method and at the postExecute dismiss it.

您必须编写代码以在 preExecute 方法中启动 progressDialog 并在 postExecute 关闭它。

@Override
protected void onPreExecute() {
     proDialog = new ProgressDialog(context);
     proDialog.setTitle("App name");
     proDialog.setMessage("Loding...");
     proDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
     //proDialog.setIcon(R.drawable.)
     proDialog.setCancelable(true);
     proDialog.show();
}

@Override
protected void onPostExecute(Void res) {
     proDialog.dismiss();
}