【发布时间】:2019-05-29 06:32:32
【问题描述】:
如何向新创建的实例添加标签?
case 0:
return FragmentNotifications.newInstance(notifications, mCurrentTime);
新实例:
public static FragmentNotifications
newInstance(String notificationsArray, long currentTime){
FragmentNotifications fragment = new FragmentNotifications();
return fragment;
}
如何为使用 viewpager 创建的片段指定名称?
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return
FragmentNotifications.newInstance(notifications, mCurrentTime);
case 1:
return FragmentMessages.newInstance(messages,
mCurrentTime);
case 2:
return new FragmentProfile();
default:
return null;
}
}
【问题讨论】:
-
“标签”是什么意思?当您使用
FragmentManager加载片段时,您可以针对片段设置标签值 - 这就是您所指的吗? (看起来像getFragmentManager().beginTransaction().replace(layoutId,frag,"A_TAG").commit();) -
是的,但是我将如何使用新的实例呢?因为我正在尝试在 viewpager 中创建片段的新实例,所以我不能使用替换
-
我不明白这个问题 - “标签”用于
FragmentManager,作为稍后检索片段的一种方式。 Fragment 本身并没有直接的用途。您可以将标签分配为片段中的常量值,即 ...replace(layoutId,frag,MyFragment.TAG)... -
我正在尝试在 viewpager 中获取片段。但是我确定在viewpager创建片段时如何分配它(上面添加了编辑)