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();
            }
        }
    }
  • 相关阅读:
    android开发中提示:requires permission android.permission write_settings解决方法
    TRX(腾讯通)与OA集成
    windows下Tomcat指定jdk并部署到系统服务设置开机启动
    如何配置Tomcat服务器环境
    IntelliJ IDEA 工具使用
    AOP切面操作
    Spring Boot中使用AOP面向切面
    sun.misc jar包
    Excel导出数据库数据
    FreeMarker js 获取后台设置的request、session
  • 原文地址:https://www.cnblogs.com/snake-hand/p/3174399.html
Copyright © 2011-2022 走看看