zoukankan      html  css  js  c++  java
  • 跟Google学习Android开发-起始篇-用碎片构建一个动态的用户界面(4)

    4.4与其它碎片通信


    为了重用碎片UI组件,你应该把它们每个建立成一个完全独立的、模块化的组件,让它定义自己的布局和行为。一旦你定义了这些可复用的碎片,您可以将它们与活动关联起来,并将它们连接到应用程序逻辑来实现整体复合UI

    通常情况下,你会想让一个碎片与另一个通信,例如根据用户事件改动内容。所有碎片到碎片的通信是通过相关联的活动进行的。两个碎片永远不应该直接通信。


    定义一个接口


    为了让碎片与其活动通信,你可以在Fragment类中定义一个接口,并在活动内实现它。碎片在其onAttach()生命周期方法捕获接口实现,然后可以调用接口中的方法来与活动通信。

    下面是一个活动到碎片通信的例子:


    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");
            }
        }
        
        ...
    }
    
    

    现在碎片可以通过调用onArticleSelected()方法(或其它接口中的方法)给活动分发消息,使用OnHeadlineSelectedListener接口的mCallback实例。

    例如,当用户点击一个列表项时碎片的下述方法会被调用。碎片使用回调接口来分发事件到父活动。


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

    实现的接口


    为了从碎片接收事件回调,装载碎片的活动必须实现碎片类中定义的接口。

    例如,以下活动实现了上面例子的接口。


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


    分发信息到碎片


    载体活动可以通过使用findFragmentById()捕捉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();
            }
        }
    }
  • 相关阅读:
    Java RunTime Environment (JRE) or Java Development Kit (JDK) must be available in order to run Eclipse. ......
    UVA 1597 Searching the Web
    UVA 1596 Bug Hunt
    UVA 230 Borrowers
    UVA 221 Urban Elevations
    UVA 814 The Letter Carrier's Rounds
    UVA 207 PGA Tour Prize Money
    UVA 1592 Database
    UVA 540 Team Queue
    UVA 12096 The SetStack Computer
  • 原文地址:https://www.cnblogs.com/snake-hand/p/3174399.html
Copyright © 2011-2022 走看看