2

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

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



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

任何帮助都是可建议的。谢谢。

编辑:这是我的第一个活动代码:

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

3 回答 3

0

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

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

于 2013-03-04T07:46:28.430 回答
0

那么你应该考虑拆分你的布局。将您的主要布局作为一个占位符。完成 onCreate 执行,然后在 onStart() 中启动进度条并加载表格布局主活动视图的子视图。

你的 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() 结束

于 2013-03-04T07:49:00.497 回答
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;
    }
}

快乐编码;)

于 2013-09-26T11:41:54.427 回答