0

我有一个布局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);
//    }
}

我扩展FragmentlistFragment。现在我应该在哪个生命周期中编写onListItemClick()方法以及如何编写它?

任何帮助我的人都非常感谢。提前致谢。

4

3 回答 3

1

当你不使用ListFragment你所做的是设置一个与底层相关的点击监听器ArrayAdapter

listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // do something            
         }

    });

listView 就是ListView您在该片段中使用的任何内容。您可以在将适配器设置为与列表关联的同一部分进行设置 - 这可能位于onActivityCreated()or中onCreateView

于 2013-07-10T12:52:02.550 回答
0

将以下代码放入onCreateView()方法中。希望它会帮助你。

ListView list;

//code to get the listView instance using findViewByID etc

list.setOnItemClickListener(new OnItemClickListener()
{
    @Override public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
    { 
        Toast.makeText(MyActivity.this, "Clicked", Toast.LENGTH_SHORT).show();
    }
});
于 2013-07-10T12:52:21.810 回答
0

尝试这个

list.setOnItemClickListener(new AppointmentListener());
class AppointmentListener implements OnItemClickListener {
    public void onItemClick(AdapterView<?> listview, View row, int index,
            long id) {
        Log.e(TAG, "---------------clicked index is: " + index);
    }

}
于 2013-07-10T12:52:43.267 回答