这种实现方法主要是
1.在布局中使用FrameLayout
2.不同的页面继承Fragment父类,重写onCreateView()方法,在此方法中定义需要返回的View对象
3.使用Fragment Manager去管理
主页面布局:
<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: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">
<FrameLayout
android:id="@+id/fl_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<include layout="@layout/bottom_menu" />
</LinearLayout>
主页面代码:
package com.example.baidu.mydemos.demo1;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.RadioGroup;
import com.example.baidu.mydemos.R;
/**
* Created by baidu on 15/12/30.
*/
public class DemoOneMenuTwoActivity extends Activity implements RadioGroup.OnCheckedChangeListener {
private FrameLayout fl_content;
private RadioGroup rg_bottom;
private FragmentManager fragmentManager;
//tab views
private AppFragmentOneContent appFragmentOneContent;
private AppFragmentTwoContent appFragmentTwoContent;
private AppFragmentThreeContent appFragmentThreeContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo1_2);
fragmentManager = getFragmentManager();
rg_bottom = (RadioGroup) findViewById(R.id.rg_bottom);
rg_bottom.setOnCheckedChangeListener(this);
initData();
}
public void initData() {
setTabContent(0);
rg_bottom.check(R.id.rb_1);
}
public void setTabContent(int index) {
FragmentTransaction transaction = fragmentManager.beginTransaction();
switch (index) {
case 0:
if (appFragmentOneContent == null) {
appFragmentOneContent = new AppFragmentOneContent();
}
transaction.replace(R.id.fl_content, appFragmentOneContent, "fragmentone");
break;
case 1:
if (appFragmentTwoContent == null) {
appFragmentTwoContent = new AppFragmentTwoContent();
}
transaction.replace(R.id.fl_content, appFragmentTwoContent, "fragmenttwo");
break;
case 2:
if (appFragmentThreeContent == null) {
appFragmentThreeContent = new AppFragmentThreeContent();
}
transaction.replace(R.id.fl_content, appFragmentThreeContent, "fragmentthree");
}
transaction.commit();
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.rb_1:
setTabContent(0);
break;
case R.id.rb_2:
setTabContent(1);
break;
case R.id.rb_3:
setTabContent(2);
break;
}
}
}
Fragment所使用的布局文件:
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:text="content"/>
</LinearLayout>
package com.example.baidu.mydemos.demo1;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.baidu.mydemos.R;
/**
* Created by baidu on 15/12/30.
*/
public class FragmentOneContent extends Fragment {
public FragmentOneContent() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.demo1_content, null);
TextView tv_content = (TextView) view.findViewById(R.id.tv_content);
tv_content.setText("content 1");
return view;
}
}