APIDemo分析:fragment的使用方法

来源:互联网 发布:manifest.json是什么 编辑:程序博客网 时间:2024/06/01 09:59

public class FragmentArguments extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);Log.e("_____tag", "onCreate in main avtivity___1");setContentView(R.layout.fragment_arguments);Log.e("_____tag", "onCreate in main avtivity____2");if (savedInstanceState != null) {// First-time init; create fragment to embed in activity.FragmentTransaction ft = getFragmentManager().beginTransaction();Fragment newFragment = MyFragment.newInstance("From Arguments");ft.add(R.id.created, newFragment);ft.commit();Log.e("_____tag", "onCreate in main avtivity____3");}}public static class MyFragment extends Fragment {CharSequence mLabel;/** * Create a new instance of MyFragment that will be initialized with the * given arguments. */static MyFragment newInstance(CharSequence label) {MyFragment f = new MyFragment();Bundle b = new Bundle();b.putCharSequence("label", label);f.setArguments(b);return f;}/** * Parse attributes during inflation from a view hierarchy into the * arguments we handle. */@SuppressLint("NewApi")@Overridepublic void onInflate(Activity activity, AttributeSet attrs,Bundle savedInstanceState) {super.onInflate(activity, attrs, savedInstanceState);Log.e("_____tag", "onInflate in MyFragment______1");TypedArray a = activity.obtainStyledAttributes(attrs,R.styleable.FragmentArguments);mLabel = a.getText(R.styleable.FragmentArguments_android_label);Log.e("_____tag", (String)mLabel + "in onInflate in MyFragment______2");a.recycle();}/** * During creation, if arguments have been supplied to the fragment then * parse those out. */@SuppressLint("NewApi")@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);Log.e("_____tag", "onCreate in fragment");Bundle args = getArguments();Log.e("_____tag", (String)mLabel + "onCreate in MyFragment____1");if (args != null) {mLabel = args.getCharSequence("label", mLabel);Log.e("_____tag", (String)mLabel + "in onCreate in MyFragment____2");}}/** * Create the view for this fragment, using the arguments given to it. */@SuppressWarnings("deprecation")@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {Log.e("_____tag", "onCreateView in MyFragment____1");View v = inflater.inflate(R.layout.hello_world, container, false);View tv = v.findViewById(R.id.text);Log.e("_____tag", (String)mLabel + "in onCreateView in MyFragment____2");((TextView) tv).setText(mLabel != null ? mLabel : "(no label)");tv.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb));return v;}}}


好吧这是APIDemo的源码,里面加了点log用来分析的。

setContentView(R.layout.fragment_arguments);这个显示用户界面信息,来看看fragment_arguments.xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:padding="4dip"    android:gravity="center_horizontal"    android:layout_width="match_parent" android:layout_height="match_parent">    <TextView            android:id="@+id/text"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="0"            android:padding="4dip"            android:layout_gravity="center_vertical|center_horizontal"            android:gravity="top|center_horizontal"            android:textAppearance="?android:attr/textAppearanceMedium"            android:text="@string/fragment_arguments_msg" />    <LinearLayout android:orientation="horizontal" android:padding="4dip"        android:layout_width="match_parent" android:layout_height="wrap_content">        <fragment class="com.example.android.apis.app.FragmentArguments$MyFragment"                android:id="@+id/embedded"                android:layout_width="0px" android:layout_height="wrap_content"                android:layout_weight="1"                android:label="@string/fragment_arguments_embedded" />        <FrameLayout                android:id="@+id/created"                android:layout_width="0px"                android:layout_height="wrap_content"                android:layout_weight="1" />    </LinearLayout></LinearLayout>

LinearLayout就不说了,主要是fragment 和FrameLayout这2个标签. 第一个fragment指定了类MyFragment,就是在Activity里面定义的内部类。指定了类那么在setContentView的时候再内部调用了MyFragment类中复写的一些方法就可以显示一个fragment提供的UI 布局(From Attributes这个框框)。

水平布局layout_weight=1,在From Attributes普遍还有一个FrameLayout目前还是没有UI界面显示出来

if (savedInstanceState != null) {// First-time init; create fragment to embed in activity.FragmentTransaction ft = getFragmentManager().beginTransaction();Fragment newFragment = MyFragment.newInstance("From Arguments");ft.add(R.id.created, newFragment);ft.commit();Log.e("_____tag", "onCreate in main avtivity____3");}

本来savedInstanceState == null是这样的条件,在第一次初始化的时候savedInstanceState 这个值应该是空的!可以进入IF,一个MyFragment添加到了R.id.created,commit提交后,就可以显示在屏幕上了。

每使用MyFragment创建一个对象用来显示在屏幕上,MyFragment的生命周期就会执行,即MyFragment复写的几个函数会执行。


这里发现了2类实用Fragment的不同方法。

1.直接在xml文件里面添加自定义的fragment类的标签实用,

2.在xml里面布局framelayout标签,获取ID后,将MyFragment使用FragmentTransaction对象的 add方法添加的指定的ID,commit提交后显示界面


思考:自定义的MyFragment出现在xml里面的时候只是可以使用例程中的方法指定类名吗,直接使用类名作为标签可以吗?

            在xml里面布局的标签不使用framelayout,使用其他标签例如Linraelayout,可以在里面commit自定义的fragment 吗?

    FragmentTransaction可以用来管理MyFragment,还有关于back stack的处理是和它相关的看看其他例子FragmentTransaction是怎么管理Fragment的。。。










0 0
原创粉丝点击