进度而加载的ListView(使用的AsyncTask)(ProgressBar while loa

2019-06-27 20:26发布

我想,当加载自定义ListView和事后显示一个进度条 - 隐藏它。 我使用的异步任务,但由于某种原因 - 内容视图尚未设置,直到所有的列表视图被加载之前的布局视图被卡住。

这里是我的代码:

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

进度和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" />

Answer 1:

您需要设置的默认知名度progressBar gone.and onPreExecute()设置可见光和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" />

您的活动应该是这样的

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

使用一个单独的类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);            
        }
    }


Answer 2:

如果您使用asnkTask是在主线程工作,让应用更加load.so更好的解决方案是使用Service.class。 立即下载这个例子检查此链接都是解决方案在那里,下载:你也必须得到重新开张清单此应用程序。 https://github.com/PankajSavaliyaGit/Upload-List



Answer 3:

你必须写代码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();
}


Answer 4:

你可以做的第一件事就是把进度条代码之后OnCreate方法。 但是,最好的办法是创建进度条的简单方法,只调用它OnCreate方法。

下面的代码工作对我罚款:

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

然后调用这个方法OnCreate

showDialog();

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

progressDialog.dismiss();

我希望这是有帮助的。



文章来源: ProgressBar while loading ListView (using AsyncTask)