Fragment技术之加载fragment视图

从android API中可以发现,要创建一个Fragment必须重写其生命周期的onCreate()、onCreateView()、onPause()三个方法。我们可以在onCreateView()方法中动态的去加载Fragment对应的视图。

MainActivity.java代码如下:

public class MainActivity extends FragmentActivity {

	private Button button;
	private FragmentManager manager;
	/*事物*/
	private FragmentTransaction transaction;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		/*获取按钮*/
		button = (Button) this.findViewById(R.id.button1);
		
		/*获取Manager*/
		manager = this.getSupportFragmentManager();
		
		/*处理按钮的监听事件*/
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				/*创建事物*/
				transaction = manager.beginTransaction();
				/*创建一个Fragment*/
				RightFragment rightFragment = new RightFragment();
				/*通过事物管理器把fragment添加到右侧的容器中(注:第一个参数是id,不是layout)*/
				transaction.add(R.id.right, rightFragment);
				/*提交事物*/
				transaction.commit();
			}
		});
	}


}


RightFragment.java代码如下:

public class RightFragment extends Fragment {

	/*无参构造函数*/
	public RightFragment() {

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		/*动态获取Fragment布局*/
		View view = inflater.inflate(R.layout.right, null);
		/*从动态布局中获取按钮*/
		Button button = (Button) view.findViewById(R.id.button1);
		/*设置Fragment布局中按钮的点击事件*/
		button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				/*第一个参数为上下文,这里使用getActivity()表示获取当前Fragment对应的Activity*/
				Toast.makeText(getActivity(), "hello world", 1).show();
			}
		});
		return view;
	}

	@Override
	public void onPause() {
		
		super.onPause();
	}

}


activity_main.xml:

<LinearLayout 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:orientation="horizontal"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#CCCCCC"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="显示" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/right"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:background="#CCFFDD"
        android:orientation="vertical" >
    </LinearLayout>

</LinearLayout>


right.xml:

<LinearLayout 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:orientation="horizontal"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#CCCCCC"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="显示" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/right"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:background="#CCFFDD"
        android:orientation="vertical" >
    </LinearLayout>

</LinearLayout>


demo结果展示

Fragment技术之加载fragment视图_第1张图片 Fragment技术之加载fragment视图_第2张图片

你可能感兴趣的:(android,layout,Fragment,布局)