如何使列表视图中的子项打开片段



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

公共类 TodoFragment 扩展片段 {

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);
}

}

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

相关内容

  • 没有找到相关文章

最新更新