动态加载Fragment

1.获取到FragmentManager,在Activity中可以直接通过getFragmentManager得到。
 
2.开启一个事务,通过调用beginTransaction方法开启。
 
3.向容器内加入Fragment,一般使用replace方法实现,需要传入容器的id和Fragment的实例。
 
4.提交事务,调用commit方法提交。
fragment1 的布局
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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:background="#ff00ff"
    android:orientation="vertical" >
  
    <TextView
        android:id="@+id/fr1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="fragment1" />
  
</LinearLayout>
fragment2的布局
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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:background="#0000ff"
    android:orientation="vertical" >
  
    <TextView
        android:id="@+id/fr2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="fragment2"
        android:textColor="#ffffff" />
  
</LinearLayout>
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.sanya.dynamicfragment;
  
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
  
public class Fragment1 extends Fragment {
  
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
          
             
        return  inflater.inflate(R.layout.fragment1, container, false);
    }
}
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.sanya.dynamicfragment;
  
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
  
public class Fragment2 extends Fragment {
  
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
          
             
        return  inflater.inflate(R.layout.fragment2, container, false);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.sanya.dynamicfragment;
  
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Display;
  
public class MainActivity extends FragmentActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
  
        Display display = getWindowManager().getDefaultDisplay();
        int height = display.getHeight();
        int width = display.getWidth();
        if (width > height) {
            transaction.replace(android.R.id.content, new Fragment1());
        } else {
            transaction.replace(android.R.id.content, new Fragment2());
        }
  
        transaction.commit();
    }
  
}
 
 
 
 


来自为知笔记(Wiz)


posted on 2014-04-28 13:56  转折点人生  阅读(470)  评论(0)    收藏  举报