Fragment
来源:互联网 发布:免费下载音乐的软件 编辑:程序博客网 时间:2024/06/10 06:58
碎片能让我们更好地利用屏幕的空间,接下来就是Fragment的方法
新建一个左侧碎片布局left_fragment.xml,代码如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <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_frgment.xml,代码如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="#00ff00" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:textSize="20sp" android:text="This is right fragment"/> </LinearLayout>
接着新建一个LeftFragment类,并让它继承自Fragment。注意这里会有两个包下的Fragment供你选择,最好使用support-v4库中的Fragment,因为它能让碎片在所有Android系统版本中保持功能一致性。代码如下:
public class LeftFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.left_fragment, container, false); return view; }}
这里仅是重写了Fragment的onCreateView()方法,然后通过LayoutInflater的inflate()方法将刚才定义的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; }}
修改activity_main.xml中的代码:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:id="@+id/left_fragment" android:name="com.example.administrator.fragementtest.LeftFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <fragment android:id="@+id/right_fragment" android:name="com.example.administrator.fragementtest.RightFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/></LinearLayout>
我们使用<fragment>标签在布局中添加碎片。这里需要通过android:name属性来显式指明要添加的碎片类名,注意一定要将类的包名也加上
动态添加碎片
以上面的为基础,新建another_right_fragment.xml,代码如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="#ffff00" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:textSize="20sp" android:text="This is another right fragment"/></LinearLayout>
这个布局的代码和right_fragment.xml中的代码基本相同,只是将背景色和显示的文字改了改。
然后新建AnotherRightFragment,代码如下:
public class AnotherRightFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.another_right_fragment,container,false); return view; }}
修改activity_main.xml,代码如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:id="@+id/left_fragment" android:name="com.example.administrator.fragementtest.LeftFragment" android:layout_width="match_parent" android:layout_height="match_parent"/> <FrameLayout android:id="@+id/right_layout" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"> </FrameLayout></LinearLayout>
将右侧碎片替换成了一个FrameLayout,所有的控件都默认摆在布局的左上角。
修改MainActivity中的代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button) findViewById(R.id.button); button.setOnClickListener(this); replaceFragment(new RightFragment()); } private void replaceFragment(Fragment fragment) { FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction transaction = fragmentManager.beginTransaction(); transaction.replace(R.id.right_layout, fragment); transaction.addToBackStack(null); transaction.commit(); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.button : replaceFragment(new AnotherRightFragment()); break; default: break; } }}
给左侧碎片中的按钮注册了一个点击事件,然后调用replaceFragment()方法动态添加RightFragment这个碎片,当点击左侧碎片中那个按钮时,又会调用replaceFragment()方法将右侧碎片替换成AnotherRightFragment。其中transaction.addToBackStack()实现在碎片中模拟返回栈,它可以接收一个名字用于描述返回栈的状态,一般传入null即可。
结合以上代码可知动态添加碎片主要分为五步:
- 创建待添加的碎片实例。
- 获取FragmentManager,在活动中可以直接通过调用getSupportFragmentManager( )方法得到。
- 开启一个事务,通过调用beginTransaction( )开启。
- 向容器内添加或替换碎片,一般使用replace( )方法实现,需要传入容器的id和待添加的碎片实例。
- 提交事务,调用commit( )方法来完成。
碎片和活动之间进行通信
碎片的生命周期
①Activity加载Fragment的时候,依次调用下面的方法: onAttach -> onCreate -> onCreateView -> onActivityCreated -> onStart ->onResume②当我们弄出一个悬浮的对话框风格的Activity,或者其他,就是让Fragment所在的Activity可见,但不获得焦点 onPause③当对话框关闭,Activity又获得了焦点: onResume④当我们替换Fragment,并调用addToBackStack()将他添加到Back栈中 onPause -> onStop -> onDestoryView !!注意,此时的Fragment还没有被销毁哦!!!⑤当我们按下键盘的回退键,Fragment会再次显示出来: onCreateView -> onActivityCreated -> onStart -> onResume⑥如果我们替换后,在事务commit之前没有调用addToBackStack()方法将 Fragment添加到back栈中的话;又或者退出了Activity的话,那么Fragment将会被完全结束, Fragment会进入销毁状态 onPause -> onStop -> onDestoryView -> onDestory -> onDetach
本文大部分参考《第一行代码·第二版》和菜鸟教程中的Fragment教程
阅读全文
0 0
- Fragment
- Fragment
- fragment
- Fragment
- Fragment
- Fragment
- Fragment
- fragment
- fragment
- Fragment
- Fragment
- fragment
- Fragment
- Fragment
- Fragment
- Fragment
- Fragment
- fragment
- 专业术语翻译
- winform 控件中显示实时时间
- UGUI 学习笔记 4 Image
- Monkey脚本检测内存泄漏学习小记
- The Water Problem RMQ区间最大值
- Fragment
- “天鸽”袭港遇天鸽被沽空,最有力的证据还是业绩
- mysql 使用CONCAT函数、group_concat函数,查询的结果为bolob类型解决办法
- linux打补丁和git打patch方法
- Android 检测应用中的UI卡顿的工具
- 网狐棋牌6603棋牌商业源码+详细架设教程!
- C#并发操作
- C++学习笔记(更新……)
- 实战CentOS6.3部署Hadoop集群服务