acvity和fragment通过广播联系_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > acvity和fragment通过广播联系

acvity和fragment通过广播联系

 2016/9/4 5:31:47  徐经欢  程序员俱乐部  我要评论(0)
  • 摘要:今天我遇到一个问题,就是在activity中需要动态改变fragment中的控件,以前看到过可以在activity中得到fragment实例,然后修改控件;今天实验了一个新的办法,通过新的办法,就是通过本地广播;1.在Fragment中编写广播接收器publicclassShouYeFragmentextendsFragment{privateListViewlistView;privateList<BoWen>list=newArrayList<BoWen>()
  • 标签:

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

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

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

class="brush:java;gutter:true;">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);
    }
}

  

  • 相关文章
发表评论
用户名: 匿名