我想在点击可展开列表视图的子项时打开一个新活动。我尝试使用代码,但它既没有工作也没有任何错误。
Exp_list.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Intent mum;
if(groupPosition==0){
mum = new Intent(getApplicationContext(), Example.class);
startActivity(mum);
}
return true;
}
});
以下是适配器类的代码
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> Travels_Header;
private HashMap<String, List<String>> Details_Child;
public ExpandableListAdapter(Context context, List<String> Travels_Header, HashMap<String, List<String>> Details_Child) {
this._context = context;
this.Travels_Header = Travels_Header;
this.Details_Child = Details_Child;
}
@Override
public Object getChild(int parent, int child) {
// TODO Auto-generated method stub
return this.Details_Child.get(this.Travels_Header.get(parent)).get(child);
}
@Override
public long getChildId(int parent, int child) {
// TODO Auto-generated method stub
return child;
}
@Override
public View getChildView(int parent, int child, boolean lastChild, View convertView,
ViewGroup parentView) {
// TODO Auto-generated method stub
final String childText = (String) getChild(parent, child);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_view_child, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.textView1);
txtListChild.setText(childText);
return convertView;
}
@Override
public int getChildrenCount(int parent) {
// TODO Auto-generated method stub
return this.Details_Child.get(this.Travels_Header.get(parent)).size();
}
@Override
public Object getGroup(int parent) {
// TODO Auto-generated method stub
return this.Travels_Header.get(parent);
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return this.Travels_Header.size();
}
@Override
public long getGroupId(int parent) {
// TODO Auto-generated method stub
return parent;
}
@Override
public View getGroupView(int parent, boolean isExpanded, View convertView, ViewGroup parentView) {
// TODO Auto-generated method stub
String headerTitle = (String) getGroup(parent);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_view_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.textView1);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isChildSelectable(int parent, int child) {
// TODO Auto-generated method stub
return true;
}
}
尝试了各种方法来执行此代码。
答案 0 :(得分:0)
尝试此代码我只对您的代码进行了更改。
@Override
public View getChildView(int parent, int child, boolean lastChild, View convertView,
ViewGroup parentView) {
// TODO Auto-generated method stub
final String childText = (String) getChild(parent, child);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_view_child, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.textView1);
txtListChild.setText(childText);
//convertview listener
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
//parent position
if(parent==0){
//child positon
if(child==0){
Intent i = new Intent(context, activity.class);
in.addFlags(Intent.FLAF_ACTIVITY_NEW_TASK);
startActivity(i);
}
}
}
});
return convertView;
}
答案 1 :(得分:0)
Exp_list.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
switch (groupPosition) { // the First you must know the group position
case 0: // Here your Group position the first Group
switch (childPosition) {
case 7: // Here your Child position
startActivity(new Intent(getApplicationContext(), Example.class));
break; // this break for Child
}
break; // this break for Group
case 1: // again here your Group position the Second Group
switch (childPosition) {
case 9: // again here your Child position
startActivity(new Intent(getApplicationContext(), Example.class));
break;
}
break;
case 2:
switch (childPosition) {
case 9:
startActivity(new Intent(getApplicationContext(), Example.class));
break;
}
break;
}
return false;
}
});