Android 如何获取在ExpandableListView中单击的对象?
我想知道,如何获取在Android 如何获取在ExpandableListView中单击的对象?,android,Android,我想知道,如何获取在ExpandableListView 我有一个BaseExpandableListAdapter,下面是我的方法: @Override public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { final Foo children = (Foo) getCh
ExpandableListView
我有一个BaseExpandableListAdapter
,下面是我的方法:
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final Foo children = (Foo) getChild(groupPosition, childPosition);
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, null);
}
// view stuff
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// how to get the clicked child object of type Foo?
}
});
convertView.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// also how to get it here?
return true;
}
});
return convertView;
}
如何为单击和长单击侦听器获取类型为
Foo
的子元素?在代码中使用onChildClick
方法。单击子项时会自动调用此方法。使用链接进行长按
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
return true;
}
你为什么不使用McClickListener?在onClick中,你可以得到你最后的Foo-childrenc,但你找不到这个方法。我只能通过ConvertViewNon选择onClick和LongClick。您不必在listView上应用此方法:listView。setOnItemClickListener(…)查看此教程中的listView,它说明了一切:问题是,当我单击子对象时,setOnItemLongClickListener和setOnItemClickListener不被调用,那么长时间的单击呢?我只使用了onChildClick()事件。不知道长时间单击。如本链接所述,Expandable listview只提供onChildClick()事件。