平板的碎片添加步骤

一    要新建三个布局的实例一左,一右,一个替换的  主活动的布局中嵌入碎片

二   新建三个类与三个布局配合(继承Fragment)

public class LeftFragement extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view=inflater.inflate(R.layout.left_fragement,container,false);
return view;
}
}
三 主活动中的方法实现

1.建立待添加的碎片实例     AnotherRightFragment()

2.获取FragmentManger,在活动中可以通过调用getSupportFragmentManager()方法获得   getSupportFragmentManager()

3开启一个食物,通过调用beginTransaction()方法开启    fragmentManager.beginTransaction()

4.向容器内添加或者替换碎片,一般使用replace()方法实现,传入容器的Id 和需要添加的碎片实例    transaction.replace(R.id.right_layout,fragment);

5.提交食物,调用commit()方法实现    transaction.commit();

 

private void replaceFragment(Fragment fragment)
{
FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction transaction=fragmentManager.beginTransaction();
transaction.replace(R.id.right_layout,fragment);
  transaction.addToBackStack(null);放入返回栈中,按下返回键后可以返回上一步的碎片
transaction.commit();
}


posted @ 2018-11-29 15:48  拎着红杯子的黄鸭子  Views(208)  Comments(0Edit  收藏  举报