访问片段活动? [英] Access Fragment in Activity?

查看:107
本文介绍了访问片段活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2片段一个主活动。这两个片段具有的ListView 。我想更新的列表中 MainActivity 。有没有什么办法来访问片段列表适配器活动与出使适配器,静态适配器。 目前,我做这样的 Mainactivity.java

 公共无效updatelist(){
  如果(fragmentstate = 0)
    Fragment1.adapter.notifyDataSetChanged();
  其他
    Fragment2.adapter.notifyDataSetChanged();

}
 

解决方案

如果你只是想更新的片段列表,您不必访问adpaters。你可以在片段注册本地播放,并从MainActivity发送本地广播消息。

从frgments,

  LocalBroadcastManager.getInstance(的getContext())registerReceiver(...)。
 

从MainActivity,

  LocalBroadcastManager.getInstance(的getContext())。sendBroadcast(...)
 

I have one main activity with 2 fragments. Both fragments have a ListView. I want to update the list in MainActivity. Is there any way to access fragment list adapter in activity with out making adapter as static adapter. Currently I am doing like this in Mainactivity.java

public void updatelist() {
  if(fragmentstate=0)
    Fragment1.adapter.notifyDataSetChanged();
  else
    Fragment2.adapter.notifyDataSetChanged();

}

解决方案

If you just want to update lists on fragments, you don't have to access the adpaters. You can register local broadcast on fragments, and send local broadcast message from MainActivity.

From frgments,

LocalBroadcastManager.getInstance(getContext()).registerReceiver(...);

From MainActivity,

LocalBroadcastManager.getInstance(getContext()).sendBroadcast(...)

这篇关于访问片段活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆