<small id='Sy5XV'></small><noframes id='Sy5XV'>

    1. <i id='Sy5XV'><tr id='Sy5XV'><dt id='Sy5XV'><q id='Sy5XV'><span id='Sy5XV'><b id='Sy5XV'><form id='Sy5XV'><ins id='Sy5XV'></ins><ul id='Sy5XV'></ul><sub id='Sy5XV'></sub></form><legend id='Sy5XV'></legend><bdo id='Sy5XV'><pre id='Sy5XV'><center id='Sy5XV'></center></pre></bdo></b><th id='Sy5XV'></th></span></q></dt></tr></i><div id='Sy5XV'><tfoot id='Sy5XV'></tfoot><dl id='Sy5XV'><fieldset id='Sy5XV'></fieldset></dl></div>

        <bdo id='Sy5XV'></bdo><ul id='Sy5XV'></ul>
    2. <legend id='Sy5XV'><style id='Sy5XV'><dir id='Sy5XV'><q id='Sy5XV'></q></dir></style></legend>
      <tfoot id='Sy5XV'></tfoot>

        ViewPager Activity 通知特定事件的 Fragment

        时间:2023-10-04
        <i id='1zbQy'><tr id='1zbQy'><dt id='1zbQy'><q id='1zbQy'><span id='1zbQy'><b id='1zbQy'><form id='1zbQy'><ins id='1zbQy'></ins><ul id='1zbQy'></ul><sub id='1zbQy'></sub></form><legend id='1zbQy'></legend><bdo id='1zbQy'><pre id='1zbQy'><center id='1zbQy'></center></pre></bdo></b><th id='1zbQy'></th></span></q></dt></tr></i><div id='1zbQy'><tfoot id='1zbQy'></tfoot><dl id='1zbQy'><fieldset id='1zbQy'></fieldset></dl></div>
        <legend id='1zbQy'><style id='1zbQy'><dir id='1zbQy'><q id='1zbQy'></q></dir></style></legend>
        1. <small id='1zbQy'></small><noframes id='1zbQy'>

        2. <tfoot id='1zbQy'></tfoot>
              <tbody id='1zbQy'></tbody>
              <bdo id='1zbQy'></bdo><ul id='1zbQy'></ul>

                  本文介绍了ViewPager Activity 通知特定事件的 Fragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个 ViewPager,它使用 FragmentAdapter 来显示多个相同类型的片段.虽然这些 Fragment 基本上都是从同一个类中实例化出来的,但是它们使用一个 ListView 来显示不同的信息.(显然 ListView 是由 ArrayAdapter 填充的.)
                  后台服务也在运行,并不断从 Internet 接收数据.当我的后台服务通知我特定事件时,我希望能够更新 ViewPager 中的特定片段.
                  我怎样才能做到这一点?非常感谢您提供代码片段!

                  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!

                  (顺便说一句,我看过 这个类似的问题,但我不知道如何使用他们的建议!)

                  (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]

                  后台服务告诉我(通过广播)更新 Fragment 1 中的 ListView.

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

                  以下是示例代码:

                  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(new FragmentReceiver1(), new IntentFilter("fragmentupdater"));

                  现在为每个片段分配一个唯一的 id,例如 1 用于 Fragment1,2 用于 Fragment2 等等

                  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(data);
                  

                  现在您的每个片段都将接收数据,但您可以通过我们在 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 Activity 通知特定事件的 Fragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:查看寻呼机,上一个和下一个项目的尺寸更小,无限滚动 下一篇:仅在第二次导航到 Fragment 时导致 java.IllegalStateException 错误,No Activ

                  相关文章

                    <small id='lAnKV'></small><noframes id='lAnKV'>

                      <legend id='lAnKV'><style id='lAnKV'><dir id='lAnKV'><q id='lAnKV'></q></dir></style></legend>
                      <i id='lAnKV'><tr id='lAnKV'><dt id='lAnKV'><q id='lAnKV'><span id='lAnKV'><b id='lAnKV'><form id='lAnKV'><ins id='lAnKV'></ins><ul id='lAnKV'></ul><sub id='lAnKV'></sub></form><legend id='lAnKV'></legend><bdo id='lAnKV'><pre id='lAnKV'><center id='lAnKV'></center></pre></bdo></b><th id='lAnKV'></th></span></q></dt></tr></i><div id='lAnKV'><tfoot id='lAnKV'></tfoot><dl id='lAnKV'><fieldset id='lAnKV'></fieldset></dl></div>

                      <tfoot id='lAnKV'></tfoot>
                        <bdo id='lAnKV'></bdo><ul id='lAnKV'></ul>