android 文档阅读记录-添加fragment

来源:互联网 发布:营销活动数据分析报告 编辑:程序博客网 时间:2024/04/30 23:19

1.在xml文件里面直接添加Fragment

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal"    android:layout_width="fill_parent"    android:layout_height="fill_parent">    <fragment android:name="com.example.android.fragments.HeadlinesFragment"              android:id="@+id/headlines_fragment"              android:layout_weight="1"              android:layout_width="0dp"              android:layout_height="match_parent" />    <fragment android:name="com.example.android.fragments.ArticleFragment"              android:id="@+id/article_fragment"              android:layout_weight="2"              android:layout_width="0dp"              android:layout_height="match_parent" /></LinearLayout>
当你在xml文件里面直接添加Fragment的时候,在程序运行时的时候是不能移除的,如果你打算在运行的时候根据用户的互动来切换,那么你只能通过activity 在onceat的时候添加和管理。

2.如何创建灵活的Fragment

首先需要在XML里面加上 

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/fragment_container"    android:layout_width="match_parent"    android:layout_height="match_parent" />

HeadlinesFragment firstFragment = new HeadlinesFragment();//创建Fragment实例  
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, firstFragment).commit();
//通过FragmentTransaction 将fragment加入activity            
3.Fragment 通信 通过接口的方式实现 Fragment 和其他 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);               //确保 要和此fragment通信的容器 实现了OnHeadlineSelectedListener 接口 否则抛出 没有实现接口的异常        try {            mCallback = (OnHeadlineSelectedListener) activity;        } catch (ClassCastException e) {            throw new ClassCastException(activity.toString()                    + " must implement OnHeadlineSelectedListener");        }    }        ...}
这样 Fragment就可以通过调用onArticleSelected()方法将信息传到Activity里面去

简单实例:

    @Override    public void onListItemClick(ListView l, View v, int position, long id) {        // Send the event to the host activity        mCallback.onArticleSelected(position);    }
当Fragment 里面的listview 点击时,就会将这个position 传到activity里面去,当然也可以传其他的参数,执行相应的操作。


从Activity传递消息到Fragment里面

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



0 0
原创粉丝点击