fragment
来源:互联网 发布:淘宝店关键词优化 编辑:程序博客网 时间:2024/06/06 11:03
--Android在3.0中引入的主要是用在大屏幕设备上
--平板比手机大可以放更多的 Ui,组件之间会产生更多的交互
1、fragment可以作为Activity的一部分出现
2、一个Activity中可以包含多个fragment且一个Fragment也可以在多个Activity中出现
3、在Activity运行中可以添加、移除、替换Fragment
4、Fragment可以响应自己的输入事件,有自己的生命周期(并且受宿主影响)
--先创建一个子类
加载方式:静态加载、动态加载
-静态加载:在layout文件中声明Fragment
<fragment
android:id="@+id/fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.example.android_fragment.MyFragment"
/>
在《fragment》name属性指定layout中实例化的Fragment类
标示fragment的方法:提供一个唯一字符串(id或者tag);
动态加载:
---要动态加载就要完成一个事务:
---getFragmentManager().beginTransaction().
---add(这个参数是标识符(id), fragment2).commit();
注:根据包不同得到Mannager对象方法不一样
事务:3种add()、reovme()、rep...()
---如果允许用户通过按下BACK按键返回到前一个Fragment,
---在调用commit()前加入addToBackStack();
生命周期:Fragment被添加到Activity时调用方法:onAttach()
生命周期:
1.onAttach();当Fragment被添加到Activity时候会回调这个方法,并且只调用一次
2.onCreat();创建Fragment时被回调,并且只调用一次
3.onCreatView();每次创建都会绘制Fragment的View组件时调用该方法
4.onActivityCreated();当Fragment所在的Activity启动完成后调用这个方法
5.onStart();恢复Fragment时会被回调,
--调用onStart()方法后面一定会调用onResume()方法
6.onPause();暂停Fragment
7.onStop();停止Fragment
8.onDestroyView();销毁Fragment所包含的View组件
9.onDestroy();销毁Fragment回调
10.onDetach();Fragment从Activity删除时回调
这里我写了一个Activity两个Fragment一个静态添加Activity一个动态添加Activity:
package zxx.fragment;import android.os.Bundle;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentActivity;import android.util.Log;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;public class MainActivity extends FragmentActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);findViewById(R.id.textView1);MyFragment mfragment=(MyFragment)getSupportFragmentManager().findFragmentById(R.layout.fragment);Button but=(Button) findViewById(R.id.add_fragment);}public void listenner(View v ){getSupportFragmentManager().beginTransaction().add(R.id.LinearLayout1, new Fragmnet2()).commit();Log.i("******", "dianji ");}}
这里Activity所需要的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="zxx.fragment.MainActivity" > <Button android:id="@+id/add_fragment" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="listenner" android:text="添加一个Fragment" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="fragment的动/静加载" /> <fragment android:id="@+id/fragment" android:name="zxx.fragment.MyFragment" android:layout_width="match_parent" android:layout_height="wrap_content" /></LinearLayout>
这里Fragment我只实现了view方法:
public class MyFragment extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View v = inflater.inflate(R.layout.fragment, null, false);return v;}}
<pre class="html" name="code"><?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="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="一个静态fragment" /></LinearLayout>
public class Fragmnet2 extends Fragment {public Fragmnet2() {super();}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {// TODO Auto-generated method stubView v=inflater.inflate(R.layout.fragment2, null);Log.i("******", "fragmrn执行了 ");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="wrap_content" android:orientation="vertical" > <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="这是一个动态fragment"/></LinearLayout>
0 0
- Fragment
- Fragment
- fragment
- Fragment
- Fragment
- Fragment
- Fragment
- fragment
- fragment
- Fragment
- Fragment
- fragment
- Fragment
- Fragment
- Fragment
- Fragment
- Fragment
- fragment
- cognos Mashup 与 ECharts
- Android多用户系列:跨用户启动(绑定)Service、启动Activity、访问ContentProvider
- 用户定位
- Object-C--->Swift之(五)数组和字典
- Red Hat 和Cent OS 低版本安装mysql
- fragment
- 开源日志系统比较:scribe、chukwa、kafka、flume
- EJB invocations from a remote client using JNDI
- 使用DES方式,加、解密字符
- JSQMessagesViewController (一)
- linux系统shell语法简介-49
- C++语言学习资料大全分享 想要的速度下
- Nine-Patch格式图片浅析
- linux中的一些小技巧