在android中避免blankscreen的进度条

时间:2013-03-04 07:38:19

标签: android progress-bar

我是Android和Stackoverflow的新手,我需要一些关于在我的Android应用程序中开发进度条的帮助。我有两个活动,同时将意图从第一个活动传递到第二个iam试图在两个活动之间显示进度条。现在怀疑:是否可以在第二个活动的SetContentlayout之前放置进度条。因为我在第二个活动中有更多的表格布局&我需要花时间加载。我尝试了很多Stackoverflow的例子,但没有解决这个问题。请帮忙解决这个问题。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);



    setContentView(R.layout.activity_sub_products);      
         Intent subprointent=getIntent();

任何帮助都是值得建议的.thankyou。

修改: 这是我的第一个活动代码:

public class FrontPage extends Activity {
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_front_page);
    grid.setAdapter(new ImageAdapter(this));
     grid.setColumnWidth( 170 );
     grid.setVerticalSpacing(20 );
     grid.setStretchMode( GridView.STRETCH_COLUMN_WIDTH );
     grid.setNumColumns( GridView.AUTO_FIT );

     grid.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
             Intent subprointent=new Intent(FrontPage.this,SubProducts.class);
                startActivity(subprointent);

                  // your loading code goes here


        } 

     });



}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_front_page, menu);
    return true;
}
}

这是第二个活动代码:

public class SubProducts extends Activity {
private ProgressDialog pdlg;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);



    setContentView(R.layout.activity_sub_products);      

         Intent subprointent=getIntent();


        /* if (progDailog.isShowing()) {
                progDailog.dismiss();
            }*/

        Button b1=(Button)findViewById(R.id.subbutton);


        b1.setOnClickListener(new OnClickListener() {



            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Dialog settingsDialog = new Dialog(SubProducts.this);
                settingsDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
                settingsDialog.setContentView(getLayoutInflater().inflate(R.layout.cust_toast_layout , null));
                settingsDialog.show();
            }

         });

}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_sub_products, menu);
    return true;
}
}

3 个答案:

答案 0 :(得分:0)

这是一个关于asynctask和进度条的精彩教程:link

你应该在doInBackground时显示进度条并在onPostExecute中释放它

答案 1 :(得分:0)

然后你应该看看拆分你的布局。将您的主要布局放置为占位符。完成执行onCreate然后在onStart()中启动progess栏并加载表格布局主活动视图的子视图。

您的activity_sub_products布局应该像

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <LinearLayout
    android:id="@+id/placeholderView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

然后创建一个名为myTableViews.xml

的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 //put all your table views here
</LinearLayout>

然后覆盖活动中的onStart()并执行

@Override
protected void onStart()
{
 super.onStart();
 showProgressBar("Loading...");
 LinearLayout rlView =  (LinearLayout) findViewById(R.id.placeholderView);
  rlView.addView(getLayoutInflater().inflate(R.layout.myTableViews, null));
}
    public synchronized void  showProgressBar(final String message){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try{
                    if(pdlg == null){
                        pdlg = new ProgressDialog(MyAcitivity.this); 
                        pdlg.setCancelable(false); 
                        pdlg.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
                    }
                }catch (Exception e) {}

                try{
                    if(!pdlg.isShowing()) pdlg.show();
                    pdlg.setMessage(message); 
                }catch (Exception e) {}
            }
        });

    }

你需要移动

Button b1=(Button)findViewById(R.id.subbutton);


        b1.setOnClickListener(new OnClickListener() {



            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Dialog settingsDialog = new Dialog(SubProducts.this);
                settingsDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
                settingsDialog.setContentView(getLayoutInflater().inflate(R.layout.cust_toast_layout , null));
                settingsDialog.show();
            }

         });

到onStart()的末尾

答案 2 :(得分:0)

有一种简单的方法对我有用,我希望这对你有用... 在第一个活动中获取AsyncTask,并在protected Void doInBackground(Void... params)方法中执行第二个活动操作,然后调用您的意图,即startActivity();

class hello extends AsyncTask<Void,Void,Void>
{
       ProgressDialog dialog=null;
       Intent i;

   @Override
   protected void onPreExecute() 
   {


           dialog=ProgressDialog.show(MainActivity.this,"PLEASE WAIT","LOADING CONTENTS  ..",true);
   }

    @Override
    protected void onPostExecute(Void result) 
    {
            if(dialog.isShowing())
                    {
                       dialog.dismiss();

                    }           
    }

    @Override
    protected Void doInBackground(Void... params) 
    {

        // Perform your task Here and then call intent
        i = new Intent(MainActivity.this , Countries.class);
        startActivity(i);
        return null;
    }
}

快乐的编码;)

相关问题