zoukankan      html  css  js  c++  java
  • Fragment的两种加载方式。

    一、 静态加载。

         即用布局文件来加载,

        1. 主类activity类继承自v4包下的FragmentActivity,

    public class MainActivity extends FragmentActivity {
    
        public MainActivity() {
            Log.e("TAG", "MainActivity()..");
        }
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            Log.e("TAG", "MainActivity onCreate()..");
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    }

        2. 定义一个Fragment的子类,并加载一个布局文件。

    public class MyFragment1 extends Fragment {
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            
            //加载布局得到View对象并返回
            
            //创建一个视图对象, 设置数据并返回
            TextView  textView = new TextView(getActivity());
            textView.setText("fragment11111");
            textView.setBackgroundColor(Color.RED);
            
            return textView;
        }
    }
    public class MyFragment2 extends Fragment {
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            
            //加载布局得到View对象并返回
            
            //创建一个视图对象, 设置数据并返回
            TextView  textView = new TextView(getActivity());
            textView.setText("fragment22222");
            
            return textView;
        }
    }

        3. 在布局文件中通过<fragment>指定自定义的Fragment

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:orientation="horizontal">
    
        <fragment
            android:name="com.example.l12_fragment1.MyFragment1"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent" />
    
        <fragment
            android:name="com.example.l12_fragment1.MyFragment2"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent" />
    
    </LinearLayout>

     每一个Fragment本质上都是一个Framelayout,它加载的布局为其子布局。

         

    二、动态加载。

         用代码来加载,主要API:

         FragmentActivity (getSupportFragmentManager)

         Fragmentmanager (beginTrasacation() )

         FragmentTransacation  (add(),replace(),remove(), commit(),  addToBackStack() )

    --------------------------------------------------------------------------------------------------------------------

    public class MainActivity extends FragmentActivity {
    
        public MainActivity() {
            Log.e("TAG", "MainActivity()..");
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            Log.e("TAG", "MainActivity onCreate()..");
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // 创建Fragment对象
            MyFragment1 fragment1 = new MyFragment1();
            // 得到FragmentManager
            FragmentManager manager = getSupportFragmentManager();
            // 得到FragmentTransacation
            FragmentTransaction transaction = manager.beginTransaction();
            // 添加Fragment对象并提交
            transaction.add(R.id.ll_main_container, fragment1).commit();
        }
    }

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:orientation="vertical">
        
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
            
            <Button
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="显示fragment2" 
                android:onClick="showFragment2"/>
            
            <Button
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="移除fragment2" 
                android:onClick="deleteFragment2"/>
        </LinearLayout>
    
        <LinearLayout 
            android:id="@+id/ll_main_container"   ////替换的容器
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" 
            android:orientation="horizontal">
        </LinearLayout>
    </LinearLayout>
    public class MyFragment1 extends Fragment {
    
        
        @Override
        public void onCreate(Bundle savedInstanceState) {
            Log.e("TAG", "onCreate()");
            super.onCreate(savedInstanceState);
        }
        
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            Log.e("TAG", "onCreateView()");
            //加载布局得到View对象并返回
            
            //创建一个视图对象, 设置数据并返回
            TextView  textView = new TextView(getActivity());
            textView.setText("fragment11111");
            textView.setBackgroundColor(Color.RED);
            
            return textView;
        }
    }
  • 相关阅读:
    思考题回答
    Winform中DevExpress的TreeList的入门使用教程(附源码下载)
    感兴趣的开源项目列表Java 标签: jfreechart报表引擎图表制作eclipsejavabeans 20080325 11:20 466人阅读
    试用GNU Gettext 开源多语组件包 标签: delphi语言file文本编辑domainstring 20080428 17:54 1330人阅读
    DDK Source Files Allot 标签: ddk测试 20080917 16:03 312人阅读 评论(0)
    Philip 190SW8 LCD 设置 标签: 游戏 20080713 14:45 515人阅读 评论(0)
    爆笑:两分钟让你明白什么是ERP! 标签: 电话工作平台产品 20080326 13:45 311人阅读 评论(0)
    通过控件移动窗体 标签: integer 20080520 16:13 342人阅读 评论(0) 收藏
    Google Web Toolkit 真的至关重要? 标签: googlewebjavascriptgwtajax平台 20080325 09:47 273人阅
    我的Eclipse插件列表 标签: eclipse插件eclipselog4jpropertiestomcattools 20080324 15:11 477人
  • 原文地址:https://www.cnblogs.com/sdgtxuyong/p/10477409.html
Copyright © 2011-2022 走看看