问题描述
我有默认的主从流动,这是创建新的项目时自动创建。我的问题是。当我添加一个按钮,细节的一面。有没有办法通过pressing该按钮来更新我的名单身边?换句话说,可以ItemDetailFragment和ItemListFragment沟通?
I have default Master-Detail flow, which was created automatically when creating new project. My question is. When I add a button to detail side. Is there a way to update my list side by pressing that button ? In other words, can ItemDetailFragment and ItemListFragment communicate ?
推荐答案
是刚刚经历了监听活动的通信。
Yes just communicate through the activity with a listener.
您的活动:
public class MyActivity extends FragmentActivity implements OnFragmentClickListener {
@Override
public void OnFragmentClick(int action, Object object) {
switch(action) {
}
}
}
监听器类:
public interface OnFragmentClickListener {
public void OnFragmentClick(int action, Object object);
}
您片段届时将有在code,以实现该接口下面的话:
Your fragments will then have following somewhere in code in order to implement the interface:
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentClickListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement listeners!");
}
}
那么你的碎片互相喜欢这个通信:碎裂 - >活动 - > fragmentB。你的活动可以直接打电话methodes的片段,而无需担心同步问题。
Then your fragments communicate with each other like this: fragmentA -> activity -> fragmentB. Your activity can call methodes directly on the fragments without worrying about synchronization problems.
这是片段的一个调用示例:
Example of a call from fragment a:
mListener.OnFragmentClick(GLOBAL_ACTION_KEY someObject);
活动再处理:
Activity then handle:
public class MyActivity extends FragmentActivity implements OnFragmentClickListener {
@Override
public void OnFragmentClick(int action, Object object) {
switch(action) {
case GLOBAL_ACTION_KEY:
// you call fragmentB.someMethod();
break;
}
}
}
这篇关于片段之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!