如果您的片段中只有一个 ListView,最好使用ListFragment。OnitemClickListener 更好地集成。您只需要覆盖 OnListItem 侦听器
这里有很好的教程:http:
//developer.android.com/guide/components/fragments.html
这是一个例子:
public class ListMenu extends android.support.v4.app.ListFragment {
private String[] strList = { "test1", "test2", "test3", "test4" };
OnMenuSelectedListener mCallback;
public interface OnMenuSelectedListener {
public void onMenuSelected(int position);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_activated_1, strList));
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception.
try {
mCallback = (OnMenuSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
mCallback.onMenuSelected(position);
getListView().setItemChecked(position, true);
}
}