美文网首页
[code]_[fragment interact activi

[code]_[fragment interact activi

作者: 勤学奋进小郎君 | 来源:发表于2018-09-20 00:22 被阅读0次

    活动中获取碎片对象

    • 只能获取活动绑定布局文件中的碎片
        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();
                }
            });
    

    相关文章

      网友评论

          本文标题:[code]_[fragment interact activi

          本文链接:https://www.haomeiwen.com/subject/newnwftx.html