demo源码下载:http://download.csdn.net/detail/u010778159/8643203
fragment间的切换
- 工程有3个.java文件,4个xml文件
我们要实现的是:fragment_content2中有一个Button,而fragment
_content1中有一个TextView, 一开始MainActivity.java显示fragment_content2的内容,当点击Button之后,切换成fragment_content1的内容。
fragment_content1.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:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:layout_gravity="center"
android:text="这是fragment1" />
</LinearLayout>
fragment_content2.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/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="这是fragment2" />
</LinearLayout>
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" >
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.LZP.fragmenttest.MainActivity"
tools:ignore="MergeRootFrame" >
</FrameLayout>
</RelativeLayout>
可以看到activity_main.xml中有一个FrameLayout,这个就是fragment的容器
ContentFragment.java:
package com.LZP.fragment_change;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class ContentFragment extends Fragment {
private Button send2;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fragment_content1, container, false);
return view;
}
}
ContentFragment2.java:
package com.LZP.fragment_change;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class ContentFragment2 extends Fragment {
private Button button;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fragment_content2, container, false);
button=(Button)view.findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
//按钮点击响应事件
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
FragmentManager fm = getActivity().getSupportFragmentManager();
ContentFragment fragment=new ContentFragment();
android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
//container是activity_main.xml中的FrameLayout的ID,用来放置Fragment
ft.replace(R.id.container, fragment);
ft.commit();
}
});
return view;
}
}
MainActivity.java:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
ContentFragment2 fragment=new ContentFragment2();
android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.container, fragment);
ft.commit();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
时间: 2024-11-07 10:10:35