(一)开始____5、用Fragment构建一个动态UI——与其他片段交互

原文链接:http://developer.android.com/intl/zh-CN/training/basics/fragments/communicating.html#Deliver

目录

 [隐藏

与其他Fragment交互 Communication with Other Fragments

为了重用Fragment UI组件,你应该将Fragment建立成完全独立,模块化并且定义了自己布局和行为的组件。一旦你定义了这些可重用的Fragment, 你可以通过activity,应用程序逻辑使它们关联,交互以组成一个整体复合型UI。 通常情况下,你希望一个Fragment可以与另一个交互。比如在用户事件的基础上去修改内容,所有Fragment到Fragment的交互都是通过相关联的activity来做的,两个fragment应该从不直接交互;

定义一个接口 Define an Interface

为了允许Fragment与它的activity交互,你可以在fragment类中定义一个接口并且在activity中实现它。fragment可以在onAttach()方法中获取接口的实现并调用接口的方法与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");
        }
    }
 
    ...
}

现在fragment可以使用OnHeadlineSelectedListener的实例mCallback调用onArticleSelected()方法(或者其他接口内的方法)提供信息给activity了。 例如,当 用户点击list item(list子项)时就会调用下面在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);
}

实现接口 Implement the Interface

为了接收来自fragment的事件回调,主activity(你需要用来与fragment交互的activity)必须实现定义在fragment类中的接口。 例如:下面这个activity就实现了上一例子中的接口:

public static class MainActivity extends Activity
        implements HeadlinesFragment.OnHeadlineSelectedListener{
    ...
 
    public void onArticleSelected(Uri articleUri) {
        // The user selected the headline of an article from the HeadlinesFragment
        // Do something here to display that article
    }
}

Deliver a Message to a Fragment

主activity可以使用findFragmentById()方法获取Fragment实例,然后直接调用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();
        }
    }
}

 

posted @ 2014-07-29 09:11  ╰→劉じ尛鶴  阅读(97)  评论(0)    收藏  举报