按子片段访问父片段 [英] access parent fragment by child fragment

查看:69
本文介绍了按子片段访问父片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的子片段,如何从该片段访问我的父片段,请帮助我修复它

this is my child fragment and how to access my parent fragment from this fragment please help me to fix it

public class Profilefragmant extends Fragment {
public Profilefragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.profile_layout, container, false);

    TextView VE = (TextView) rootView.findViewById(R.id.profile_view_enquiry);
    VE.setOnClickListener(new View.OnClickListener(){
        public void onClick (View view){
            Fragment fragment = null;

            fragment = new Enquiryfragment();
            replaceFragment(fragment);
        }
    });



    return rootView;
}
public void replaceFragment(Fragment someFragment) {
    android.support.v4.app.FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.containerView, someFragment);
    transaction.addToBackStack(null);
    transaction.commit();
}
}

这是我在运行项目时遇到的错误

this is my error while run the project

 java.lang.IllegalArgumentException: No view found for id 0x7f0c0099 (com.knrmarine.knr:id/containerView) for fragment EnquiryActivity{3754711 #2 id=0x7f0c0099} 

这是我的Enquiryfragment代码:

public class Enquiryfragmant extends Fragment {
public Enquiryfragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.enquiry_layout, container, false);
return rootView;}}

推荐答案

您可以通过以下方式获取父Fragment的引用:

You can get parent Fragment's reference by :

ParentFragment parentFrag = ((ParentFragment)ChildFragment.this.getParentFragment());
parentFrag.someMethod();

我在ViewPager中使用了它.如果您还有其他要求,则InterfaceEventBus是常规的两个.

I used this in my ViewPager. If you have other requirement, Interface or EventBus are two of the general ones.

这篇关于按子片段访问父片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆