简体   繁体   English

如何在Android中在其中添加动态标签和动态扩展列表视图?

[英]How to add dynamic tabs and dynamic expandable list view inside it in Android?

I just want to create a tabs dynamically according to the number category available. 我只想根据可用的数字类别动态创建标签。 Inside each tab I need to create a number of Expandable list views dynamically inside the each tab. 在每个标签内,我需要在每个标签内动态创建许多可扩展列表视图。 (Example: say I got four categories like education, sports, science, aerospace) then I need to create four tabs on run time, it will change according to the number categories I am getting (one or twenty or more). (例如:说我有四个类别,例如教育,体育,科学,航空航天),那么我需要在运行时创建四个选项卡,它将根据我所获得的数字类别(一个或二十个或更多)而变化。

Inside each tab I need to create number of expandable list views (dynamically). 在每个标签内,我需要(动态)创建多个可扩展列表视图。

After much research, I found some related links ( here , here , here and more). 经过大量研究,我发现了一些相关链接( 此处此处此处以及更多)。 I would prefer to go with the proper and efficient method which I am not aware of. 我宁愿使用我不知道的适当而有效的方法。

you need to consider as like this 你需要这样考虑

TAB2 = TAB3 = TAB1 = Fragement---- In this you can do expandable listview which will be defined in layout itself but with the reference of TAB'S you need to Update the expandable listview. TAB2 = TAB3 = TAB1 =碎片化----在这种情况下,您可以执行可扩展列表视图,该列表视图将在布局本身中定义,但需要使用TAB的引用来更新可扩展列表视图。

For dynamic Tabview you can do like this You should specify a TabSpec either by specifying 对于动态Tabview,您可以这样操作:您应该通过指定

the id of a View, or a TabHost.TabContentFactory that creates the View content Your code does both! View的ID或创建View内容的TabHost.TabContentFactory。

Change your code to one of the following: 将您的代码更改为以下之一:

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

To preserve the same instance of ListView, as mentioned in your comment, do the following: 要保留您的注释中提到的ListView的相同实例,请执行以下操作:

Register a TabHost.OnTabChangeListener When the tab changes, you should first call removeView() on the ListView's current parent and then addView() it to the new parent. 注册TabHost.OnTabChangeListener当选项卡更改时,应首先在ListView的当前父级上调用removeView(),然后将其添加到新的父级上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM