活动中获取碎片对象
- 只能获取活动绑定布局文件中的碎片
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fragmentManager = getFragmentManager();
// anotherright r = (anotherright)fragmentManager.findFragmentById(R.id.another_right_layout); //这个不能获取,因为another_right_layout布局不在这个活动绑定的layout.activity_main.xml文件中
right ri = (right)fragmentManager.findFragmentById(R.id.right_fragment);
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.right_fragment, r);
fragmentTransaction.commit();
}
});
网友评论