ConstraintLayout Flow 与 Fragments 以编程方式
ConstraintLayout's Flow vs. Fragments programaticaly
我的 ConstraintLayout
中有 Flow
设置如下:
<androidx.constraintlayout.helper.widget.Flow
android:id="@+id/subjects"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
app:flow_maxElementsWrap="2"
app:flow_wrapMode="aligned"
app:constraint_referenced_ids="test1,test2,test3"
app:layout_constraintTop_toBottomOf="@id/pair_your_device_subtitle" />
<!-- The test ids are just simple Views with colorful background -->
它有效,测试视图排列在 2 列中。
现在,我有列表,它被转换为片段,我希望这些片段被安排在这个流程中。
所以我有如下代码:
childFragmentManager.registerFragmentLifecycleCallbacks(
object : FragmentManager.FragmentLifecycleCallbacks() {
override fun onFragmentViewCreated(fm: FragmentManager, f: Fragment, v: View, savedInstanceState: Bundle?) {
if (f is SnapshotFragment) {
Timber.d("onFragmentViewCreated ${v.id}")
require(v.id != View.NO_ID) // id is generated in SnapshotFragment with View.generateId()
subjects.addView(v)
// Testing
subjects.addView(View(requireContext()).apply {
id = View.generateViewId()
layoutParams = ViewGroup.LayoutParams(400, 400)
background = ColorDrawable(resources.getColor(android.R.color.black))
constraint.addView(this)
})
Timber.d("onFragmentViewCreated ${subjects.referencedIds.asList()}")
}
}
},
false
)
childFragmentManager.commit {
subjectsList.forEach { subject ->
add(
constraint.id,
SnapshotFragment::class.java,
bundleOf(SnapshotFragment.SUBJECT_KEY to subject)
)
}
}
这是行不通的。
好了,调用了回调,日志中的id看起来不错,正确添加了黑色测试视图,但是片段的视图卡在了左上角。
https://issuetracker.google.com/issues/159516508
这个问题就是原因。当我将视图 ID 生成从 onViewCreated()
直接更改为 onCreateView()
时,它开始工作了。
我的 ConstraintLayout
中有 Flow
设置如下:
<androidx.constraintlayout.helper.widget.Flow
android:id="@+id/subjects"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
app:flow_maxElementsWrap="2"
app:flow_wrapMode="aligned"
app:constraint_referenced_ids="test1,test2,test3"
app:layout_constraintTop_toBottomOf="@id/pair_your_device_subtitle" />
<!-- The test ids are just simple Views with colorful background -->
它有效,测试视图排列在 2 列中。
现在,我有列表,它被转换为片段,我希望这些片段被安排在这个流程中。
所以我有如下代码:
childFragmentManager.registerFragmentLifecycleCallbacks(
object : FragmentManager.FragmentLifecycleCallbacks() {
override fun onFragmentViewCreated(fm: FragmentManager, f: Fragment, v: View, savedInstanceState: Bundle?) {
if (f is SnapshotFragment) {
Timber.d("onFragmentViewCreated ${v.id}")
require(v.id != View.NO_ID) // id is generated in SnapshotFragment with View.generateId()
subjects.addView(v)
// Testing
subjects.addView(View(requireContext()).apply {
id = View.generateViewId()
layoutParams = ViewGroup.LayoutParams(400, 400)
background = ColorDrawable(resources.getColor(android.R.color.black))
constraint.addView(this)
})
Timber.d("onFragmentViewCreated ${subjects.referencedIds.asList()}")
}
}
},
false
)
childFragmentManager.commit {
subjectsList.forEach { subject ->
add(
constraint.id,
SnapshotFragment::class.java,
bundleOf(SnapshotFragment.SUBJECT_KEY to subject)
)
}
}
这是行不通的。
好了,调用了回调,日志中的id看起来不错,正确添加了黑色测试视图,但是片段的视图卡在了左上角。
https://issuetracker.google.com/issues/159516508
这个问题就是原因。当我将视图 ID 生成从 onViewCreated()
直接更改为 onCreateView()
时,它开始工作了。