setId() 用于以编程方式生成的片段
setId() for programmatically generated Fragment
我是 android 开发新手。
我想做一个通知菜单所以我做了一个片段class:
class NotificationFragment {
private String name;
public NotificationFragment() {}
public NotificationFragment(String name) {
this.name = name;
}
public String getName() {return this.name;}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.notification_fragment, container, false);
}
}
现在我想在我的屏幕上加载通知:
private List<int> notifications = new ArrayList<>();
private ConstraintLayer notification_layout = findViewById(R.id.aConstraintLayoutToHoldNotifications);
public load_notification(int i) {
NotificationFragment cn = new NotificationFragment("TestName"+i);
FragmentFrame ff = new FragmentFrame(this);
int last_id = (notifications.size() >= 1 ? notifications.get(notifications.size() - 1) : ConstraintSet.PARENT_ID);
int toObjectSide = (last_id == ConstraintSet.PARENT_ID? ConstraintSet.TOP : ConstraintSet.BOTTOM);
int ff_id = View.generateViewId();
ff.setId(ff_id);
notification_layout.add(ff);
ConstraintSet cs = new ConstraintSet();
cs.clone(notification_layout);
cs.connect(
ff_id,
ConstraintSet.TOP,
last_id,
toObjectSide
);
cs.applyTo(notification_layout);
getSupportFragmentManager()
.beginTransaction()
.add(notificationLayerID, cn, "UIFragment")
.commit();
notifications.add(ff_id);
}
现在可以正常工作了。
但是,如果我再次调用它,它会抛出一个错误 java.lang.RuntimeException: All children of ConstraintLayout must have ids to use ConstraintSet
所以我尝试进行一些调试,我认为该片段需要一个 id,但是没有 setId() 用于片段 class.
问题:
1) is there a way to set the id of a programmatically generated Fragment?
2) if not, is there a "hack" around this?
3) is there something that i am missing and i don't need an id on the Fragment?
注意
我尽量减少代码,post 上的应该是最小可执行示例
这应该可以回答问题 1,但它可能无法解决您的问题。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.notification_fragment, container, false);
view.setId(View.generateViewId());
return view;
}
或者在你 notification_fragment.xml 中添加一个 Id 到根元素,但是如果你同时有两个通知,你很可能会 运行 陷入重复的 ids 问题。
我是 android 开发新手。
我想做一个通知菜单所以我做了一个片段class:
class NotificationFragment {
private String name;
public NotificationFragment() {}
public NotificationFragment(String name) {
this.name = name;
}
public String getName() {return this.name;}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.notification_fragment, container, false);
}
}
现在我想在我的屏幕上加载通知:
private List<int> notifications = new ArrayList<>();
private ConstraintLayer notification_layout = findViewById(R.id.aConstraintLayoutToHoldNotifications);
public load_notification(int i) {
NotificationFragment cn = new NotificationFragment("TestName"+i);
FragmentFrame ff = new FragmentFrame(this);
int last_id = (notifications.size() >= 1 ? notifications.get(notifications.size() - 1) : ConstraintSet.PARENT_ID);
int toObjectSide = (last_id == ConstraintSet.PARENT_ID? ConstraintSet.TOP : ConstraintSet.BOTTOM);
int ff_id = View.generateViewId();
ff.setId(ff_id);
notification_layout.add(ff);
ConstraintSet cs = new ConstraintSet();
cs.clone(notification_layout);
cs.connect(
ff_id,
ConstraintSet.TOP,
last_id,
toObjectSide
);
cs.applyTo(notification_layout);
getSupportFragmentManager()
.beginTransaction()
.add(notificationLayerID, cn, "UIFragment")
.commit();
notifications.add(ff_id);
}
现在可以正常工作了。
但是,如果我再次调用它,它会抛出一个错误 java.lang.RuntimeException: All children of ConstraintLayout must have ids to use ConstraintSet
所以我尝试进行一些调试,我认为该片段需要一个 id,但是没有 setId() 用于片段 class.
问题:
1) is there a way to set the id of a programmatically generated Fragment?
2) if not, is there a "hack" around this?
3) is there something that i am missing and i don't need an id on the Fragment?
注意
我尽量减少代码,post 上的应该是最小可执行示例
这应该可以回答问题 1,但它可能无法解决您的问题。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.notification_fragment, container, false);
view.setId(View.generateViewId());
return view;
}
或者在你 notification_fragment.xml 中添加一个 Id 到根元素,但是如果你同时有两个通知,你很可能会 运行 陷入重复的 ids 问题。