自定义ListView的片段在哪里写OnItemClick

时间:2013-07-10 12:43:44

标签: android listview fragment

我有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。现在我应该在哪个Lifecycle中编写onListItemClick()方法以及如何编写它?

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

3 个答案:

答案 0 :(得分:1)

如果您没有使用ListFragment,那么设置一个与基础ArrayAdapter

相关联的点击监听器
listView.setOnItemClickListener(new OnItemClickListener() {

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

    });

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

答案 1 :(得分: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();
    }
});

答案 2 :(得分: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);
    }

}