活动到列表片段

时间:2013-09-16 08:00:57

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

以下代码目前正在使用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);

1 个答案:

答案 0 :(得分: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在你上面夸大的布局中被引用,这应该可行。