2014-10-01 164 views

回答

0

看看Android開發人員培訓網站。它有你問什麼的實現,是一個更好的答案:

http://developer.android.com/guide/components/fragments.html

public static class FragmentA extends ListFragment { 

    OnArticleSelectedListener mListener; 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
     try { 
      mListener = (OnArticleSelectedListener) activity; 
     } catch (ClassCastException e) { 
      throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener"); 
     } 
    } 


    @Override 
    public void onListItemClick(ListView l, View v, int position, long id) { 
     // Append the clicked item's row ID with the content provider Uri 
     Uri noteUri = ContentUris.withAppendedId(ArticleColumns.CONTENT_URI, id); 
     // Send the event and Uri to the host activity 
     mListener.onArticleSelected(noteUri); 
    } 

} 
相關問題