将ExpandableListView放入片段 - Android

时间:2014-11-08 12:49:52

标签: android android-fragments expandablelistview

我正在制作我的第一个Android应用程序,这是我大学的一个项目。 现在,我按照this guide创建了一个可扩展列表,但现在我希望将它包含在自己的片段中,以便它看起来更多或更少。

enter image description here
任何人都可以帮助我吗?我对Android tbh缺乏经验,所以我感谢任何帮助!

指南中的代码段:

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();

/* ******************** Start Group  ********************* */
Map<String, String> curgroupMap1 = new HashMap<String, String>();
groupData.add(curgroupMap1);
curgroupMap1.put("parent", "Master 1");

List<Map<String,String>> children1 =new ArrayList<Map<String,String>>();
/* *** ChildData ***/
Map<String, String> curChildMap1 = new HashMap<String, String>();
children1.add(curChildMap1);
curChildMap1.put("child", "Bank");

/* *** ChildData ***/
Map<String, String> curChildMap2 = new HashMap<String, String>();
children1.add(curChildMap2);
curChildMap2.put("child", "Executive");

/* *** ChildData ***/
Map<String, String> curChildMap3 = new HashMap<String, String>();
children1.add(curChildMap3);
curChildMap3.put("child", "Customer");

/* ***ChildData ***/
Map<String, String> curChildMap4 = new HashMap<String, String>();
children1.add(curChildMap4);
curChildMap4.put("child", "State");

/* ***ChildData ***/
Map<String, String> curChildMap5 = new HashMap<String, String>();
children1.add(curChildMap5);
curChildMap5.put("child", "City");

childData.add(children1);

/* *************************End Group **************************/

/* ******************** Start Group  ********************* */
Map<String, String> curgroupMap2 = new HashMap<String, String>();
groupData.add(curgroupMap2);
curgroupMap2.put("parent", "Master 2");
List<Map<String,String>> children2 =new ArrayList<Map<String,String>>();

/* *** ChildData ***/
Map<String, String> curChildMap6 = new HashMap<String, String>();
children2.add(curChildMap6);
curChildMap6.put("child", "Android");

/* *** ChildData ***/
Map<String, String> curChildMap7 = new HashMap<String, String>();
children2.add(curChildMap7);
curChildMap7.put("child", "iPhone");

/* *** ChildData ***/
Map<String, String> curChildMap8 = new HashMap<String, String>();
children2.add(curChildMap8);
curChildMap8.put("child", "Windows");

childData.add(children2);

/* *************************End Group **************************/


 mAdapter = new SimpleExpandableListAdapter(
            this,
            groupData,
            android.R.layout.simple_expandable_list_item_1,
            new String[] { "parent" },
            new int[] { android.R.id.text1, android.R.id.text2 },
            childData,
            android.R.layout.simple_expandable_list_item_2,
            new String[] {"child"},
            new int[] { android.R.id.text1 }
            );
 setListAdapter(mAdapter);

 expand = getExpandableListView();

 expand.setOnChildClickListener(new OnChildClickListener() {

    @Override
    public boolean onChildClick(ExpandableListView parent, View v,
            int groupPosition, int childPosition, long id) {
        // TODO Auto-generated method stub

        switch (groupPosition)
        {
        case 0 :
            switch(childPosition)
            {
            case 0 :
                Toast.makeText(getBaseContext(), "Bank",
                    Toast.LENGTH_SHORT).show();
                break;
            case 1:
                Toast.makeText(getBaseContext(), "Executive",
                    Toast.LENGTH_SHORT).show();
                break;
            case 2:
                Toast.makeText(getBaseContext(), "Customer",
                    Toast.LENGTH_SHORT).show();
                break;
            case 3 :
                Toast.makeText(getBaseContext(), "State",
                    Toast.LENGTH_SHORT).show();
                break;
            case 4 :
                Toast.makeText(getBaseContext(), "City",
                    Toast.LENGTH_SHORT).show();
                break;
            }
            break;
        case 1:
            switch(childPosition)
            {
            case 0 :
                Toast.makeText(getBaseContext(), "Android",
                    Toast.LENGTH_SHORT).show();
                break;
            case 1:
                Toast.makeText(getBaseContext(), "iPhone",
                    Toast.LENGTH_SHORT).show();
                break;
            case 2:
                Toast.makeText(getBaseContext(), "Windows",
                    Toast.LENGTH_SHORT).show(); 
                break;
            }
            break;
        }

        return false;
    }
});

1 个答案:

答案 0 :(得分:0)

好吧,如果你已经在活动中完成了这项工作,那么你不必再做任何工作了,加载片段并做同样的事情。

  1. 在片段中的onCreateView()中实例化您的视图。您不应该假设您的视图实际可用于进行任何更改。 UI更改应在OnActivityCreated()中完成。
  2. 将可扩展列表视图加载到该片段的onActivityCreated()中。
  3. 代码将与您指向此类视图的代码完全相同

    例如:Listview listview =(Listview)getActivity()。findViewbyID(R.id.listview);在你的onCreateview()