Fragment的创建:
-
public class ContentFragment extends Fragment
-
{
-
@Override
-
public View onCreateView(LayoutInflater inflater, ViewGroup container,
-
Bundle savedInstanceState)
-
{
-
return inflater.inflate(R.layout.fragment_content, container, false);
-
}
-
}
静态使用:
在布局文件中直接写就可以、很简单。
-
<fragment
-
android:layout_below="@id/id_fragment_title"
-
android:id="@+id/id_fragment_content"
-
android:name="com.zhy.zhy_fragments.ContentFragment"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent" />
动态Fragment的使用:
在Fragment的父所属的Activity中留出一块位置:
这边暂且用一个FramLayout占一个位置,下面在代码中用Fragment对着块进行赋予Fragment,可以动态的用别的Fragment替换。
-
<FrameLayout
-
android:id="@+id/id_content"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
android:layout_above="@id/id_ly_bottombar"
-
android:layout_below="@id/id_fragment_title" />
Activity中的代码:
-
public class MainActivity extends Activity implements OnClickListener
-
{
-
private LinearLayout mTabWeixin;
-
private LinearLayout mTabFriend;
-
-
private ContentFragment mWeixin;
-
private FriendFragment mFriend;
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState)
-
{
-
super.onCreate(savedInstanceState);
-
requestWindowFeature(Window.FEATURE_NO_TITLE);
-
setContentView(R.layout.activity_main);
-
-
// 初始化控件和声明事件
-
mTabWeixin = (LinearLayout) findViewById(R.id.tab_bottom_weixin);
-
mTabFriend = (LinearLayout) findViewById(R.id.tab_bottom_friend);
-
mTabWeixin.setOnClickListener(this);
-
mTabFriend.setOnClickListener(this);
-
-
// 设置默认的Fragment
-
setDefaultFragment();
-
}
-
这一块代码是动态使用Fragment的关键代码,只需要记住写法就行:
-
private void setDefaultFragment()
-
{
-
FragmentManager fm = getFragmentManager();
-
FragmentTransaction transaction = fm.beginTransaction();
-
mWeixin = new ContentFragment();
-
transaction.replace(R.id.id_content, mWeixin);
-
transaction.commit();
-
}
-
-
@Override
-
public void onClick(View v)
-
{
-
FragmentManager fm = getFragmentManager();
-
// 开启Fragment事务
-
FragmentTransaction transaction = fm.beginTransaction();
-
-
switch (v.getId())
-
{
-
case R.id.tab_bottom_weixin:
-
if (mWeixin == null)
-
{
-
mWeixin = new ContentFragment();
-
}
-
// 使用当前Fragment的布局替代id_content的控件
-
transaction.replace(R.id.id_content, mWeixin);
-
break;
-
case R.id.tab_bottom_friend:
-
if (mFriend == null)
-
{
-
mFriend = new FriendFragment();
-
}
-
transaction.replace(R.id.id_content, mFriend);
-
break;
-
}
-
// transaction.addToBackStack();
-
// 事务提交
-
transaction.commit();
-
}
-
-
}
上面activity 中的代码 对刚刚在布局中留给Fragment的位置赋予了各种Fragment,红色的代码就是我们用来占Fragment的Id,也是我们对那一块位置进行赋予Fragment的步骤。上面用的都是replace(),此外Fragment的动态加载还有各种,例如:
transaction.add()
往Activity中添加一个Fragment
transaction.remove()
从Activity中移除一个Fragment,如果被移除的Fragment没有添加到回退栈(回退栈后面会详细说),这个Fragment实例将会被销毁。
transaction.replace()
使用另一个Fragment替换当前的,实际上就是remove()然后add()的合体~
transaction.hide()
隐藏当前的Fragment,仅仅是设为不可见,并不会销毁
transaction.show()
显示之前隐藏的Fragment
detach()
会将view从UI中移除,和remove()不同,此时fragment的状态依然由FragmentManager维护。
attach()
重建view视图,附加到UI上并显示。