注意的地方:
MainActivity要继承FragmentActivity
import android.support.v4.app.Fragment;
如果导入的是这个包:
一定要用getSupportFragmentManager()获取管理器
FragmentManager fm = getSupportFragmentManager();事务完成后一定要提交
FragmentTransaction tra = fm.beginTransaction();
tra.commit();
tra几个要用到的方法
tra.add(R.id.content,tab0);注意的是R.id.content是main_Activity.mxl中Framlayout的id
tra.hide();
tra.show();
Activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent"> <FrameLayout android:id="@+id/content" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="wrap_content"></FrameLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/btn0" android:text="btn0" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/btn1" android:text="btn1" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/btn2" android:text="btn2" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>
Tab0Fragment:
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A placeholder fragment containing a simple view.
*/
public class Tab0Fragment extends Fragment {
public Tab0Fragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.tab0, container, false);
}
}
MainActivity:
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.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
public class MainActivityextends FragmentActivity implements View.OnClickListener {
FrameLayout content;
Button btn0;
Button btn1;
Button btn2;
Fragment tab0;
Fragment tab1;
Fragment tab2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initEvent();
}
public void setSelect(int select) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction tra = fm.beginTransaction();
hideFragment(tra);
switch (select) {
case 0:
if (tab0 == null) {
tab0 = new Tab0Fragment();
tra.add(R.id.content, tab0);
} else {
tra.show(tab0);
}
break;
case 1:
if (tab1 == null) {
tab1 = new Tab1Fragment();
tra.add(R.id.content, tab1);
} else {
tra.show(tab1);
}
break;
case 2:
if (tab2 == null) {
tab2 = new Tab2Fragment();
tra.add(R.id.content, tab2);
} else {
tra.show(tab2);
}
break;
}
tra.commit();
}
private void hideFragment(FragmentTransaction transaction) {
if (tab0 != null) {
transaction.hide(tab0);
}
if (tab1 != null) {
transaction.hide(tab1);
}
if (tab2 != null) {
transaction.hide(tab2);
}
}
private void initView() {
content = (FrameLayout) findViewById(R.id.content);
btn0 = (Button) findViewById(R.id.btn0);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
}
private void initEvent() {
btn0.setOnClickListener(this);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn0:
setSelect(0);
break;
case R.id.btn1:
setSelect(1);
break;
case R.id.btn2:
setSelect(2);
break;
}
}
}