在Android中创建Tabhost并从AsyncTask添加规范

时间:2012-05-04 08:15:46

标签: android android-asynctask android-tabhost

我正在尝试在其中创建一个tabbar和specs,但问题是我需要在asycn任务中执行它,所以它没有TabActivity。在下面运行此代码后,我只看到我的主要布局。最后我想要做的是,我想向用户显示一个进度条,说明安装应用程序后第一次复制必要的文件,同时复制文件然后创建tabHost并添加规格。如果没有asyncTask,它的效果非常好,但是第一次运行应用程序时,屏幕会被锁定20或30秒,直到复制过程完成。任何的想法 ?提前谢谢。

public class AsyncTest extends AsyncTask<Void, Void, Void> {

Context context;
DataBaseJSONFunctions json;

TabHost tabHost;

TabWidget tabWidget;

Resources res;

TabHost.TabSpec sp;

Intent intent;

ProgressDialog dialog;

Activity ac;

public AsyncTest(Context context, TabHost tabHost, TabHost.TabSpec sp, Bundle savedInstanceState) {
    this.context = context;
    json = new DataBaseJSONFunctions(context);
    this.tabHost = tabHost;
    tabWidget = tabHost.getTabWidget();
    this.sp = sp;
    ac = (Activity) context;
    res = context.getResources();

    LocalActivityManager mlam = new LocalActivityManager(ac, false);
    mlam.dispatchCreate(savedInstanceState);
    tabHost.setup(mlam );

}

@Override
protected Void doInBackground(Void... params) {
    initializeAll();
    return null;
}

@Override
protected void onPostExecute(Void result) {

    dialog.dismiss();

    // to go through to the another activity in the tab I need to initialize an intent.
    // and I need to set the Tab bar and it's icon.
    intent = new Intent().setClass(ac, Activities.class);
    sp = tabHost.newTabSpec("activities").setIndicator("activities",res.getDrawable(R.drawable.tab_activities_selector)).setContent(intent);
    tabHost.addTab(sp);

    // doing the same things for Songs Activity.
    intent = new Intent().setClass(ac, Promotions.class);
    sp = tabHost.newTabSpec("promotions").setIndicator("promotions",res.getDrawable(R.drawable.tab_promotions_selector)).setContent(intent);
    tabHost.addTab(sp);


    // doing the same things for another Activity.
    intent = new Intent().setClass(ac,Menu.class);
    sp = tabHost.newTabSpec("menu").setIndicator("Menu",res.getDrawable(R.drawable.tab_menu_selector)).setContent(intent);
    tabHost.addTab(sp);


    intent = new Intent().setClass(ac, Gallery.class);
    sp = tabHost.newTabSpec("gallery").setIndicator("Gallery",res.getDrawable(R.drawable.tab_gallery_selector)).setContent(intent);
    tabHost.addTab(sp);


    intent = new Intent().setClass(ac, Info.class);
    sp = tabHost.newTabSpec("info").setIndicator("Info",res.getDrawable(R.drawable.tab_info_selector)).setContent(intent);
    tabHost.addTab(sp);

    for(int i = 0; i < tabWidget.getChildCount(); i++){
        tabWidget.getChildAt(i).setBackgroundResource(R.drawable.tab_bar);
    }

    tabHost.setCurrentTab(0);

    // Starting location listener service.
    ac.startService(new Intent(ac, LocationService.class));

    ac.setContentView(R.layout.tabbar_main);
    super.onPostExecute(result);
}

@Override
protected void onPreExecute() {
    dialog = ProgressDialog.show(context, "", "Copying files please wait...");
    super.onPreExecute();
}

2 个答案:

答案 0 :(得分:0)

用于标签 布局你的xml应该像这样;

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0" />
        </LinearLayout>
    </TabHost>


</RelativeLayout>

答案 1 :(得分:0)

我找到了这样做的方法,我有一个图像视图,其可视性为GONE,而在onPreExecute()中,我将此图像的可见性设置为VISIBLE。还有onPostExecute()我像以前一样使它变得像GONE一样。