expandablelistview子项单击

时间:2015-01-16 23:56:26

标签: android onclick fragment expandablelistview

我有几个孩子的扩展名单。如果我点击一个孩子,我希望它打开自己的活动。现在我让它去工作只去1活动。因此,当我点击Mambo海滩时,我希望它打开一个有Mambo海滩信息的活动。当我点击Avis汽车租赁时,它应该打开该活动

在我的例子之下:

公共类TodoFragment扩展了Fragment {

public TodoFragment(){}

ExpandableListAdapter listAdapter;
ExpandableListView expListView;
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
    expListView = (ExpandableListView) getView().findViewById(R.id.lvExp);

    // preparing list data
    prepareListData();

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


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

    // Listview Group click listener
    expListView.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 on child click listener
    expListView.setOnChildClickListener(new OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
            //You have to create next activity say NextActivity.java
            Intent intent = new Intent(getActivity(), MainActivity.class); 
            startActivity(intent);
            return false;
        }
    });

}

/*
 * 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);

}

}

2 个答案:

答案 0 :(得分:0)

您可以通过简单地根据Intent的值创建和启动不同的childPosition来实现此目的。例如:

switch (childPosition)
{
    case 0:
        Intent intentChild0 = new Intent(yourMainActivity.this, ActivityForChild0.class); 
        startActivity(intentChild0);
        break;

    case 1:
        Intent intentChild1 = new Intent(yourMainActivity.this, ActivityForChild1.class); 
        startActivity(intentChild1);
        break;

    //Etc...

    default:
        break;
}

将此内容添加到OnChildClickListener将导致启用不同的Activity,具体取决于点击的孩子。

答案 1 :(得分:0)

有两个解决方案:一个是切换子位置并重定向活动。但是这个解决方案有问题,因为你将有很多活动而且你的项目不会被订购。

另一种解决方案是实施一项活动,许多片段取决于你有多少孩子。在您的活动中,您只需重定向片段,具体取决于子ID。