【问题标题】:Android ExpandableListActivity and TabActivity in same Class同一类中的 Android ExpandableListActivity 和 TabActivity
【发布时间】:2012-06-04 10:54:53
【问题描述】:

我正在尝试在我的程序中使用 ExpandableListActivity,但我已经在我的类中继承了 TabActivity。我不能进行多重继承,我需要在我的程序中同时使用 ExpandableListView 和 Tabs。 我在这里检查了 Google 的 ExpandableListView 代码http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ExpandableList1.html

但在这里他们继承了我无法做到的 ExpandableListActivity,因为我已经在我的班级中继承了 TabActivity。

我知道我可以使用 ListView 而无需在我的班级中继承 ListActivity。我也可以对 ExpandableListView 类做同样的事情吗?如果是这样,那么任何人都可以向我提供任何示例代码来使用 ExtendableListView 而不继承活动吗?

【问题讨论】:

    标签: android expandablelistview expandablelistadapter


    【解决方案1】:

    您可以拥有一个扩展 ExpandableListView 的自定义视图并在 TabActivity 中使用它。

    可扩展的列表视图

      public class CustomExpListView extends ExpandableListView{
              /** Your code **/
         }
    

    承载标签的主要活动

    public class MyTabActivity extends TabActivity{
        private TabHost tabhost;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              tabHost = getTabHost();
              tabHost.addTab(tabHost
                    .newTabSpec("Tab 1")
                    .setIndicator("Tab 1",
                            this.getResources().getDrawable(
                                    R.drawable.tab_icon))
                    .setContent(new Intent(this, Tab1.class)));
    
          /** Further code**/
        }
      }
    

    带有 exp 列表 (Tab 1) 的选项卡的 XML 布局文件

    /**Other layout stuff goes here**/
    <com.jmango.nexus.view.ProductListView
            android:layout_width="fill_parent"
                        android:layout_height="wrap_content" 
                        android:groupIndicator="@drawable/group_indicator"
            android:childIndicator="@drawable/child_indicator"
            android:cacheColorHint="#00000000" 
                        android:divider="#00BBFF"
                        android:childDivider="@android:drawable/divider_horizontal_dark"
            android:smoothScrollbar="true"
            android:id="@+id/exp_list_view"/>
    

    在带有 exp 列表的选项卡的类中

    CustomExpListView custExpListView = (CustomExpListView) this
                .findViewById(R.id.exp_list_view);
    

    您还可以扩展侦听器并对其进行自定义。

    希望对你有帮助!

    【讨论】:

    • 感谢您的回复,但是如何设置 ExpList 的适配器?
    • 非常感谢。我用你的想法解决了它。现在就像一个魅力。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 2010-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多