我有一個listview
佈局,並在片段我將它綁定在onActivity創建,但我的問題是我應該實現`onlistitemClick哪個生命週期。片段自定義ListView在哪裏寫OnItemClick
我的代碼如下:
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()
方法以及如何編寫它?
任何人都可以幫助我是不勝感激。提前致謝。