【问题标题】:How to add dynamic tabs and dynamic expandable list view inside it in Android?如何在 Android 中添加动态选项卡和动态可扩展列表视图?
【发布时间】:2015-07-30 09:30:02
【问题描述】:

我只想根据可用的数字类别动态创建一个选项卡。在每个选项卡内,我需要在每个选项卡内动态创建多个可扩展列表视图。 (例如:假设我有四个类别,如教育、体育、科学、航空航天)然后我需要在运行时间上创建四个选项卡,它会根据我获得的类别数量(一个或二十个或更多)而变化。

在每个选项卡中,我需要(动态)创建多个可展开的列表视图。

经过大量研究,我找到了一些相关链接(hereherehere 等)。我更愿意使用我不知道的正确有效的方法。

【问题讨论】:

    标签: android tabs pagerslidingtabstrip


    【解决方案1】:

    你需要这样考虑

    TAB2 = TAB3 = TAB1 = Fragment---- 在此您可以执行可扩展列表视图,该列表视图将在布局本身中定义,但参考 TAB'S 您需要更新可扩展列表视图。

    对于动态 Tabview,您可以这样做 您应该通过指定来指定 TabSpec

    视图的 id,或 创建视图内容的 TabHost.TabContentFactory 您的代码两者兼而有之!

    将您的代码更改为以下之一:

    for (int i=1; i<=n; i++){
       final String tabName = "tab" + Integer.toString(i);
       final TabSpec ourSpec = th.newTabSpec(tabName);
       ourSpec.setContent(new TabHost.TabContentFactory() { 
          public View createTabContent(String tag) { 
              TextView text = new TextView(NewTicket.this);
              String tabText = tabName;
              text.setText("You've created a new tab " + tabText); 
              return (text);
          }
       });
       ourSpec.setIndicator(tabName);
       tabhost.addTab(ourSpec);
      }
    
    or
    
     for (int i=1; i<=n; i++){
       final String tabName = "tab" + Integer.toString(i);
       final TabSpec ourSpec = th.newTabSpec(tabName);
       ourSpec.setContent(R.id.llItemList);   
       ourSpec.setIndicator(tabName);
        tabhost.addTab(ourSpec);
      }
    

    要保留您评论中提到的相同 ListView 实例,请执行以下操作:

    注册一个 TabHost.OnTabChangeListener 当 tab 改变时,你应该首先调用 ListView 的当前父级的 removeView() ,然后 addView() 它到新的父级。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-19
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-23
      • 1970-01-01
      相关资源
      最近更新 更多