Fragment使用简单示例
生活随笔
收集整理的這篇文章主要介紹了
Fragment使用简单示例
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Fragment即碎片,使用它可以減少Activity的代碼量,降低耦合,使代碼更清爽,更易于讀懂。它和Activity具有類似的生命周期,連主要使用的方法也一樣。
Fragment添加到Activity的FragmetLayout:
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.add(R.id.fragment_content, Fragment1.getInstance(),"fragmentTag"); transaction.commit();Fragment的切換:
transaction.replace(R.id.fragment_content, Fragment2.getInstance(),"fragmentTag");添加使用add方法。替換使用replace方法。
下面是簡單的實現(xiàn):
java代碼
Activity部分:
package com.example.Activity;import com.example.R; import com.example.Fragment.Fragment1; import com.example.Fragment.Fragment2;import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button;public class MainActivity extends FragmentActivity {private Button btn_qiehuan;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();setDefaultFragment(savedInstanceState);}/*** 初始化視圖*/private void initView() {btn_qiehuan = (Button)findViewById(R.id.button_qiehuan);viewListen();}/*** 設(shè)置默認的Fragment* @param savedInstanceState */private void setDefaultFragment(Bundle savedInstanceState) {if (savedInstanceState==null) {FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();//將fragment對象add添加到FrameLayout中。而FrameLayout是在Activity中,所以也即綁定到Activitytransaction.add(R.id.fragment_content, Fragment1.getInstance(),"fragmentTag");//添加transaction.commit();}}/*** 切換Fragment*/public void switchFragment(){FragmentManager fm = getSupportFragmentManager();//Fragment管理器FragmentTransaction transaction = fm.beginTransaction();//fragment事務(wù)/** 每次添加與切換Fragment都共用一個標志:"fragmentTag",因此通過該標識能獲取當前fragment*/Fragment currFragment = fm.findFragmentByTag("fragmentTag");if (currFragment instanceof Fragment1) {//如果當前Fragment 是Fragment1的對象//replace替換transaction.replace(R.id.fragment_content, Fragment2.getInstance(),"fragmentTag");//replace替換}else{transaction.replace(R.id.fragment_content, Fragment1.getInstance(),"fragmentTag");//replace替換}transaction.commit();//提交 }/*** 設(shè)置監(jiān)聽*/private void viewListen() {btn_qiehuan.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {switchFragment();}});}}Fragment部分:
Fragment1:
package com.example.Fragment;import com.example.R;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 {private static Fragment1 instance;private Fragment1() {}public static Fragment1 getInstance(){if (instance == null) {instance = new Fragment1();}return instance; }@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View rootView = inflater.inflate(R.layout.fragment_1, container,false);return rootView;} }Fragment2:
package com.example.Fragment;import com.example.R;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 {private static Fragment2 instance;private Fragment2() {}public static Fragment2 getInstance(){if (instance == null) {instance = new Fragment2();}return instance; }@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View rootView = inflater.inflate(R.layout.fragment_2, container,false);return rootView;} }xml布局
activity_main.xml
<RelativeLayout 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" ><LinearLayout android:id="@+id/titleBar"android:layout_width="match_parent"android:layout_height="50dp"android:layout_alignParentTop="true"android:background="#22ff22"android:gravity="center_vertical" ><TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Activity標題欄" /></LinearLayout><Button android:id="@+id/button_qiehuan"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_alignParentRight="true"android:text="切換Fragment" /><FrameLayout android:id="@+id/fragment_content"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_above="@+id/button_qiehuan"android:layout_below="@+id/titleBar" ></FrameLayout></RelativeLayout>fragment_1.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" ><TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Fragment1"android:textSize="25sp" /></LinearLayout>fragment_2
<?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" ><TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Fragment2"android:textSize="25sp"/></LinearLayout>總結(jié)
以上是生活随笔為你收集整理的Fragment使用简单示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html 公告栏 上下滚动,jQuery
- 下一篇: 面向对象的五个基本原则