【问题标题】:How to get supportactionbar when my class extends ExpandableListActivity当我的课程扩展 ExpandableListActivity 时如何获得支持操作栏
【发布时间】:2014-02-25 07:07:20
【问题描述】:

我有一个扩展 ExpandableListActivity 的类以将我的数据显示为可解释的列表视图,现在我的应用程序支持旧版本直到 API 7 如何使用 getSupportActionBar() 添加 ActionBar 到这个类...请帮帮我

public class Facts extends ExpandableListActivity {


public void onCreate(Bundle savedInstanceState) {

    try{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.facts);

        SimpleExpandableListAdapter expListAdapter =
                new SimpleExpandableListAdapter(
                        this,
                        createGroupList(),              // Creating group List.
                        R.layout.facts_grouprow,                // Group item layout XML.
                        new String[] { "questions" },   // the key of group item.
                        new int[] { R.id.row_name },    // ID of each group item.-Data under the key goes into this TextView.
                        createChildList(),              // childData describes second-level entries.
                        R.layout.facts_childrow,                // Layout for sub-level entries(second level).
                        new String[] {"answers"},       // Keys in childData maps to display.
                        new int[] { R.id.grp_child}     // Data under the keys above go into these TextViews.
                        );
        setListAdapter(expListAdapter);     // setting the adapter in the list

    }catch(Exception e){
        System.out.println("Errrr +++ " + e.getMessage());
    }

}

现在建议我如何实现它。

【问题讨论】:

    标签: android android-actionbar android-support-library android-actionbar-compat


    【解决方案1】:

    看看ExpandableListActivity source code,似乎很容易在你自己的类中复制相同的功能并将ActionBarActivity扩展为它的基类。

    【讨论】:

    • 你是说我必须编写自定义适配器并将其扩展到 BaseExpandableListAdapter 才能实现这一点。..
    【解决方案2】:

    我建议您使用ActionBarActivity。你有方法getSupportActionBar

    要显示您的 ExpandableList,您可以从 support-v4 库中添加 ListFragment。 有一个方法叫setListAdapter(),你可以在其中添加ExpandableListAdapter

    Here is an example.

    【讨论】:

    • 您能否详细说明我的问题上下文,因为我已经编辑了我的问题,还提供链接作为示例不使用列表使用列表片段,它只使用扩展 BaseExpandableListAdapter 的自定义适配器
    • 我添加示例是因为我不知道您是否已经使用过Fragments。您还可以将您的SimpleExpandableListAdapter 添加到setListAdapter,这样您就不必编写自定义的ExpandableListAdapter
    • 如果我解释错了,请纠正我。你是说我的Facts 类应该扩展ActionBarActivity,从它调用一个扩展ListFragment 的片段,在那个ListFragment 我可以使用SimpleExpandableListAdaptersetListAdapter
    • ok @owe 让我这样做,如果工作正常,请接受您的回答...感谢您的建议
    猜你喜欢
    • 1970-01-01
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多