无法从父视图调用Fragment的方法

时间:2015-12-06 05:50:12

标签: java android android-fragments android-activity mobile

我试图从其父视图中调用Fragment的公共方法,然后在Stack上跟踪thisthis和这些问题。但是,当我尝试从父视图中调用它时,我需要的方法是不可用的。

这是我的代码:

片段:

public class FirstTabView extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.activity_first_tab_view, container, false);
            return rootView;
    }

     //The method I need to call
     public void PostStuff(String caption){
     }
}

父视图:(其中包含主视图寻呼机)

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_view);

        pager = (ViewPager) findViewById(R.id.mainpager);
        mAdapter = new HomePagerAdapter(getSupportFragmentManager());
        pager.setAdapter(mAdapter);
        pager.setOffscreenPageLimit(4);

        //Getting the Fragment's Tag. o is the Pager's Fragment index. i.e.: The first fragment
        String fragment_tag = "android:switcher:" + pager.getId() + ":" + 0;
        FirstTabView fragment = (FirstTabView) getSupportFragmentManager().findFragmentByTag(fragment_tag);
        fragment.  <This is where I need to call the method and pass the parameter>   ();
        //The method however doesn't appear when I try to reference it
}

我是否从父视图中正确调用了Fragment的方法?我做错了什么?

修改

这是我用来推送碎片的适配器:

public class HomePagerAdapter extends FragmentPagerAdapter {

        public HomePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int index) {

            switch (index) {
                case 0:
                    return new FirstTabView();
                case 1:
                    return new SecondTabView();
                case 2:
                    return new ThirdTabView();
                case 3:
                    return new FourthTabView();
                case 4:
                    return new FifthTabView();
            }

            return null;
        }

        @Override
        public int getCount() {
            // get item count - equal to number of tabs
            return 5;
        }

    }

1 个答案:

答案 0 :(得分:2)

您可以将FirstTabView的引用保存到MainActivity,然后调用片段的方法。

FirstTabView

public class FirstTabView extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.activity_first_tab_view, container, false);
            return rootView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        //set this fragment to your MainActivity
        MainActivity mainActivity = (MainActivity)getActivity();
        mainActivity.setFirstTabView(this);
    }


     //The method I need to call
     public void PostStuff(String caption){

     }
}

<强> MainActivity

private FirstTabView firstTabView;
@Override
public void onCreate(Bundle bundle) {
    ........
    if(firstTabView != null) {
        firstTabView.PostStuff("Hello World");
    }
}

public void setFirstTabView(FirstTabView firstTabView){
    this.firstTabView = firstTabView
}