• Android学习路径(23)应用Fragment建立动态UI——Fragment之间的通信


    为了要重用Fragment的UI组件。你应该为它们每个都构建一个完整独立的,模块化的组件来定义他自身的布局和行为。

    一旦你定义了这些可重用的Fragments。你能够通过activity关联它们同一时候通过应用逻辑连接它们来实现全部复杂的UI。

    你通常都希望一个fragment可以和其它的fragment进行交互,比如基于用户的操作改变内容。全部的fragment之间的交流的完毕都通过activity的关联。两个fragment之间一定不要直接交流。

    定义一个接口


    要同意一个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传递消息了。

    比如。当用户点击fragment中列表的item时,以下的方法将会被调用。这个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的回调事件,包括这个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传递消息。然后直接调用这些fragment的public方法。

    比如,设想上面的那个activity包括还有一个fragment,这个fragment是用来展示通过前面的回调方法返回的数据指定的item。在这个案例中,这个activity能够这个activity能够将接收的这些信息传递给其他的fragment用来显示这个item:

    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学习笔记一:项目目录结构
    ReactNative踩坑日志——OnPress随着render()执行被自动调用?
    ReactNative踩坑日志——使用async/await语法解决网络请求的异步导致的指令执行顺序错乱问题
    ReactNative踩坑日志——代码执行方式(面向对象)
    ReactNative踩坑日志——fetch如何向服务器传递参数
    ReactNative踩坑日志——函数绑定this
    ReactNative踩坑日志——如何实现删除scrollview中的视图
    ReactNative踩坑日志——页面跳转之——Undefined is not an Object(evaluating this2.props.navigation.navigate)
    Git使用笔记
    CSS拾遗
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/4675202.html
Copyright © 2020-2023  润新知