Android Fragment是什么

Fragment是Activity中用户界面的一个行为或者一个部分。你可以在一个单独的Activity上把多个Fragment组合成一个多区域的UI,并且可以在多个Activity中使用。你可以认为Fragment是Activity的一个模块零件,它有自己的生命周期,接收它自己的输入时间,并且可以在Activity运行时添加或者删除。

Fragment的生命周期直接受其宿主Activity的生命周期的影响。例如,一旦Activity被暂停,它里面所有的Fragment也被暂停,一旦Activity被销毁,它里面所有的Fragment也被销毁。

Fragment的使用

第一种方式,主布局中实现,<fragment>标签里面的name属性指向要显示的Fragment类

<LinearLayout 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"
    tools:context=".MainActivity" >

    <fragment 
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:name="com.example.fragmentdemo.TitleFragment"
        android:id="@+id/titlefragment"
        />
    
    <fragment 
        android:id="@+id/contentfragment"
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:name="com.example.fragmentdemo.ContentFragment"
        />

</LinearLayout>

新建两个Fragment类 TitleFragment和ContentFragment

public class TitleFragment extends Fragment implements OnClickListener{
    
    private MyMenuListener myMenuListener;
    
  //重写onAttach方法,把TitleFragment依附的宿主mainActivity实现的接口传回给自己 @Override
public void onAttach(Activity activity) { // TODO Auto-generated method stub super.onAttach(activity); myMenuListener=(MyMenuListener) activity; }

  //重写onCreateView 绑定布局给fragment,并且定义fragment当中按钮事件 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.titlelayout,container,false); Button btn=(Button) view.findViewById(R.id.button1_menu); btn.setOnClickListener(this); return view; }   //声明接口,为了Fragment之间通过它们的宿主mainActivity实现交互 public static interface MyMenuListener{ public void changeValue(String value); } @Override public void onClick(View arg0) { // TODO Auto-generated method stub switch (arg0.getId()) { case R.id.button1_menu: myMenuListener.changeValue("One"); break; default: break; } } }
public class ContentFragment extends Fragment {
    
    private TextView tv;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.contentlayout,container,false);
        tv=(TextView) view.findViewById(R.id.textView1_content);
        
        return view;
    }
    //定义该方法,为了在mainActivity中调用
    public void changeTextView(String value){
        tv.setText(value);
    }

}

主代码mainActivity中实现Fragment之间交互显示

public class MainActivity extends Activity implements TitleFragment.MyMenuListener{

    TitleFragment titleFragment;
    ContentFragment contentFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        titleFragment=(TitleFragment) getFragmentManager().findFragmentById(R.id.titlefragment);
        contentFragment=(ContentFragment) getFragmentManager().findFragmentById(R.id.contentfragment);
    }
    @Override
    public void changeValue(String value) {
        // TODO Auto-generated method stub
        contentFragment.changeTextView(value);
    }


}

第二种方式,代码中添加Fragment,布局文件如下,这里用FrameLaout布局

<LinearLayout 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"
    tools:context=".MainActivity" >

    <fragment 
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:name="com.example.fragmentdemo.TitleFragment"
        android:id="@+id/titlefragment"
        />

    <FrameLayout 
        android:id="@+id/content_layout"
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="3"
        ></FrameLayout>
</LinearLayout>

代码中进行动态加载

public class MainActivity2 extends Activity{

    ContentFragment contentFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        addContentLayout();
    }
    
    private void addContentLayout()
    {
        FragmentManager fm=getFragmentManager();
        //开启一个事务
        FragmentTransaction ft=fm.beginTransaction();
        contentFragment=new ContentFragment();
        ft.add(R.id.content_layout,contentFragment);
        ft.commit();
    }
}

 

posted @ 2016-02-01 17:03  hyyweb  阅读(565)  评论(0编辑  收藏  举报