• Fragment和FragmentActivity使用Demo


    在这里用代码简单演示一下两个fragment加入到同一个activity中。

    fragment相当于一个封装了的组件了,fragment里有自己的生命周期,也相当于一个Activity,区别是fragment里面重写的方法必须有:oncreate(bundle);oncreateView();onpause();

    直接上代码:

    Fragment1.class

    public class Fragment1 extends Fragment{
    /* (non-Javadoc)
    * @see android.support.v4.app.Fragment#onCreate(android.os.Bundle)
    */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    }

    /* (non-Javadoc)
    * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
    */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    return inflater.inflate(R.layout.fragment1, container, false);
    }

    /* (non-Javadoc)
    * @see android.support.v4.app.Fragment#onPause()
    */
    @Override
    public void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    }

    }

    fragment1.xml

    <?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:orientation="vertical" >
    <Button
    android:id="@+id/button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="fragment1代码添加"/>

    </LinearLayout>

    第二个

    Fragment2.java

    public class Fragment2 extends Fragment {

    /* (non-Javadoc)
    * @see android.support.v4.app.Fragment#onCreate(android.os.Bundle)
    */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    }

    /* (non-Javadoc)
    * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
    */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    return inflater.inflate(R.layout.fragment2, container, false);
    }

    /* (non-Javadoc)
    * @see android.support.v4.app.Fragment#onPause()
    */
    @Override
    public void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    }

    }

    fragmet2.xml

    <?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:orientation="vertical" >
    <Button
    android:id="@+id/button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="fragmentXML添加方式"/>

    </LinearLayout>

    装载这两个fragment的FragmentAcitivity.java

    public class MainActivity extends FragmentActivity {
    private Fragment1 fragment1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragment1 = new Fragment1();
    fragmentTransaction.add(R.id.linearcon, fragment1).commit();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }

    }

    Activity的布局文件:

    <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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" 
    android:orientation="vertical">

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />
    <LinearLayout
    android:id="@+id/linearcon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"/>
    <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <fragment
    android:name="com.tiejiang.fragmentdemo.Fragment2"
    android:id="@+id/fragment2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
    </LinearLayout>
    </LinearLayout>

    运行的结果如下:

    注:这里使用import android.support.v4.app.FragmentManager;

    则使用的是:FragmentManager fragmentManager = getSupportFragmentManager();

    后面如果用到了其提功能的话再更新这篇吧

  • 相关阅读:
    ADFUtils
    JSFUtils
    3.为JDeveloper添加不能的workspace
    ADF系列-3.VO的查询
    ArcGIS api for javascript——地图配置-增加一个调试控制台<
    ArcGIS api for javascript——显示多个ArcGIS Online服务
    ArcGIS api for javascript——放大时切换图层
    ArcGIS api for javascript——明确的创建图层列表
    ArcGIS api for javascript——动态创建图层列表
    ArcGIS api for javascript——使用图层定义显示地图
  • 原文地址:https://www.cnblogs.com/tiejiangweigaibianercunzai/p/3874327.html
Copyright © 2020-2023  润新知