MainActivity.java:java
package com.example.fragment;android
import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;app
public class MainActivity extends Activity implements OnClickListener {ide
LinearLayout context;
TextView tv1, tv2, tv3,tv4;
FragmentManager fm;
FragmentTransaction ft;
Fragment f1,f2,f3,f4;this
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);get
tv1 = (TextView) findViewById(R.id.tv1);
tv2 = (TextView) findViewById(R.id.tv2);
tv3 = (TextView) findViewById(R.id.tv3);
tv4=(TextView) findViewById(R.id.tv4);
context=(LinearLayout) findViewById(R.id.context);
f1=new Fragment1();
f2=new Fragment2();
f3=new Fragment3();
f4=new Fragment4();it
tv1.setOnClickListener(this);
tv2.setOnClickListener(this);
tv3.setOnClickListener(this);
tv4.setOnClickListener(this);
fm=getFragmentManager();
ft=fm.beginTransaction();
//默認界面
ft.replace(R.id.context, f1);
ft.commit();
}io
@Override
public void onClick(View arg0) {
ft=fm.beginTransaction();
switch (arg0.getId()) {
case R.id.tv1:
ft.replace(R.id.context, f1);
break;class
case R.id.tv2:
ft.replace(R.id.context, f2);
break;
case R.id.tv3:
ft.replace(R.id.context, f3);
break;
case R.id.tv4:
ft.replace(R.id.context, f4);
}
ft.commit();
}
}
import