gpt4 book ai didi

android - 如何在可扩展 ListView 中使 child 可点击

转载 作者:行者123 更新时间:2023-11-30 02:42:10 24 4
gpt4 key购买 nike

我在 fragment 中有一个 expandableListView 如果我点击一个 child ,我希望它打开一个包含一些内容的 Activity/fragment 。下面是我的 ListView

公共(public)类 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) {
// TODO Auto-generated method stub
Toast.makeText(
getActivity(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_LONG)
.show();
return false;
}
});
}

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

// Adding child 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");


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

}

谁能帮帮我。

最佳答案

你可以使用下面的代码

//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(), NextActivity.class);
startActivity(intent);
return false;
}
});

关于android - 如何在可扩展 ListView 中使 child 可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25608327/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com