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;
        }
    }
  • 相关阅读:
    Android基于XMPP Smack Openfire下学习开发IM(六)总结
    排序数组中重复最对的数字长度
    Android之ContextMenu的使用方法以及与OptionMenu的区别
    DirectShow Filter 开发典型例子分析 ——字幕叠加 (FilterTitleOverlay)1
    javascript排序 查找算法大全
    memcpy和strlen函数的实现
    读书笔记——数据库的ADO开发总结
    一个类似“火柴棍”问题的面试题
    使用GSoap开发WebService客户端与服务端
    Java.io下的方法是对磁盘上的文件进行磁盘操作
  • 原文地址:https://www.cnblogs.com/sdgtxuyong/p/10477409.html
Copyright © 2011-2022 走看看