Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> acvity和fragment通過廣播聯系,acvityfragment

acvity和fragment通過廣播聯系,acvityfragment

編輯:關於android開發

acvity和fragment通過廣播聯系,acvityfragment


今天我遇到一個問題,就是在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);
    }
}

  

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved