Fragment是Android中一个非常重要的知识,但是好多同学却不熟悉或者根本不明白它的使用,下面我们详细介绍一下它的使用,本次主要讲解它的两种使用方法:简单使用和动态注册。关于Fragment与Activity之间的通信以及生命周期方法我们下次再讲解。
1.1 Fragment的使用
1.1.1 Fragment的简单使用
这里我们准备一个简单的Fragment的示例,在一个活动中添加两个Fragment,并让这两个Fragment平分整个活动控件新建一个左侧Fragment布局left ragment.xml,代码如下所示:
< LinearLayout xmlns : android =" http :// schemas . android .c
om/apk /res / android "
android : layout_width =" match_parent "
android : layout_height =" match_parent "
android : orientation =" vertical " >
<Button
android :id ="@+id/ button "
android : layout_width =" wrap_content "
android : layout_height =" wrap_content "
android : layout_gravity =" center_horizontal "
android : text =" Button "
/>
</ LinearLayout >
这个布局非常简单,只是放置了一个按钮,并让他水平居中显示。然后新建右侧碎片布局right fragment.xml,代码如下所示:
< LinearLayout xmlns : android =" http :// schemas . android .c
om/apk /res / android "
android : layout_width =" match_parent "
android : layout_height =" match_parent "
android : background ="#00 ff00 "
android : orientation =" vertical " >
<TextView
android : layout_width =" wrap_content "
android : layout_height =" wrap_content "
android : layout_gravity =" center_horizontal "
android : textSize ="20 sp"
android : text =" This is right fragment "
/>
</ LinearLayout >
这里我们放置了一个TextView用于显示文本,并将布局背景设置成绿色接着新建一个LeftFragment类,并继承自Fragment。代码如下:
public class LeftFragment extends Fragment {
@Override
public View onCreateView ( LayoutInflater inflater ,
ViewGroup container , Bundle savedInstanceState ) {
View view = inflater . inflate (R. layout . left_fragment ,
container , false );
return view ;
}
}
这里重写了Fragment的OnCreateView方法,接着调用LayoutInater的inate()方法把刚才定义的left fragment布局动态添加进来接着再新建一个RightFragment,代码如下:
public class RightFragment extends Fragment {
@Override
public View onCreateView ( LayoutInflater inflater ,
ViewGroup container , Bundle savedInstanceState ) {
View view = inflater . inflate (R. layout . right_fragment ,
container , false );
return view ;
}
}
接下来创建一个FragmentTest项目,并修改activity main.xml的代码,如下所示:
< LinearLayout xmlns : android =" http :// schemas . android .c
om/apk /res / android "
android : layout_width =" match_parent "
android : layout_height =" match_parent " >
<fragment
android :id ="@+id/ left_fragment "
android : name =" com . example . fragmenttest . LeftFragment "
android : layout_width ="0 dp"
android : layout_height =" match_parent "
android : layout_weight ="1" />
<fragment
android :id ="@+id/ right_fragment "
android : name =" com . example . fragmenttest . RightFragment "
android : layout_width ="0 dp"
android : layout_height =" match_parent "
android : layout_weight ="1" />
</ LinearLayout >
可以看到,我们在布局文件中使用<Fragment>
1.1.2 Fragment的动态添加
上一节,我们在布局文件中进行了Fragment的添加,这一节我们在程序运行过程中把动态添加Fragment到活动中首先我们再新建一个项目,修改它的activity main.xml文件,代码如下:
public class RightFragment extends Fragment {
@Override
public View onCreateView ( LayoutInflater inflater ,
ViewGroup container , Bundle savedInstanceState ) {
View view = inflater . inflate (R. layout . right_fragment ,
container , false );
return view ;
}
}
接下来创建一个FragmentTest项目,并修改activity main.xml的代码,如下所示:
< 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 : paddingBottom =" @dimen / activity_vertical_margin "
android : paddingLeft =" @dimen / activity_horizontal_margin "
android : paddingRight =" @dimen / activity_horizontal_margin "
android : paddingTop =" @dimen / activity_vertical_margin "
tools : context =". MainActivity "
android : orientation =" horizontal " >
< LinearLayout
android : layout_width =" wrap_content "
android : layout_height =" match_parent "
android : orientation =" vertical ">
<Button
android :id ="@+id/bt1 "
android : layout_width =" wrap_content "
android : layout_height =" wrap_content "
android : text =" fargment01 "/>
<Button
android :id ="@+id/bt2 "
android : layout_width =" wrap_content "
android : layout_height =" wrap_content "
android : text =" fragment02 "/>
<Button
android :id ="@+id/bt3 "
android : layout_width =" wrap_content "
android : layout_height =" wrap_content "
android : text =" fragment03 "/>
</ LinearLayout >
<FrameLayout
android :id ="@+id/fl"
android : layout_width =" match_parent "
android : layout_height =" match_parent "
>
</ FrameLayout >
</ LinearLayout >这里我们我们设置了活动界面,左边三个按钮竖直排列,右边是一个FrameLayout用于显示Fragment,当我们点击不同的按钮,右边切换不同的Fragment。下面我们创建三个不同的Fragment类,分别设置他们的布局,代码如下:
public class Fragment01 extends Fragment {
@Override
public View onCreateView ( LayoutInflater inflater , ViewGroup container ,
Bundle savedInstanceState ) {
View v = inflater . inflate (R. layout . fragment01 , null );
return v;
}
<?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 : orientation =" vertical "
android : background ="# ff0000 ">
<TextView
android : layout_width =" wrap_content "
android : layout_height =" wrap_content "
android : text这是第一个=" fragment "
android : textSize ="20 sp "/>
<EditText
android :id ="@+id/ et_frag "
android : layout_width =" wrap_content "
android : layout_height =" wrap_content "/>
</ LinearLayout >
public class Fragment02 extends Fragment {
@Override
public View onCreateView ( LayoutInflater inflater , ViewGroup container ,
Bundle savedInstanceState ) {
// TODO Auto - generated method stub
View v = inflater . inflate (R. layout . fragment02 , null );
- 4 -
return v;
}
}
<?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 : orientation =" vertical "
android : background ="#00 ff00 ">
<TextView
android : layout_width =" wrap_content "
android : layout_height =" wrap_content "
android : text这是第二个=" fragment "
android : textSize ="20 sp "/>
</ LinearLayout >
public class Fragment03 extends Fragment {
private TextView tv;
@Override
public View onCreateView ( LayoutInflater inflater , ViewGroup container ,
Bundle savedInstanceState ) {
// TODO Auto - generated method stub
View v = inflater . inflate (R. layout . fragment03 , null );
return v;
}
<?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 : orientation =" vertical "
android : background ="#0000 ff" >
<TextView
android : layout_width =" wrap_content "
android : layout_height =" wrap_content "
android : text这是第三个=" fragment "
android : textSize ="20 sp "/>
</ LinearLayout >
动态添加碎片主要分为5步:
1. 创建待添加的碎片实例。
2.获取到FragmentManager,在活动中可以直接调用getFragmentManager()方法得到。
3. 开启一个事务,通过调用beginTransaction()方法开启。
4. 向容器内加入碎片,一般使用replace()方法实现,需要传入容器的id和待添加的碎片实例。
5. 提交事务,调用commit()方法来完成。
接着在主活动中为按钮设置点击监听事件,当我们点击按钮后,实现Fragment的切换,代码如下:
public class MainActivity extends Activity {
private Fragment03 fg3 ;
@Override
protected void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState );
setContentView (R. layout . activity_main );
Button bt1 = ( Button ) findViewById (R.id.bt1 );
Button bt2 = ( Button ) findViewById (R.id.bt2 );
Button bt3 = ( Button ) findViewById (R.id.bt3 );
Fragment01 fg1 = new Fragment01 ();
FragmentManager fm = getFragmentManager ();
FragmentTransaction ft = fm. beginTransaction ();
ft. replace (R.id.fl , fg1 );
ft. commit ();
bt1. setOnClickListener (new OnClickListener () {
@Override
public void onClick ( View v) {
TODO Auto - generated method stub
Fragment01 fg1 = new Fragment01 ();
FragmentManager fm = getFragmentManager ();
FragmentTransaction ft = fm. beginTransaction ();
ft. replace (R.id.fl , fg1 );
ft. commit ();
}
});
bt2. setOnClickListener (new OnClickListener () {
@Override
public void onClick ( View v) {
TODO Auto - generated method stub
Fragment02 fg2 = new Fragment02 ();
FragmentManager fm = getFragmentManager ();
FragmentTransaction ft = fm. beginTransaction ();
ft. replace (R.id.fl , fg2 );
ft. commit ();
}
});
bt3. setOnClickListener (new OnClickListener () {
@Override
public void onClick ( View v) {
fg3 = new Fragment03 ();
FragmentManager fm = getFragmentManager ();
FragmentTransaction ft = fm. beginTransaction ();
ft. replace (R.id.fl , fg3 );
ft. commit ();
}
});
}
这样就完成了在活动中动态添加Fragment的功能,运行程序,分别点击按钮,会在不同的Fragment之间切换
Copyright © 2004-2024 华清远见教育科技集团 版权所有
京ICP备16055225号-5,京公海网安备11010802025203号