今天我遇到一个问题,就是在activity中需要动态改变fragment中的控件,以前看到过可以在activity中得到fragment实例,然后修改控件;

今天实验了一个新的办法,通过新的办法,就是通过本地广播;

1.在Fragment中编写广播接收器

public class ShouYeFragment extends Fragment {

    private ListView listView;
    private List<BoWen> list=new ArrayList<BoWen>();
    private BoWenAdapter adapter;
    private WeiBoDataBase weiBoDataBase=WeiBoDataBase.getInstance(getContext());

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view=inflater.inflate(R.layout.shouye_view,container,false);

        listView= (ListView) view.findViewById(R.id.bowen_list);

        return view;
    }
    public List<BoWen> getBoWen(){
        List<BoWen> list1=new ArrayList<BoWen>();
        List<String> bPhone=weiBoDataBase.getBPhone(MainActivity.getYonghuming());
        for (String bphone: bPhone){
            for(BoWen boWen:weiBoDataBase.getBoWen(bphone)){
                list1.add(boWen);
            }
        }
        return list1;
    }

    public class ShouYeBroadCastReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            list=getBoWen();
            adapter=new BoWenAdapter(getContext(),R.layout.bowen_view,list);
            listView.setAdapter(adapter);
        }
    }
}

在activity中注册发送本地广播

public class MainActivity extends FragmentActivity {

    private ShouYeFragment shouYeFragment;
    private FragmentManager manager;
    private FragmentTransaction transaction;

    private LocalBroadcastManager broadcastManager;
    private ShouYeFragment.ShouYeBroadCastReceiver shouYeBroadCastReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_view);

        shouYeFragment=new ShouYeFragment();
        manager=getSupportFragmentManager();

        broadcastManager = LocalBroadcastManager.getInstance(this);
        shouYeBroadCastReceiver=shouYeFragment.new ShouYeBroadCastReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.intent.action.ShouYe_BROADCAST");
        broadcastManager.registerReceiver(shouYeBroadCastReceiver, intentFilter);

        transaction=manager.beginTransaction();
        transaction.add(R.id.zhuyaoneirong_layout,shouYeFragment);
        transaction.addToBackStack(null);
        transaction.commit();
        init();

    }
    private void init(){
                            Intent intent1=new Intent("android.intent.action.ShouYe_BROADCAST");
                            broadcastManager.sendBroadcast(intent1);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        broadcastManager.unregisterReceiver(shouYeBroadCastReceiver);
    }
}

  

acvity和fragment通过广播联系的更多相关文章

  1. Fragment的广播消息接收

    这种方式不用在配置文件加东西 广播注册,可以写在Activity(onCreate),也可以写在Fragment(onActivityCreated)里. LocalBroadcastManager ...

  2. acitivity 和fragment 通信,使用广播来传递信息的问题

    使用广播来传递信息时 如果 acitivity  给 太快给 fragment  发送广播,fragment 收不到 使用回调的方式来解决

  3. Service 广播 到Fragment

    //Fragment public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Sys ...

  4. Fragment和Activity之间通过广播的方式传递数据

    四大组件之间传递数据可以用广播,但是有次面试官说太重了,用eventbus代替.下面的广播传递数据方法仅当学习参考. 1.管理类 /** * 广播管理类:注册广播.注销广播.发送广播 * @autho ...

  5. 心情日记app总结 数据存储+服务+广播+listview+布局+fragment+intent+imagebutton+tabactivity+美工

    ---恢复内容开始--- 结果截图如下: 第一张图是程序主界面,主要是显示记事列表的一些个事件.旁边的侧拉框是自己登陆用的.可以设置密码.可以查看反馈与关于等信息. 点击第一张图片下方的图标,会显示不 ...

  6. Android Fragment间的广播消息接收

    这种方式不用在配置文件加东西,我比较喜欢. 广播注册,可以写在Activity(onCreate),也可以写在Fragment(onActivityCreated)里. LocalBroadcastM ...

  7. Fragment使用LocalBroadcastManager接收广播消息

    这种方式不用在配置文件加东西 变量声明 LocalBroadcastManager broadcastManager; IntentFilter intentFilter; BroadcastRece ...

  8. 用Broadcast广播在activity之间、fragment之间、activity和fragment之间相互传数据

    例如:A界面要收到B界面的更变信息 一.A界面注册广播 private static final String INTENT_BROADCAST = "android.intent.acti ...

  9. android Broadcast广播消息代码实现

    我用的是Fragment , 发送写在一个类中,接收写在另外一个类的内部类中.代码动态实现注册. 代码: myReceiver = new zcd.netanything.MyCar.myReceiv ...

随机推荐

  1. Windows Internal Database Service Pack 4 x64 Edition (KB2463332)安装失败

    系统是Windows Server 2008 R2,补丁Windows Internal Database Service Pack 4 x64 Edition (KB2463332)总是安装失败,W ...

  2. net面试题

    简述 private. protected. public. internal 修饰符的访问权限.答 . private :   私有成员, 在类的内部才可以访问.   protected : 保护成 ...

  3. WP8.1 C#代码 添加/获取Grid.ColumnDefinitions/RowDefinitions

    WP8.1: ColumnDefinitions和RowDefinitions的道理是相同的,语法顺序是一样的,只不过是将ColumnDefinitions换成RowDefinitions而已 获取并 ...

  4. ftp应用

    ftp的基本应用: 下载easyfzs ftp,仿真模拟ftp服务器. 类库: using System; using System.Collections.Generic; using System ...

  5. cursor or set-based

    标题可能和正文不太相符.我主要是记录工作中遇到使用游标的语句改成普通set-based operation,执行时间快了很多. 1.游标语句 declare @startDate dateTime d ...

  6. docker 组件(c/s)

    Docker 组件 1. docker client : docker的客户端 2. docker server : docker daemon的主要组成部分,接受用户通过docker client发 ...

  7. TPS04-J. 使用线程池时确保ThreadLocal变量每次都初始化

    线程池可以提供这种保障,一旦你的代码开始执行了,被分配来执行这个task的线程在执行完你的task之前不会做别的事情. 所以不用担心执行到一半被别的task改了 thread local 的变量. 由 ...

  8. lua table integer index 特性

    table.maxn (table) Returns the largest positive numerical index of the given table, or zero if the t ...

  9. H5音频和视频

    使用媒体标签最大的缺点在于缺少编解码器的支持一.浏览器支持性检测检测浏览器是否支持audio元素或video元素最简单的方式就是用脚本动态创建它,然后检测特定函数是否存在 var hasVideo=! ...

  10. Unity: Invalid serialized file version xxx Expected version: 5.3.4f1. Actual version: 5.3.5f1.

    Unity发布安卓项目,如果直接使用Unity打包APK一切Ok,导出Google项目 使用Idea打包 一进去直接Crash. 报错: 1978-2010/? E/Unity﹕ Invalid se ...