在我的MainActivity class
我有一个包含3个标签fragmentHome class
,fragmentHot class
和FragmentCategoryclass
以下是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
它跑了。请把你的想法或代码告诉我。
答案 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);
}
}