main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/left_fragment"
android:name="com.example.administrator.fragmenttest.Fragmenteft"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!--<fragment-->
<!--android:id="@+id/right_fragment"-->
<!--android:name="com.example.administrator.fragmenttest.FragmentRight"-->
<!--android:layout_width="0dp"-->
<!--android:layout_height="match_parent"-->
<!--android:layout_weight="1"/>-->
<FrameLayout
android:id="@+id/right_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
</FrameLayout>
</LinearLayout>
MainActivity.java文件
package com.example.administrator.fragmenttest;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);//为按钮注册一个点击事件
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.button :
replaceFragment(new AnotherRightFragment());//调用 replaceFragment(方法动态添加RightFragment这个碎片)
break;
}
}
//创建待添加的碎片实例
private void replaceFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();//获取FragmentManager,在活动中可以直接调用getSupportFragmentManager()方法得到
FragmentTransaction transaction = fragmentManager.beginTransaction();//开启一个事务,通过调用beginTransaction()方法开启
transaction.replace(R.id.right_layout,fragment);//向容器中添加或替换碎片 replace(需要传入容器的ID,代添加的碎片实例)
transaction.addToBackStack(null);
transaction.commit();//提交事务
}
}
another.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:background="#ffff00"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:text="This is another right fragment"/>
</LinearLayout>
AnotherRightFragment,java文件
package com.example.administrator.fragmenttest;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Administrator on 2017/3/20.
*/
public class AnotherRightFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.another_right_fragment,container,false);
return view;
}
}