MainActivity
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);
replaceFragment(new RightFragment());//用replaceFragment()加载碎片
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button:
replaceFragment(new RightFragment2());
break;
default:
break;
}
}
private void replaceFragment(Fragment fragment) {//加载碎片
FragmentManager fragmentManager = getSupportFragmentManager();//
FragmentTransaction transaction = fragmentManager.beginTransaction();//开启一个事务
transaction.replace(R.id.right_layout,fragment);//向容器添加碎片
transaction.addToBackStack(null);//将事务加入返回栈
transaction.commit();//提交事务
}
}
LeftFragment
public class LeftFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savefInstanceState){
View view = inflater.inflate(R.layout.left_fragment,container,false);
return view;
}
}
RightFragment
public class RightFragment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savefInstanceState){
View view = inflater.inflate(R.layout.right_fragment_2,container,false);
return view;
}
}
activity_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.ldxiaofeng.android.fragmenttest.LeftFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<!--fragment
android:id="@+id/right_fragment"
android:name="com.ldxiaofeng.android.fragmenttest.RightFragment"
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>
left_fragment.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:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Button"/>
</LinearLayout>
right_fragment.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:layout_height="match_parent"
android:background="#00ff00">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="这是右碎片"
android:textSize="26sp"/>
</LinearLayout>