Fragment

 一。创建Fragment

1.从布局文件中创建视图

public static class ExampleFragment extends Fragment {  

    @Override  
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  
        // Inflate the layout for this fragment  
        return inflater.inflate(R.layout.example_fragment, container, false);  
    }  
}  

 2. 动态替换某个布局文件中的ViewGroup

public class ExampleFragment extends Fragment
{
    // Fragment对应的标签,当Fragment依附于Activity时得到
    private String tag;

    @Override
    public void onAttach(Activity activity)
    {
        super.onAttach(activity);
        tag = getTag();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        TextView textView = new TextView(getActivity());

        textView.setText(tag);
        return textView;
    }
}
        ExampleFragment frag = new ExampleFragment();
            FragmentManager manager = getFragmentManager();
            FragmentTransaction transaction = manager.beginTransaction();
            // 将Activity中的内容替换成对应选择的Fragment
            transaction.replace(R.id.container, frag, listNames[itemPosition]);
            transaction.commit();
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
  >

     <LinearLayout 
         android:id="@+id/container"
         android:orientation="horizontal"
          android:layout_width="match_parent"
         android:layout_height="match_parent">
     </LinearLayout>

</RelativeLayout>
 

二。为Activity创建事件回调方法

      在一些情况下, 你可能需要一个fragment与activity分享事件. 一个好的方法是在fragment中定义一个回调的interface, 并要求宿主activity实现它.

当activity通过interface接收到一个回调, 必要时它可以和在layout中的其他fragment分享信息.

    例如, 如果一个新的应用在activity中有2个fragment, A用来显示文章列表, B显示文章内容 ,  A必须告诉activity何时一个list item被选中,然后它可以告

诉fragmentB去显示文章。

    public interface OnArticleSelectedListener {  

        public void onArticleSelected(Uri articleUri);  
    }  

           然后fragment的宿主activity实现 OnArticleSelectedListener 接口。为了确保宿主activity实现这个接口,  A的 onAttach() 回调方法将

传入的Activity参数转换OnArticleSelectedListener实例.

public static class FragmentA extends ListFragment {  

    OnArticleSelectedListener mListener;  
    @Override  
    public void onAttach(Activity activity) {  
        super.onAttach(activity);  
        try {  
            mListener = (OnArticleSelectedListener) activity;  
         } catch (ClassCastException e) {  
           throw new ClassCastException(activity.toString() + " must implementOnArticleSelectedListener");  
        }  
    }   
}  

          你的fragment可以通过实现 onCreateOptionMenu() 提供菜单项给activity的选项菜单(以此类推, Action Bar也一样).为了使这个方法接收调用,无论如何,

你必须在 onCreate() 期间调用 setHasOptionsMenu() 来指出fragment要添加item到选项菜单(否则, 不会调用fragment的onCreateOptionsMenu()).


    注意: 尽管你的fragment会接收到它所添加的每一个菜单项被选择后的回调,  但实际上当用户选择一个菜单项时, activity会首先接收到对应的回调.如果activity的

on-item-selected回调函数实现并没有处理被选中的项目, 然后事件才会被传递到fragment的回调.

      这个规则适用于选项菜单和上下文菜单.


    

原文地址:https://www.cnblogs.com/yuyutianxia/p/3494629.html