【安卓笔记】Fragment

如何创建Fragment?
Fragment通常创建在activity下,如果我们希望创建一个Fragment,可以在activity对应的布局文件中增加fragment节点(就跟清单文件一样),然后为该节点增加name或者class属性,绑定一个待实例化的Fragment类。具体步骤如下所示:
1.在activity布局下增加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"
    android:orientation="horizontal"
    tools:context=".MainActivity" >
 <fragment
     android:id="@+id/fg1"
     android:name="com.example.fragmentdemo1.Fragment1"
     android:layout_width="0dp"
     android:layout_weight="1"
     android:layout_height="match_parent"
     />
</LinearLayout>
2.创建Fragment类:
package com.example.fragmentdemo1;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class Fragment1 extends Fragment
{
    private TextView tv = null;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.f1,container);
        tv = (TextView) view.findViewById(R.id.tv1);
        return view;
    }
    
    public void setText(String text)
    {
        tv.setText(text);
    }
    
}
3.Fragment类对应的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff0000"
    android:orientation="vertical" >
    
    <TextView 
        android:id="@+id/tv1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="文本1"
        />
</LinearLayout>
经过上面几个步骤,fragment就被创建好了,下面当activity被创建后,fragment就能被显示出来。
创建向下兼容的Fragment
support.v4包中提供了也提供了Fragment类,使用v4包的fragment兼容性会更好。
使用方式:
1.activity继承v4包中的FragmentActivity
2.必须导入v4包中的Fragment
3.如果你要使用FragmentManager,必须使用getSupportFragmentManager()
注意:布局中的fragment节点不用修改,仍然是fragment!
Fragment间的通讯
因为每个fragment都会关联一个activity,所以只要获取这个activity,就能从activity中获取其他fragment,像这样
getActivity().getSupportFragmentManager().findFragmentById(R.id.fg1);
案例:
通过点击一个fragment上的按钮设置另一个fragment的文本:
fragment1:
package com.example.fragmentdemo1;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class Fragment1 extends Fragment
{
    private TextView tv = null;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.f1,container);
        tv = (TextView) view.findViewById(R.id.tv1);
        return view;
    }
    
    public void setText(String text)
    {
        tv.setText(text);
    }
    
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff0000"
    android:orientation="vertical" >
    
    <TextView 
        android:id="@+id/tv1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="文本1"
        />
</LinearLayout>
fragment2:
package com.example.fragmentdemo1;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class Fragment2 extends Fragment
{
    private Button but = null;
    private TextView tv = null;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.f2, container);
        but = (Button) view.findViewById(R.id.but);
        tv = (TextView) view.findViewById(R.id.tv2);
        return view;
    }
    
    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);
        but.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Fragment1 f1 = (Fragment1) Fragment2.this.getActivity().getSupportFragmentManager().findFragmentById(R.id.fg1);
                f1.setText("哈哈");
            }
        });
    }
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffff00"
    android:orientation="vertical" >
    
    <TextView 
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="文本2"
        />
    <Button 
        android:id="@+id/but"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="设置文本"
        />
</LinearLayout>
Mainactivity:
package com.example.fragmentdemo1;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
布局:
<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"
    android:orientation="horizontal"
    tools:context=".MainActivity" >
 <fragment
     android:id="@+id/fg1"
     android:name="com.example.fragmentdemo1.Fragment1"
     android:layout_width="0dp"
     android:layout_weight="1"
     android:layout_height="match_parent"
     />
 <fragment
     android:id="@+id/fg2"
     android:name="com.example.fragmentdemo1.Fragment2"
     android:layout_width="0dp"
     android:layout_height="match_parent"
     android:layout_weight="1" />
</LinearLayout>
动态添加Fragment
用到了FragmentTransaction这个类,跟数据库事务差不多,对fragment进行动态的增删,并且需要调用commit方法执行事务
示例代码:
切换横竖屏时切换fragment显示
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
          Display display = getWindowManager().getDefaultDisplay();  
            if (display.getWidth() > display.getHeight()) {  
                Fragment1 fragment1 = new Fragment1();  
                ft.replace(android.R.id.content, fragment1).commit();  
            } else {  
                Fragment2 fragment2 = new Fragment2();  
                ft.replace(android.R.id.content, fragment2).commit();  
            }  



从图中可以看出,fragment的生命周期跟activity很类似,但是多了一些方法。
其中:
onAttach:Fragment和Activity建立关联的时候调用。

onCreateView():系统在fragment要画自己的界面时调用(在真正显示之前)此方法。这个方法必须 返回 frament layout 的根控件。如果这个 fragment 不提供界面,那它应返回 null

onActivityCreated:当Activity中的onCreate方法执行完后调用。

onDestroyView:Fragment中的布局被移除时调用

onDetach:Fragment和Activity解除关联的时候调用

大家可以重写所有生命周期方法,然后通过打log的方式观察生命周期,这里就不贴代码了。













  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值