`
252190908
  • 浏览: 228249 次
文章分类
社区版块
存档分类
最新评论

Android Developers:和其它Fragment通信

 
阅读更多

为了重用Fragment UI组件,你应该作为一个定义了它自己的布局和行为的,完全独立的,模块化的组建来构建。一旦你定义了这些可重用的Fragment,你使用一个Activity关联它们,和结合应用程序的逻辑以实现整体复合界面。

经常你会想让一个fragment和另一个通信,例如基于用户事件改变内容。所有Fragment和Fragment的通信是通过相关的Activity完成。两个Fragment不能直接通信。

定义一个接口

—————————————————————————————————————————————————————————————

为了允许fragment和它的activity通信,你可以在这个Fragment类中定义一个接口,并在这个Activity中实现它。这个fragment在它的onAttach()生命周期方法中获取接口的实现,然后调用这个接口方法和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"); 
        } 
    } 
     
    ... 
} 

现在fragment能使用onHeadlineSelectedListener接口的mCallback实例,调用onArticleSelected()方法(或者在接口中的其它方法)发送消息给activity。

例如,下面在fragment中的方法当用户点击列表项的时候被调用。fragment使用回调接口来想父activity发送这个事件。

@Override 
    public void onListItemClick(ListView l, View v, int position, long id) { 
        // Send the event to the host activity 
        mCallback.onArticleSelected(position); 
    } 

实现接口

————————————————————————————————————————————————

为了从fragment获取事件回调方法,寄存它的activity必须实现被定义在fragment类中的接口。

例如,下面的activity实现了上面例子的接口:

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 
    } 
} 

向Fragment发送消息

—————————————————————————————————————————

寄主activity能向使用findFragmentById()方法捕获的Fragment实例发送消息,然后直接调用fragment的公共方法。

例如,想象一下,上面显示的activity可能包含另一个fragment,它被用于显示选项详情,通过上面的回调方法被返回的数据。在这种情况下,activity能想另一个显示选项的fragment,传递在回调方法中获取的信息。

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics