2013-07-10 39 views
0

我有一個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); 
// } 
} 

我伸出FragmentlistFragment。現在在哪個生命週期中我應該寫onListItemClick()方法以及如何編寫它?

任何人都可以幫助我是不勝感激。提前致謝。

回答

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

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(); 
    } 
}); 
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); 
    } 

} 
相關問題