本文主要是介绍学习fragment---通过编码将fragment添加到已存在的ViewGroup中,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
</pre>在activity运行的任何时候,你都可以将fragment添加到activity布局中。你仅需要简单指定用来放置fragment的ViewGroup。<p></p><p style="text-indent:21.0pt">你应当使用FragmentTransaction的API来对activity中的fragment进行操作(例如添加,移除,或者替换fragment)。你可以像下面这样从Activity中取得FragmentTransaction的实例:</p><pre class="prettyprint"><span class="pln"> </span><span class="typ">FragmentManager</span><span class="pln"> fragmentManager </span><span class="pun">=</span><span class="pln"> getFragmentManager</span><span class="pun">()</span><span class="pln"></span><span class="typ">FragmentTransaction</span><span class="pln"> fragmentTransaction </span><span class="pun">=</span><span class="pln"> fragmentManager</span><span class="pun">.</span><span class="pln">beginTransaction</span><span class="pun">();</span>
可以用add()函数添加fragment,并指定要添加的fragment以及要将其插入到哪个视图(view)之中:
ExampleFragment fragment = new ExampleFragment();fragmentTransaction.add(R.id.fragment_container, fragment);fragmentTransaction.commit();
传入add()函数的第一个参数是fragment被放置的ViewGroup,它由资源ID(resource ID)指定,第二个参数就是要添加的fragment。
一旦通过FragmentTransaction 做了更改,都应当使用commit()使变化生效。
我的例子是在一个activity中,加载两个fragment,其中另一个fragment2是通过fragment1点击按钮启动的
首先是xml布局文件
activity_main.xml
</pre><pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"android:id="@+id/Main_container"tools:context="com.shilixia.learnfragment2.MainActivity"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello World!" />
</RelativeLayout>
fragment1.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"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textAllCaps="false"android:text="启动另一个fragment"android:id="@+id/btnAnotherfragment" />
</LinearLayout>
fragment2.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"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#a5ff45"android:text="这是另一个fragment"android:id="@+id/textView"android:layout_gravity="center_horizontal" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="后退"android:id="@+id/btnBack"android:layout_gravity="center_horizontal" />
</LinearLayout>
类文件
fragment1.java
package com.shilixia.learnfragment2;import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;/*** Created by Administrator on 2016/9/7 0007.*/
public class fragment1 extends Fragment {@Nullable@Overridepublic View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {View rootView = inflater.inflate(R.layout.fragment1,container,false);rootView.findViewById(R.id.btnAnotherfragment).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.Main_container,new fragment2()).commit();}});return rootView;}
}
fragment2.java
package com.shilixia.learnfragment2;import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;/*** Created by Administrator on 2016/9/7 0007.*/
public class fragment2 extends Fragment {@Overridepublic void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);}@Nullable@Overridepublic View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {View view = inflater.inflate(R.layout.fragment2,container,false);view.findViewById(R.id.btnBack).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {getFragmentManager().popBackStack();}});return view;}@Overridepublic void onPause() {super.onPause();}
}
MainActivity.java
package com.shilixia.learnfragment2;import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);if (savedInstanceState == null){getSupportFragmentManager().beginTransaction().add(R.id.Main_container,new fragment1()).commit();}}
}
这篇关于学习fragment---通过编码将fragment添加到已存在的ViewGroup中的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!