ViewPager活动通知特定事件的片段 [英] ViewPager Activity to notify a Fragment of a specific event

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

问题描述

我有一个 ViewPager ,这是为了显示同样的几个片段使用 FragmentAdapter 。虽然这些片段是由同一类基本上实例,他们使用的是的ListView 来显示不同​​的信息。 (显然,的ListView 正在poulated由 ArrayAdapter )。
后台服务也在运行,并不断从互联网上接收数据。我希望能够更新特定片段 ViewPager 时,我的后台服务已通知特定事件的我。
我该怎么办呢? 一个code段将是巨大的AP preciated!

I have a ViewPager and it is using a FragmentAdapter in order to display several fragments of the same kind. Although these Fragments are basically instantiated from the same class, they are using a ListView to display different information. (Obviously the ListView is being poulated by an ArrayAdapter.)
A background service is also running and is constantly receiving data from the Internet. I want to be able to update a specific Fragment in the ViewPager when my background service has notified me of a specific event.
How can I do that? A code snippet would be hugely appreciated!

(顺便说一句,我有锯<一个href="http://stackoverflow.com/questions/6101958/how-should-a-fragment-get-notified-about-the-result-of-an-asynchronous-task">this类似的问题,但我不知道如何利用自己的建议!)

(By the way, I have saw this similar question but I have no idea how to use their suggestion!)

为使这一切更简单: 我的活动与ViewPager:
[片段0] [片段1] [片段2]

To make it all more simple: My activity with the ViewPager:
[Fragment 0] [Fragment 1] [Fragment 2]

后台服务告诉我(通过广播)来更新的ListView 片段1

The background service tells me (via a broadcast) to update the ListView in Fragment 1.

编辑: 这里有样本codeS:

Here are sample codes:

public class ChatWindowPager extends FragmentActivity
{
    private ViewPager mViewPager = null;
    private ChatFragmentAdapter mAdapter = null;
    @Override
    protected void onCreate(final Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chat_window_pager);

        this.mViewPager = (ViewPager) findViewById(R.id.chatPager);
        this.mAdapter = new ChatFragmentAdapter(getSupportFragmentManager());
        this.mViewPager.setAdapter(this.mAdapter);
        .
        .
        .
    }

    class ChatFragmentAdapter extends FragmentPagerAdapter implements ViewProvider
    {

        public ChatFragmentAdapter(final FragmentManager fm)
        {
            super(fm);
        }

        @Override
        public Fragment getItem(final int arg0)
        {
            String friendId = ..... // Some initializations
            ChatWindowFragment f = ChatWindowFragment.newInstance(friendId);
            return f;
        }

        @Override
        public int getCount()
        {
            ...
        }

        @Override
        public View getView(final int position)
        {
            View v = getLayoutInflater().inflate(R.layout.tab_holder, null);
            .
            .
            .
            return v;
        }
    }
}

现在的片段是这样定义的:

Now the fragments is defined like this:

public class ChatWindowFragment extends Fragment
{
    public String friendId;
    private ListView lv;

    public static ChatWindowFragment newInstance(final String friendId)
    {
        ChatWindowFragment chatWindowFragment = new ChatWindowFragment();
        Bundle bundle = new Bundle();
        bundle.putString("friendId", friendId);
        chatWindowFragment.setArguments(bundle);
        return chatWindowFragment;
    }

    @Override
    public void onCreate(final Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.friendId = getArguments().getString("friendId");
    }

    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState)
    {
        View v = inflater.inflate(R.layout.chat_window, container, false);

        this.friendId = getArguments().getString("friendId");
        .
        .
        .
        return v;
    }

    //The rest of the class

}

由于我使用的是 FragmentPagerAdapter 我不明白我怎么可以设置每个片段的标签! (很明显,我不使用事务添加的片段!)

As I am using a FragmentPagerAdapter I don't see how I can set the tag of each fragment! (Obviously, I am not using transactions to add the Fragments!)

编辑2: 我想知道是否我在做什么,是正确的方法来处理我想做的事......任何其他解决方案也欢迎!

EDIT 2: I would like to know whether what I'm doing, is the correct way to handle what I want to do... Any other solution is also welcome!

推荐答案

试试这个,

注册的所有片段的广播接收器......像这样

Register a broadcast receiver in all your fragments... like this

创建一个类延伸广播接收器中的所有类的如:

create a class which extends a broadcast receiver in all the classes, for eg:

public class FragmentReceiver1 extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
        }    
}

在你片段的onCreate ...

and register this receiver in you fragment's onCreate ...

有关,例如。 getActivity()registerReceiver(新FragmentReceiver1(),新的IntentFilter(fragmentupdater));

现在片段像1片段1,2 Fragment2同样地分配一个唯一的ID给每个你

Now assign a unique id to each of you fragment like 1 for Fragment1, 2 for Fragment2 and likewise

现在,每当你想传递的任何数据,并更新任何片段只是发送与意向中的数据广播和fragmentupdater作为意图过滤器...

now whenever you want to pass any data and update any of the fragment just send a broadcast with the data in intent and "fragmentupdater" as the intent-filter...

有关,例如:

Intent data = new Intent("fragmentupdater");
data.putString("key","data");
data.putInt("fragmentno",1); // Pass the unique id of fragment we talked abt earlier
activity.sendBroadcast(intent);

现在您的每一个片段都将接收到的数据,但你可以验证数据是否为同一片段由我们通过它的的onReceive 函数的唯一ID。 ..,你得到的意图,是我们通过上面

Now each of your fragment will receive the data but you can verify if the data if for the same fragment by the unique id we passed in it in the onReceive function..., the intent which you get, is the intent we passed above

这篇关于ViewPager活动通知特定事件的片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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