我有一个布局listview
,在片段中我将它绑定在创建的 onActivity 中,但我的问题是我应该在哪个生命周期中实现 `onlistitemClick.
我的代码如下:
public class HeadFragment extends Fragment {
OnHeadlineSelectedListener mCallback;
ListView list;
// The container Activity must implement this interface so the frag can deliver messages
public interface OnHeadlineSelectedListener {
/** Called by HeadlinesFragment when a list item is selected */
public void onArticleSelected(int position);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.listlayout, container, false);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onStart() {
super.onStart();
// When in two-pane layout, set the listview to highlight the selected list item
// (We do this during onStart because at the point the listview is available.)
if (getFragmentManager().findFragmentById(R.id.article_fragment) != null) {
//getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
}
@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 = (OnHeadlineSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
// We need to use a different list item layout for devices older than Honeycomb
int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
android.R.layout.simple_list_item_activated_1 : android.R.layout.simple_list_item_1;
list=(ListView)getActivity().findViewById(R.id.list);
list.setAdapter(new ArrayAdapter<String>(getActivity(), layout, Ipsum.Headlines));
}
// @Override
// public void onListItemClick(ListView l, View v, int position, long id) {
// // Notify the parent activity of selected item
// mCallback.onArticleSelected(position);
//
// // Set the item as checked to be highlighted when in two-pane layout
// getListView().setItemChecked(position, true);
// }
}
我扩展Fragment
不listFragment
。现在我应该在哪个生命周期中编写onListItemClick()
方法以及如何编写它?
任何帮助我的人都非常感谢。提前致谢。