【问题标题】:Change tab host intent更改选项卡主机意图
【发布时间】:2011-12-25 09:20:32
【问题描述】:

我正在开发一个从互联网上提取信息的应用程序。信息分为类别、子类别和子子类别。

我的主视图是带有 3 个选项卡的 TabHost 视图(父类别)和初始列表视图(子类别)。当用户点击列表视图中的一个项目时,它会调用一个新的列表视图来显示所选子类别的子类别。

除了选择子类别时,tabHost 视图消失并且子子类别全屏显示之外,我得到了一切工作。

如何更改选项卡的意图以显示子类别的子类别?

编辑:这是我的代码,抱歉我没有早点发布!

包含标签主机的我的主视图:

public class tabwidget  extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tabs);

    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, category1Activity.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("category1").setIndicator("Category1",
                      res.getDrawable(R.drawable.ic_tab_category1))
                  .setContent(intent);
    tabHost.addTab(spec);

    // Do the same for the other tabs
    intent = new Intent().setClass(this, category2Activity.class);
    spec = tabHost.newTabSpec("category2").setIndicator("Category2",
                      res.getDrawable(R.drawable.ic_tab_category2))
                  .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, category3Activity.class);
    spec = tabHost.newTabSpec("category3").setIndicator("Category3",
                      res.getDrawable(R.drawable.ic_tab_category3))
                  .setContent(intent);
    tabHost.addTab(spec);


    tabHost.setCurrentTab(0);
}

当应用程序启动时,酒精标签默认被选中。这是 category1Acitivity 列表视图,带有调用子类别的 onlclick 操作:

listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
          //Toast.makeText(getApplicationContext(), "You clicked item at position"+position,
          //Toast.LENGTH_SHORT).show();

            Toast.makeText(getApplicationContext(), "Loading "+((TextView) view.findViewById(R.id.categoryname)).getText(),
            Toast.LENGTH_SHORT).show();

            Intent i = new Intent(category1Activity.this, subCategoryActivity.class);
            i.putExtra("id", ((TextView) view.findViewById(R.id.message)).getText());
            i.putExtra("catname", ((TextView) view.findViewById(R.id.categoryname)).getText());
            i.putExtra("parentcatid", "0");
            startActivityForResult(i, ACTIVITY_CREATE);




        }
    });

列表视图由发送到服务器的类别 ID 生成,从数据库中提取结果。

【问题讨论】:

    标签: android android-tabhost


    【解决方案1】:

    您必须使用ActivityGroups 来执行此操作。

    http://ericharlow.blogspot.com/2010/09/experience-multiple-android-activities.html

    http://united-coders.com/nico-heid/use-android-activitygroup-within-tabhost-to-show-different-activity

    但是,请注意 ICS 中已弃用 ActivityGroups。

    编辑:这是我对 ActivityGroup 的实现:

    标签中的活动:

    Intent i = new Intent(v.getContext(), SearchList.class);
    i.putExtra("search", search);
    
    View view = SearchActivityGroup.group.getLocalActivityManager()  
    .startActivity("SearchList", i  
    .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))  
    .getDecorView();  
    
    // Again, replace the view  
    SearchActivityGroup.group.replaceView(view);
    

    活动组:

    package nl.dante.SuperDeals;
    
    import java.util.ArrayList;
    
    import android.app.ActivityGroup;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    
    public class SearchActivityGroup extends ActivityGroup {
    
    View rootView;
    
    // Keep this in a static variable to make it accessible for all the nested
    // activities, lets them manipulate the view
    public static SearchActivityGroup group;
    
    // Need to keep track of the history if you want the back-button to work
    // properly, don't use this if your activities requires a lot of memory.
    private ArrayList<View> history;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /*
         * this.history = new ArrayList<View>(); group = this;
         * 
         * // Start the root activity within the group and get its view View
         * view = getLocalActivityManager().startActivity("Search", new
         * Intent(this,Search.class) .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
         * .getDecorView();
         * 
         * // Replace the view of this ActivityGroup replaceView(view);
         */
    
    }
    
    @Override
    protected void onResume() {
    
        super.onResume();
        this.history = new ArrayList<View>();
        group = this;
    
        // Start the root activity within the group and get its view
        View view = getLocalActivityManager().startActivity("Search", new Intent(this, Search.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
    
        // Replace the view of this ActivityGroup
        replaceView(view);
    }
    
    public void replaceView(View v) {
        // Adds the old one to history
        if (history.size() == 0) {
            if (rootView != null) {
                history.add(rootView);
                rootView = null;
            }
        }
        history.add(v);
        // Changes this Groups View to the new View.
        setContentView(v);
    }
    
    public void back() {
        try {
            if (history.size() > 0) {
                if (history.size() == 1) {
                    rootView = history.get(0);
                    Toasts.ToastImageView(this, "Druk nogmaals BACK om af te sluiten", R.drawable.power_64_off, "red");
                }
                history.remove(history.size() - 1);
                setContentView(history.get(history.size() - 1));
            } else {
                finish();
            }
            if (history.size() < 3) {
                // Tabhost.bannerImage2.setImageResource(0);
                Tabhost.banner.setBackgroundResource(R.drawable.gradient_blue);
            }
            if (history.size() == 2) {
                Tabhost.bannerImage1.setImageResource(R.drawable.sorteer_btn);
            }
        } catch (Exception ex) {
        }
    }
    
    public int getHistorySize() {
        return history.size();
    }
    
    @Override
    public void onBackPressed() {
        try {
            SearchActivityGroup.group.back();
        } catch (Exception ex) {
    
        }
        return;
    }
    }
    

    【讨论】:

    • 非常感谢!这是完美的,经过一些微调后,我设法让它工作:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-03
    • 2011-03-02
    • 2019-03-12
    • 1970-01-01
    • 1970-01-01
    • 2016-01-08
    相关资源
    最近更新 更多