Fragment(3)和其他Fragment之間互動--偉大的介面回撥
阿新 • • 發佈:2019-02-01
這個是我一直想要找的,沒想到偉大的官網竟然有。感動到流鼻涕了。。
為了重用Fragment UI元件,我們應該建立一個完全獨立的自控的,定義自己佈局和行為的模型元件。一旦我們定義了這些可重用的Fragments,我們就可以把它們和Activity關聯起來並且用應用邏輯連線它們來實現一個整體的混合的UI。
所有的Fragment和Fragment的互動,都是通過它們共同關聯的Activity。就是說兩個Fragment永遠都不可能直接互動的。
定義一個介面
為了允許一個Fragment和它上級的Activity互動,我們可以在Fragment類中來定義一個Interface並且那個Activity要實現這個介面。Fragment在自己的onAttach()生命週期方法中獲得這個interface的實現類物件,並且為了和Activity互動可以接著呼叫介面的方法。
下面是官網的例子:
本Fragment在適當的地方呼叫介面的方法,在Activity中實現介面和對應的方法,那個方法就會被執行public class HeadlinesFragment extends 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"); } } ... }
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Send the event to the host activity
mCallback.onArticleSelected(position);
}
實現介面
為了接收Fragment事件回撥,宿主的Activity必須實現那個介面。
例子:
傳遞訊息給Fragmentpublic static class MainActivity extends Activity implements 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 } }
宿主的Activity可以傳遞訊息給Fragment通過使用findFragmentById()捕獲Fragment例項,接著呼叫Fragment的public方法。
public static class MainActivity extends Activity
implements 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
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呼叫public方法更新佈局介面
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();
}
}
}