我在listView
内的三个tab
片段中有mainActivity
个public function getAdminQueryBuilder() {
return $this->createQueryBuilder('u')
->where('u.hasRole(:role)', true)
->setParameter('role', 'ROLE_ADMIN')
;
}
。我想在主要活动的底部有一个视图,它不会受到片段转换的影响,但是它内部的数据必须随着列表项的数据更改而更新。
我对这种沟通如何运作感到很困惑。如果这是一个明显的问题,我很抱歉。通过讽刺或任何方式我会感激任何帮助。
答案 0 :(得分:0)
让您的片段与主要活动进行通信非常简单。您需要做的就是让您的MainActivity实现一个接口,并从任何片段调用其方法,以通知主要活动的某些更改。
这将是您的MainActivity:
public class MainActivity extends Activity
implements ArticleActionListener{
public void onArticleAddedToBasket(Article article) {
// Increment the value on the bottom view's total TextView,
// with the price of the article added to basket
}
public void onArticleRemovedFromBasket(Article article) {
// Decrement the value on the bottom view's total TextView,
// with the price of the article removed from basket
}
}
这将是您的界面:
public interface ArticleActionListener {
public void onArticleAddedToBasket(Article article);
public void onArticleRemovedFromBasket(Article article);
}
这将是你的片段:
public class ArticlesListFragment extends ListFragment {
ArticleActionListener articleCallback;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
articleCallback = (ArticleActionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement ArticleActionListener");
}
}
}
现在,您可以在Fragments中的任何位置使用ArticleActionListener接口的方法,并在MainActivity中调用它们。
让我知道它是否适合你:D