FragmentActivity提供这么一个方法
//
Return the FragmentManager for interacting with fragments associated with this activity.
public FragmentManager getSupportFragmentManager()
也就是说用于Activity与Fragment之间的联系交流
FragmentManager提供这么几个方法
abstract FragmentTransaction beginTransaction()
Start a series of edit operations on the Fragments associated with this FragmentManager.
可以通过定义一个 FragmentTransaction transaction = manager.beginTransaction();
abstract Fragment findFragmentById(int id)
Finds a fragment that was identified by the given id either when inflated from XML or as the container ID when added in a transaction.
abstract Fragment findFragmentByTag(String tag)
Finds a fragment that was identified by the given tag either when inflated from XML or as supplied when added in a transaction.
也就是说可以通过id,或者tag的查找方法去找到对应的Fragment,例如下面用法
messageFragment = (MessageFragment)fragmentManager.findFragmentByTag(
"message")
;
callFragment = (CallFragment)fragmentManager.findFragmentByTag("call");
Tranction的使用
abstract FragmentTransaction replace(int containerViewId, Fragment fragment, String tag)
Replace an existing fragment that was added to a container.
abstract FragmentTransaction replace(int containerViewId, Fragment fragment)
Calls
replace(int, Fragment, String) with a null tag.
这个方法是将Fragment替换
abstract int commit()
Schedules a commit of this transaction.
abstract int commitAllowingStateLoss()
Like
commit() but allows the commit to be executed after an activity's state is saved.
详细解析如下
public abstract int commit ()
Schedules a commit of this transaction. The commit does not happen immediately; it will be scheduled as work on the main thread to be done the next time that thread is ready.
A transaction can only be committed with this method prior to its containing activity saving its state. If the commit is attempted after that point, an exception will be thrown. This is because the state after the commit can be lost if the activity needs to be restored from its state. See commitAllowingStateLoss() for situations where it may be okay to lose the commit.
Returns
Returns the identifier of this transaction's back stack entry, if addToBackStack(String) had been called. Otherwise, returns a negative number.
public abstract int commitAllowingStateLoss ()
Like
commit()
but allows the commit to be executed after an activity's state is saved. This is dangerous because the commit can be lost if the activity needs to later be restored from its state, so this should only be used for cases where it is okay for the UI state to change unexpectedly on the user.
按我的理解相当于用于提交
主要常见的有这几种用法,具体实现查看E盘-AndroidProject-FragmentConvert
所以 完整地加载一个fragment需要如下
FragmentManager fragmentManager = getSupportFragmentManager();//获得Manager
FragmentTransaction transaction = fragmentManager.beginTransaction();//获得事务
callFragment = new CallFragment();//创建一个自定义Fragment
transaction.add(R.id.fl_content, callFragment, "zhishi");//加进去事务
transaction.show(callFragment);//显示该fragment
transaction.hide(messageFragment);//隐藏另一个fragment
在Layout的代码如下
主视图
<FrameLayout
android
:id=
"@+id/fl_content"
android
:layout_width=
"fill_parent"
android:layout_height="fill_parent" />
另外还需要自己定义xml
<?
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"
>
<TextView
android
:layout_width=
"match_parent"
android
:layout_height=
"match_parent"
android
:text=
"这是电话界面"
/>
</LinearLayout>
自定义的CallFragment
public class CallFragment
extends Fragment {
@Override
public View
onCreateView(LayoutInflater inflater
,
ViewGroup container
, Bundle savedInstanceState) {
return inflater.inflate(R.layout.
fragment_call
, null)
;
}
}
主要常见的有这几种用法,具体实现查看E盘-AndroidProject-FragmentConvert
转载请注明原文地址: https://ju.6miu.com/read-5652.html