基于更新片段UI关活动的广播接收器 [英] update fragment ui based off of Activity's Broadcast Receiver

查看:139
本文介绍了基于更新片段UI关活动的广播接收器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先让我说,我经历了许多相关的片段上这样的问题读取。不过,我似乎无法找到的情况刚好和我一样。

我正在使用PageAdapter,每一页是一个片段myActivity。我还接收有关所述网络连接等的服务触发myActivity接收机的更新的服务。 myActivity需要更新FragmentPage1而是因为我用的是pageAdapter并在运行时创建我的片段,我不能'findFragmentbyId'等。我并不需要通过任何数据我只需要触发FragmentPage1类里面的功能。请参考下面code片段。

 公共类myActivity扩展FragmentActivity实现ViewPager.OnPageChangeListener {    FragmentManager FM = getSupportFragmentManager();
    mPagerAdapter =新PagerAdapter(FM);
    mPager.setAdapter(mPagerAdapter);
    mPager.setOnPageChangeListener(本);    //添加标签。使用动作条为3.0及以上,否则使用TabWidget
    最后的动作条酒吧= getActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);    bar.addTab(bar.newTab()
            .setText(R.string.fragment_page_1)
            .setTabListener(新ActionBarTabListener(mPager)));
    bar.addTab(bar.newTab()
            .setText(R.string.fragment_page_2)
            .setTabListener(新ActionBarTabListener(mPager)));     私人广播接收器广播接收器=新的广播接收器(){
            @覆盖
            公共无效的onReceive(上下文的背景下,意图意图){
                 如果(intent.getAction()。等于(InterfaceManager.BROADCAST_UPDATE_CONNECTION_STATS)){
                     updateFragmentPage2();
                 }否则如果(intent.getAction()。等于(InterfaceManager.BROADCAST_UPDATE_RULES)){
                     UpdateFragmentPage1();
                 }
            }
     };
}
公共类FragmentPage2扩展片段实现OnCheckedChangeListener,OnClickListener {
    公共无效UpdateFragmentPage2(){
        //更新列表视图
    }}


解决方案

根据您的code,这里是你可以很快做什么。

  INT的tabIndex = 0;
(。getActionBar()getTabAt(的tabIndex).getText()的toString())MyCustomFragment FRAG = getFragmentManager()findFragmentByTag;
frag.updateFragmentContent();

创建一个自定义的基础片段MyCustomFragment,并有一个抽象方法updateFragmentContent(),那么你只需要改变的选项卡索引并没有什么特殊的类型转换

请注意,上面是一个更清洁的方式来做到这一点。与您现有的code,你仍然可以有两个不同的类型转换,并调用两个独立的方法来更新相应片段。

希望这有助于。

First let me say I have read through many of the questions related to fragments on SO. However, I can't seem to find a situation quite the same as mine.

I have myActivity that is using the PageAdapter, each page being a fragment. I also have a service that receives updates about the network connections etc. The service triggers the receiver in myActivity. myActivity needs to update FragmentPage1 but because I am using a pageAdapter and creating my fragments at run time I cannot 'findFragmentbyId' etc. I do not need to pass any data I just need to trigger the function inside of the FragmentPage1 class. Please see code snippet below.

public class myActivity extends FragmentActivity implements ViewPager.OnPageChangeListener {

    FragmentManager fm = getSupportFragmentManager();
    mPagerAdapter = new PagerAdapter(fm);
    mPager.setAdapter(mPagerAdapter);
    mPager.setOnPageChangeListener(this);

    // add tabs. Use ActionBar for 3.0 and above, otherwise use TabWidget
    final ActionBar bar = getActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    bar.addTab(bar.newTab()
            .setText(R.string.fragment_page_1)
            .setTabListener(new ActionBarTabListener(mPager)));
    bar.addTab(bar.newTab()
            .setText(R.string.fragment_page_2)
            .setTabListener(new ActionBarTabListener(mPager)));

     private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive (Context context, Intent intent){
                 if(intent.getAction().equals(InterfaceManager.BROADCAST_UPDATE_CONNECTION_STATS)) { 
                     updateFragmentPage2();
                 } else if (intent.getAction().equals(InterfaceManager.BROADCAST_UPDATE_RULES)) {
                     UpdateFragmentPage1();
                 }
            }
     };
}


public class FragmentPage2 extends Fragment implements OnCheckedChangeListener, OnClickListener  {
    public void UpdateFragmentPage2() {
        //update list view
    }

}

解决方案

Based on your code, Here's what you can do quickly.

int tabIndex = 0;
MyCustomFragment frag = getFragmentManager().findFragmentByTag(getActionBar().getTabAt(tabIndex).getText().toString());
frag.updateFragmentContent();

Create a custom base fragment MyCustomFragment and have an abstract method updateFragmentContent(), then you'd just need to change the tab index and no special typecast

Please note, The above is a cleaner way to do it. With your existing code, you can still have two separate type cast and call two separate methods to update corresponding fragments.

Hope this helps.

这篇关于基于更新片段UI关活动的广播接收器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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