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

oha*_*nho 5 android android-ui android-layout

我正在尝试在加载自定义ListView时显示进度条,然后隐藏它.我正在使用ASync任务,但出于某种原因 - 未设置内容视图,并且在加载所有列表视图之前,先前的布局视图一直处于停滞状态.

这是我的代码:

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);            
        }
    }
Run Code Online (Sandbox Code Playgroud)

我的布局中的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" />
Run Code Online (Sandbox Code Playgroud)

Dil*_*lip 5

您需要设置progressBargone的默认可见性并onPreExecute()设置Visible并onPostExecute()设置为off .

<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" />
Run Code Online (Sandbox Code Playgroud)

你的活动应该是这样的

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();        
        }
    }
Run Code Online (Sandbox Code Playgroud)

使用一个单独的类 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);            
        }
    }
Run Code Online (Sandbox Code Playgroud)


Nas*_*ikh 0

您必须编写代码以在 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();
}
Run Code Online (Sandbox Code Playgroud)