核心内容
FragmentManager fm = getFragmentManager(); FragmentTransaction transaction = fm.beginTransaction(); mWeixin = new FragmentContent(); transaction.replace(R.id.activity_content, mWeixin); // transaction.addToBackStack(null);//此时可以返回 transaction.commit();
先来看下activity_main布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.devin.fragment.MainActivity"> <fragment android:layout_width="match_parent" android:layout_height="45dp" android:name="com.example.devin.view.FragmentTitle" android:id="@+id/activity_title"/> <FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/activity_content" android:layout_below="@+id/activity_title" android:layout_above="@+id/activity_bottom"/> <include android:id="@+id/activity_bottom" android:layout_width="fill_parent" android:layout_height="47dp" layout="@layout/bottom" android:layout_alignParentBottom="true" android:layout_alignParentStart="true" /> </RelativeLayout>
public class FragmentContent extends Fragment { private TextView textView = null ; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // return super.onCreateView(inflater, container, savedInstanceState); View view = inflater.inflate(R.layout.fragment_content, container, false); textView = (TextView) view.findViewById(R.id.TvContent); textView.setText("我们是内容"); return view; } }主要的Javapublic class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button mTabWeixin; private Button mTabFriend; private FragmentContent mWeixin; //FragmentContent private FragmentFriend mFriend; //FragmentFriend @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 初始化控件和声明事件 mTabWeixin = (Button) findViewById(R.id.button1); mTabFriend = (Button) findViewById(R.id.button2); mTabWeixin.setOnClickListener(this); mTabFriend.setOnClickListener(this); setDefaultFragment();// 设置默认的Fragment } private void setDefaultFragment() { FragmentManager fm = getFragmentManager(); FragmentTransaction transaction = fm.beginTransaction(); mWeixin = new FragmentContent(); transaction.replace(R.id.activity_content, mWeixin); // transaction.addToBackStack(null);//此时可以返回 transaction.commit(); } @Override public void onClick(View v) { FragmentManager fm = getFragmentManager(); FragmentTransaction transaction = fm.beginTransaction();// 开启Fragment事务 switch (v.getId()){ case R.id.button1: if (mWeixin == null) { mWeixin = new FragmentContent(); } // 使用当前Fragment的布局替代id_content的控件 transaction.replace(R.id.activity_content, mWeixin); break; case R.id.button2: if (mFriend == null) { mFriend = new FragmentFriend(); } transaction.replace(R.id.activity_content, mFriend); break; case R.id.button3: break; } transaction.addToBackStack(null);//此时可以返回 transaction.commit(); // 事务提交 }