从活动中调用父片段中的子片段的方法

时间:2016-07-15 03:00:11

标签: android android-fragments fragment

在我的MainActivity class我有一个包含3个标签fragmentHome classfragmentHot classFragmentCategoryclass

的查看寻呼机

以下是fragmentCategory class

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
  View view;
  view = inflater.inflate(R.layout.fragmentcategory, container, false);
  FragmentTransaction transaction = getFragmentManager().beginTransaction();
  transaction.replace(R.id.category_fragment, new TabCategoryFragment());
  transaction.commit();
  return view;
}

TabCategoryFragment class中,我有网格类别新闻,当点击项目网格时,我调用了:

FragmentTransaction transaction = getFragmentManager().beginTransaction();
Bundle bundle = new Bundle();
bundle.putString("category", arrDataZone.get(position).getZone_name());
bundle.putString("zoneid", arrDataZone.get(position).getZone_id());
ListCategoryFragment myFrag = new ListCategoryFragment();
myFrag.setArguments(bundle);
transaction.replace(R.id.category_fragment, myFrag,"listFramgnet");
transaction.addToBackStack("grid_category");
transaction.commit();

现在,我希望来自ListCategoryFragment的{​​{1}}中的呼叫方法或来自MainActivity的{​​{1}}中的呼叫方法。

因为我尝试使用ListCategoryFragment中的FragmentCategory来调用方法:

FragmentCategory

它跑了。请把你的想法或代码告诉我。

enter image description here

1 个答案:

答案 0 :(得分:1)

你可以使用Eventbus postSticky来解决它,你可以看到api,eventbus

我写了一个演示模拟你的案例。这是代码。 MainActivity:

 public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main22);
        getSupportFragmentManager().beginTransaction().replace(R.id.contaner,new MainActivityFragment()).commit();
        EventBus.getDefault().register(this);
    }

    @Subscribe
    public void changeFragment(ChangeFragmentEvent event) {
        // get event from MainActivityFragment immediately--->EventBus.getDefault().post(new ChangeFragmentEvent());
        getSupportFragmentManager().beginTransaction().replace(R.id.contaner,PlusOneFragment.newInstance()).commit();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}

MainActivityFragment:

    public class MainActivityFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_main22, container, false);
    }
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        View button = view.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new ChangeFragmentEvent());//notify MainActivity change fragment
                EventBus.getDefault().postSticky(new MEvent());//send a msg to PlusOneFragment,when it shown 
            }
        });
    }
}

PlusOneFragment:

    public class PlusOneFragment extends Fragment {

    public PlusOneFragment() {
    }

    public static PlusOneFragment newInstance() {
        PlusOneFragment fragment = new PlusOneFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

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

    @Subscribe(sticky = true)
    public void getMEvent(MEvent event){
        // get event from MainActivityFragment when PlusOneFragment show --->EventBus.getDefault().postSticky(new MEvent());
        Toast.makeText(getActivity(),"Hello,there",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDetach() {
        super.onDetach();
        EventBus.getDefault().unregister(this);
    }

}