링크 : Communicating with Other Fragments
이 조각이 활동까지 통신 할 수 있도록하려면, 당신은 조각 클래스의 인터페이스를 정의하고 활동 내에서 구현할 수 있습니다. Fragment는 onAttach() 라이프 사이클 메소드에서 인터페이스 구현을 캡처 한 다음 Activity와 통신하기 위해 Interface 메소드를 호출 할 수 있습니다. 여기
활동 통신에 조각의 예이다 :
공용 클래스 HeadlinesFragment가 ListFragment { OnHeadlineSelectedListener mCallback 확장;
// Container Activity must implement this interface
public interface OnHeadlineSelectedListener {
public void onArticleSelected(int position);
}
@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");
}
}
...
} 이제 OnHeadlineSelectedListener 인터페이스 mCallback 인스턴스를 사용 onArticleSelected() 메소드 (또는 다른 인터페이스 방식)를 호출하여 활성에 메시지를 전달할 수있는 단편.
예를 들어 사용자가 목록 항목을 클릭하면 다음 조각이 호출됩니다. 프래그먼트는 콜백 인터페이스를 사용하여 상위 액티비티에 이벤트를 전달합니다.
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Send the event to the host activity
mCallback.onArticleSelected(position);
}
는 조각에서이 조각 클래스에 정의 된 인터페이스를 구현해야 호스팅 활동을 이벤트 콜백을 받기 위해서는 인터페이스 를 구현합니다.
예를 들어, 다음 활동은 위 예의 인터페이스를 구현합니다.
공용 정적 클래스 MainActivity 활동 가 HeadlinesFragment.OnHeadlineSelectedListener { ...
public void onArticleSelected(int position) {
// The user selected the headline of an article from the HeadlinesFragment
// Do something here to display that article
}
} 조각 위치 인스턴스를 캡쳐하여 조각 로 프래그먼트으로 메시지를 전달할 수있는 호스트 활동 메시지 전달을 구현 연장 findFragmentById()를 사용하여 직접 프래그먼트의 public 메소드를 호출하십시오.
예를 들어 위의 활동에 위의 콜백 메소드에서 반환 된 데이터로 지정된 항목을 표시하는 데 사용되는 다른 단편이있을 수 있다고 가정 해보십시오.이 경우, 액티비티가 항목을 표시 다른 단편 콜백 방법에서 수신 된 정보를 전달할 수
공용 정적 클래스 MainActivity 연장 활동 는 { ...
public void onArticleSelected(int position) {
// The user selected the headline of an article from the HeadlinesFragment
// Do something here to display that article
ArticleFragment articleFrag = (ArticleFragment)
getSupportFragmentManager().findFragmentById(R.id.article_fragment);
if (articleFrag != null) {
// If article frag is available, we're in two-pane layout...
// Call a method in the ArticleFragment to update its content
articleFrag.updateArticleView(position);
} else {
// Otherwise, we're in the one-pane layout and must swap frags...
// Create fragment and give it an argument for the selected article
ArticleFragment newFragment = new ArticleFragment();
Bundle args = new Bundle();
args.putInt(ArticleFragment.ARG_POSITION, position);
newFragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
}
HeadlinesFragment.OnHeadlineSelectedListener는 구현
}
callback() 개념을 사용했습니다. – Tushar