0

以下代码当前正在 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);
4

1 回答 1

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 在您上面膨胀的布局中被引用,这应该可以工作。

于 2013-09-16T08:20:04.883 回答