Android Fragment 实现页面跳转

来源:互联网 发布:php print 编辑:程序博客网 时间:2024/04/30 15:57

首先要定义好要切换的容器布局文件(就是在哪个布局文件里进行切换)

activity_frame.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" >    <android.support.v4.widget.DrawerLayout        android:id="@+id/layout_menu"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="#F0F0F7" >        <!-- 主界面 -->        <FrameLayout            android:layout_width="fill_parent"            android:layout_height="fill_parent" >            <!-- 内容部分 -->            <RelativeLayout                android:id="@+id/layout_bodys"                android:layout_width="fill_parent"                android:layout_height="fill_parent" >            </RelativeLayout>            <!-- 标题栏 -->            <LinearLayout                android:id="@+id/layout_title"                android:layout_width="match_parent"                android:layout_height="50dp"                android:background="#C0C0C7"                android:orientation="horizontal"                android:padding="10dp" >                <Button                    android:id="@+id/btn"                    android:layout_width="27dp"                    android:layout_height="16dp"                    android:background="@drawable/menu" />                <TextView                    android:id="@+id/title"                    android:layout_width="fill_parent"                    android:layout_height="wrap_content"                    android:gravity="center_vertical|center_horizontal"                    android:paddingRight="27dp"                    android:text="@string/title"                    android:textColor="#000000"                    android:textSize="22dip" />            </LinearLayout>        </FrameLayout>        <!-- 菜单 -->        <ListView            android:id="@+id/left_menu"            android:layout_width="270dip"            android:layout_height="match_parent"            android:layout_gravity="start"            android:background="#23242327"            android:choiceMode="singleChoice"            android:divider="@android:color/transparent"            android:dividerHeight="0dp"            android:paddingTop="75dip" />    </android.support.v4.widget.DrawerLayout></RelativeLayout>


然后就是定义要切换的布局文件

fragment_home.xml 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/layout_home"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#ff0"    android:paddingTop="50dp" >    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:background="#00FF00"        android:gravity="center_vertical|center_horizontal"        android:text="首页"        android:textColor="#000000"        android:textSize="18dip" /></RelativeLayout>


fragment_setup.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/layout_home"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#ff0"    android:paddingTop="50dp" >    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:background="#00FF00"        android:gravity="center_vertical|center_horizontal"        android:text="设置"        android:textColor="#000000"        android:textSize="18dip" /></RelativeLayout>


接下来要有两个继承Fragment的类,分别绑定上面两个切换内容的布局文件


FragmentHome.java


package com.example.challenge.view;import com.example.challenge.R;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class FragmentHome extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {return inflater.inflate(R.layout.fragment_home, null);}}

FragmentSetup.java


package com.example.challenge.view;import com.example.challenge.R;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class FragmentSetup extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {return inflater.inflate(R.layout.fragment_setup, null);}}

接下来定义Activity绑定容器布局文件
package com.example.challenge.view;import java.util.ArrayList;import java.util.HashMap;import android.app.Activity;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.os.Bundle;import android.support.v4.widget.DrawerLayout;import android.view.ContextMenu;import android.view.ContextMenu.ContextMenuInfo;import android.view.Gravity;import android.view.KeyEvent;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.view.View.OnCreateContextMenuListener;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.Button;import android.widget.ListView;import android.widget.RelativeLayout;import android.widget.SimpleAdapter;import android.widget.TextView;import com.example.challenge.R;public class ActivityMain extends Activity {private DrawerLayout menu = null;private RelativeLayout bodys = null;private FragmentManager fm = null;private FragmentTransaction ft = null;private TextView title = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_frame);title = (TextView) findViewById(R.id.title);Button button = (Button) findViewById(R.id.btn);title.setText("首页");menu = (DrawerLayout) findViewById(R.id.layout_menu);bodys = (RelativeLayout) findViewById(R.id.layout_bodys);fm = getFragmentManager();ft = fm.beginTransaction();ft.replace(R.id.layout_bodys, new FragmentHome());ft.commit();button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// 按钮按下,将抽屉打开menu.openDrawer(Gravity.LEFT);}});// 绑定Layout里面的ListViewListView list = (ListView) findViewById(R.id.left_menu);// 生成动态数组,加入数据ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();for (int i = 0; i < 2; i++) {HashMap<String, Object> map = new HashMap<String, Object>();switch (i) {case 0: {map.put("ItemImage", R.drawable.homepage);// 图像资源的IDmap.put("ItemTitle", "首页");map.put("ItemText", "Finished in 1 Min 54 Secs, 70 Moves! ");break;}case 1: {map.put("ItemImage", R.drawable.setup);// 图像资源的IDmap.put("ItemTitle", "设置");map.put("ItemText", "Finished in 1 Min 54 Secs, 70 Moves! ");break;}}listItem.add(map);}// 生成适配器的Item和动态数组对应的元素SimpleAdapter listItemAdapter = new SimpleAdapter(this, listItem,// 数据源R.layout.activity_menu_item,// ListItem的XML实现// 动态数组与ImageItem对应的子项new String[] { "ItemImage", "ItemTitle", "ItemText" },// ImageItem的XML文件里面的一个ImageView,两个TextView IDnew int[] { R.id.ItemImage, R.id.ItemTitle });// 添加并且显示list.setAdapter(listItemAdapter);// 添加点击list.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {menu.closeDrawers();ft = fm.beginTransaction();switch (arg2) {case 0: {title.setText("首页");ft.replace(R.id.layout_bodys, new FragmentHome());break;}case 1: {title.setText("设置");ft.replace(R.id.layout_bodys, new FragmentSetup());break;}}ft.commit();}});// 添加长按点击list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {@Overridepublic void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {// menu.setHeaderTitle("长按菜单-ContextMenu"); menu.add(0, 0, 0,// "弹出长按菜单0"); menu.add(0, 1, 0, "弹出长按菜单1");// Toast.makeText(getApplicationContext(), "长按事件被触发", 0).show();}});}// 长按菜单响应函数@Overridepublic boolean onContextItemSelected(MenuItem item) {setTitle("点击了长按菜单里面的第" + item.getItemId() + "个项目");return super.onContextItemSelected(item);}@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {if (keyCode == KeyEvent.KEYCODE_MENU) {// menu键按下,将抽屉打开menu.openDrawer(Gravity.LEFT);}return super.onKeyDown(keyCode, event);}}


AndroidManifest.xml 改成高版本


<uses-sdk        android:minSdkVersion="11"        android:targetSdkVersion="18" />



注:

FragmentManager 用来管理 Fragment 类的要从容器Acivity中获取  FragmentManager fm = getFragmentManager();

FragmentTransaction 是由管理者指派真正干活的 FragmentTransaction ft = fm.beginTransaction();

ft.replace(R.id.layout_bodys, new FragmentHome()); // 这句的意思是把容器中的layout_bodys 视图 替换为刚刚定义的 FragmentHome 试图

ft.commit(); // 提交上面所做的替换动作 

0 0