如何让listview中的子项打开一个片段

时间:2015-01-18 17:11:32

标签: android fragment expandablelistview children clickable

当我点击列表视图中的某个孩子时,如何导航到我的其他片段之一。以下是我的课。 / **  *拼凑而成:  * http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/  * https://www.youtube.com/watch?v=TGXenxtDEGQ  * /

公共类TodoFragment扩展了Fragment {

public TodoFragment(){}

ExpandableListAdapter listAdapter;
ExpandableListView elv;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    return inflater.inflate(R.layout.fragment_todo,container, false);
}
@Override
public void onStart() {
    super.onStart();

    // get the listview
    elv = (ExpandableListView) getView().findViewById(R.id.lvExp);

    // preparing list data
    prepareListData();

    listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild);


    // setting list adapter
    elv.setAdapter(listAdapter);

    // Listview Group click listener
    elv.setOnGroupClickListener(new OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                int groupPosition, long id) {
            // Toast.makeText(getApplicationContext(),
            // "Group Clicked " + listDataHeader.get(groupPosition),
            // Toast.LENGTH_LONG).show();
            return false;
        }
    });



    // Listview onchildclicklistener
    elv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {     
        Intent k;
        @Override            
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {

            switch(childPosition){

            case 0:
                k = new Intent (getActivity().getApplicationContext(),GrottenFragment.class);
                break;

            case 1:
                k = new Intent (getActivity().getApplicationContext(),.class);
                break;

            case 2:
                k = new Intent (getActivity().getApplicationContext(),OstrichFragment.class);
                break;

            case 3:

                k = new Intent (getActivity().getApplicationContext(),NationalparkFragment.class);
                break;

            case 4:
                k = new Intent (getActivity().getApplicationContext(),LandhuisknipFragment.class);
                    break;

            case 5:
                k = new Intent (getActivity().getApplicationContext(),ChristoffelparkFragment.class);
                    break;

            case 6:
                k = new Intent (getActivity().getApplicationContext(),NavyFragment.class);
                        break;

             case 7:
                k = new Intent (getActivity().getApplicationContext(),PostFragment.class);
                        break;
            }
            startActivity(k);
            return true;
        }
    });

}

/*
 * Preparing the list data
 */
private void prepareListData() {
    listDataHeader = new ArrayList<String>();
    listDataChild = new HashMap<String, List<String>>();

    // Adding group data
    listDataHeader.add("Culture");
    listDataHeader.add("Beaches");
    listDataHeader.add("Car Rental");
    listDataHeader.add("Dinner");

    // Adding child data
    List<String> culture= new ArrayList<String>();  
    culture.add("Grotten van Hato");
    culture.add("Ostrich Farm");
    culture.add("Shete Boka national Park");
    culture.add("Landhuis Knip");
    culture.add("Christoffelpark");
    culture.add("Navy Museum");
    culture.add("Post Museum");

    List<String> beaches = new ArrayList<String>();
    beaches.add("Mambo Beach");
    beaches.add("Knip");
    beaches.add("Playa Kalki");
    beaches.add("Westpunt");
    beaches.add("Boca Santa Cruz");
    beaches.add("Cas Abao");
    beaches.add("Playa PortoMari");
    beaches.add("Kontiki Beach");
    beaches.add("Jan Thiel Beach");

    List<String> car = new ArrayList<String>();
    car.add("budget rental");
    car.add("Avis Rental");
    car.add("Alamo Car Rental");
    car.add("Noordstar Rental");
    car.add("Europa Rental");

    List<String> dinner = new ArrayList<String>();
    dinner.add("Truk di Pan");
    dinner.add("Burger King");
    dinner.add("Punda Food");

 // Header, Child data
    listDataChild.put(listDataHeader.get(0), culture); 
    listDataChild.put(listDataHeader.get(1), beaches);
    listDataChild.put(listDataHeader.get(2), car);
    listDataChild.put(listDataHeader.get(3), dinner);

}

}

1 个答案:

答案 0 :(得分:0)

您无法导航到片段。您可以导航到包含您的片段的新活动,也可以更新当前活动以使用FragmentManager显示您的片段。