我有一个 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!