我正在使用import android.support.v4.app.ListFragment
创建一个ListFragment。在片段本身(而不是父活动)中,我想实现onItemClick
方法。有人会提供一个简单的例子来说明它是如何工作的吗?
public class MyListFragment extends ListFragment {
Spannable[] stuff = {};//to be filled
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ArrayAdapter<Spannable> adapter = new ArrayAdapter<Spannable>(inflater.getContext(), android.R.layout.simple_list_item_1, stuff);
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//this is red underlined by eclipse
}
}
在问这个问题之前,你做过一些研究吗?无论如何,可以通过覆盖onListItemClick
方法来完成,如下所示:
public class Test extends ListFragment {
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
//do the stuff
}
}