通过动态加载fragment实现在一个Activity拥有3种不同的布局,直接看效果图吧:
常规模式下:
点击家居控制:
代码:
AndroidManifest.xml——没有做任何修改,创建工程默认
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.wxl.fragment" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.wxl.fragment.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
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:background="#ffcccc" android:orientation="vertical" > <TextView android:id="@+id/fragment1_textView" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="第一个Fragment界面" android:textSize="50sp"/> </LinearLayout>
fragment2.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:background="#00ffff" android:orientation="vertical" > <TextView android:id="@+id/fragment2_textView" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="第二个Fragment界面" android:textSize="50sp"/> </LinearLayout>
fragment3.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:background="#ffff00" android:orientation="vertical" > <TextView android:id="@+id/fragment2_textView" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="第三个Fragment界面" android:textSize="50sp"/> </LinearLayout>
activity_main.xml
<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:orientation="vertical" android:background="#000000" android:id="@+id/main"> <RelativeLayout android:layout_width="match_parent" android:layout_height="80dp" android:background="@drawable/main_menu_bk" android:orientation="vertical" > <!-- android:splitMotionEvents="false"多个view是否分流touch 事件 --> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="horizontal" android:layout_centerHorizontal="true" android:splitMotionEvents="false"> <LinearLayout android:id="@+id/menu_1" android:layout_width="250dp" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="center" android:gravity="center" android:text="常规模式" android:textColor="#ffffffff" android:textSize="28sp" /> </LinearLayout> <View android:layout_width="4dp" android:layout_height="match_parent" android:layout_marginBottom="10dp" android:layout_marginTop="10dp" android:background="?android:attr/listDivider"/> <LinearLayout android:id="@+id/menu_2" android:layout_width="250dp" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="center" android:gravity="center" android:text="家居控制" android:textColor="#ffffffff" android:textSize="28sp" /> </LinearLayout> <View android:layout_width="3dp" android:layout_height="match_parent" android:layout_marginBottom="10dp" android:layout_marginTop="10dp" android:background="?android:attr/listDivider"/> <LinearLayout android:id="@+id/menu_3" android:layout_width="250dp" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="center" android:gravity="center" android:text="幸福社区" android:textColor="#ffffffff" android:textSize="28sp" /> </LinearLayout> </LinearLayout> </RelativeLayout> <LinearLayout android:id="@+id/root" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > </LinearLayout> </LinearLayout>
Fragment1.java
package com.wxl.fragment; import android.annotation.SuppressLint; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @SuppressLint("NewApi") public class Fragment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.fragment1, container,false); } }
Fragment2.java
package com.wxl.fragment; import android.annotation.SuppressLint; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @SuppressLint("NewApi") public class Fragment2 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.fragment2, container,false); } }
Fragment3.java
package com.wxl.fragment; import android.annotation.SuppressLint; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @SuppressLint("NewApi") public class Fragment3 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.fragment3, container,false); } }
MainActivity.java
package com.wxl.fragment; import android.annotation.SuppressLint; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.LinearLayout; public class MainActivity extends Activity implements OnClickListener{ public static final int NORMAL_MODE = 0; public static final int SMARTHOME_MODE = 1; public static final int COMMUNITY_MODE = 2; FragmentManager fragmentManager; private int mMode = NORMAL_MODE; private Fragment mFragment;//记录当前处于哪个fragment private Fragment1 fragment1; private Fragment2 fragment2; private Fragment3 fragment3; @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { LinearLayout menu1, menu2, menu3; super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); menu1 = (LinearLayout)this.findViewById(R.id.menu_1); menu1.setOnClickListener(this); menu2 = (LinearLayout)this.findViewById(R.id.menu_2); menu2.setOnClickListener(this); menu3 = (LinearLayout)this.findViewById(R.id.menu_3); menu3.setOnClickListener(this); fragmentManager = getFragmentManager(); /*fragmentManager.beginTransaction()获取FragmentTransaction对象 * hide(mFragment)隐藏当前的fragment * */ fragment1 = new Fragment1(); if (fragment1.isAdded()) { fragmentManager.beginTransaction().show(fragment1).hide(mFragment).commit(); } else { fragmentManager.beginTransaction().add(R.id.root, fragment1) .show(fragment1).commit(); } mFragment = fragment1; } @SuppressLint("NewApi") @Override public void onClick(View arg0) { // TODO Auto-generated method stub int mode = NORMAL_MODE; switch (arg0.getId()) { case R.id.menu_1: { mode = NORMAL_MODE; } break; case R.id.menu_2: { mode = SMARTHOME_MODE; } break; case R.id.menu_3: { mode = COMMUNITY_MODE; } break; } if (mMode == mode) return; mMode = mode; FragmentTransaction fragmentTransaction; fragmentTransaction = fragmentManager.beginTransaction(); if (mode == NORMAL_MODE) { if (fragment1 == null) { fragment1 = new Fragment1(); } if (fragment1.isAdded()) { fragmentTransaction.show(fragment1).hide(mFragment).commit(); } else { fragmentTransaction.add(R.id.root, fragment1) .show(fragment1).hide(mFragment).commit(); } mFragment = fragment1; } else if (mode == SMARTHOME_MODE) { if (fragment2 == null) { fragment2 = new Fragment2(); } if (fragment2.isAdded()) { fragmentTransaction.show(fragment2).hide(mFragment).commit(); } else { fragmentTransaction.add(R.id.root, fragment2) .show(fragment2).hide(mFragment).commit(); } mFragment = fragment2; } else if (mode == COMMUNITY_MODE) { if (fragment3 == null) { fragment3 = new Fragment3(); } if (fragment3.isAdded()) { fragmentTransaction.show(fragment3).hide(mFragment).commit(); } else { fragmentTransaction.add(R.id.root, fragment3) .show(fragment3).hide(mFragment).commit(); } mFragment = fragment3; } } }
注意:
同一个FragmentTransaction对象只能有一次commit(),否则提示“java.lang.IllegalStateException:commit already called”。
源码下载:
http://download.csdn.net/detail/thanksgining/8406745
时间: 2024-10-11 20:13:14