【问题标题】:Activity into List Fragment活动到列表片段
【发布时间】:2013-09-16 08:00:56
【问题描述】:

以下代码当前正在 Activity 中运行。但我想将它用于片段,当我尝试时它无法识别我的可扩展列表。如果有人可以帮助我,那将是一个很大的帮助。

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_expandable_list);
    createGroupList();
    createCollection();
    expListView = (ExpandableListView) findViewById(R.id.my_exp_list);
    final ExpandableListAdapter expListAdapter = new ExpandableListAdapter(
            this, groupList, laptopCollection);
    expListView.setAdapter(expListAdapter);

    setGroupIndicatorToRight();

    expListView.setOnChildClickListener(new OnChildClickListener() {

        public boolean onChildClick(ExpandableListView parent, View v,
                                    int groupPosition, int childPosition, long id) {
            final String selected = (String) expListAdapter.getChild(
                    groupPosition, childPosition);
            Toast.makeText(getBaseContext(), selected, Toast.LENGTH_LONG)
                    .show();

            return true;
        }
    });
}

在我的片段 oncreateView 中,

View v = inflater.inflate(R.layout.activity_expandable_list, container, false);  createGroupList();
    createCollection();
    expListView = (ExpandableListView) inflater.inflate(R.id.my_exp_list, container, false);

【问题讨论】:

  • 你是如何引用片段中的可扩展列表的?
  • “不被识别”是什么意思?发布您用于片段的代码,显示有效的代码无济于事:)

标签: android android-layout android-listview expandablelistview android-listfragment


【解决方案1】:

您正在夸大 2 个不同的视图,或者至少尝试这样做。您必须给 View 充气,然后在 ExpandableListView 上获取引用,如下所示:

View v = inflater.inflate(R.layout.activity_expandable_list, container, false);
createGroupList();
createCollection();
expListView = (ExpandableListView) v.findViewById(R.id.my_exp_list);

我猜你的 Expandable ListView 在你上面膨胀的布局中被引用了,这应该可以工作。

【讨论】:

    猜你喜欢
    • 2014-09-29
    • 2016-07-01
    • 2019-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多