android BaseFragment 具有它自己的 xml 和继承的片段视图在父级的框架布局中



我有一个抽象片段,它有自己的视图(这些视图对于继承的片段很常见( 我正在创建一个从该基本片段继承的片段,它有自己的视图(布局( 怎么能做到呢? 我收到一个错误,说我需要先调用删除视图...

继承的片段的视图应进入:(在父级的 xml 中找到(

<FrameLayout
android:id="@+id/gamePadContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toTopOf="@+id/gamePadHistoryScroller"
app:layout_constraintTop_toTopOf="parent" />

碱基片段(摘要(:

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val rootView = inflater.inflate(R.layout.fragment_gamepad, container, false)
val gamePadView = inflater.inflate(onFragmentLayoutRequest(), null)
gamePadView.viewTreeObserver.addOnGlobalLayoutListener(object :
ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
joystickLeftCenter[0] = 0
joystickLeftCenter[1] = 0
joystickRightCenter[0] = 0
joystickRightCenter[1] = 0
gamePadButtonsMap[KeyEvent.KEYCODE_BUTTON_THUMBL]?.let {
joystickLeftCenter[0] = it.buttonView.left
joystickLeftCenter[1] = it.buttonView.top
}
gamePadButtonsMap[KeyEvent.KEYCODE_BUTTON_THUMBR]?.let {
joystickRightCenter[0] = it.buttonView.left
joystickRightCenter[1] = it.buttonView.top
}
gamePadView.viewTreeObserver.removeOnGlobalLayoutListener(this)
}
})
gamePadButtonsMap = onGamePadButtonMapRequest(gamePadView)
return rootView
}

基本片段 XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/gamePadContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toTopOf="@+id/gamePadHistoryScroller"
app:layout_constraintTop_toTopOf="parent" />
<!--  Buttons History  -->
<HorizontalScrollView
android:id="@+id/gamePadHistoryScroller"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="28dp"
android:layout_marginBottom="24dp"
android:fillViewport="true"
android:scrollbars="none"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.265"
tools:layout_editor_absoluteX="0dp">
<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/gamePadHistoryContainer"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginBottom="52dp"
android:background="@color/bg_buttons_history"
android:gravity="left"
android:orientation="horizontal" />
</HorizontalScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

继承片段:

class PS4Fragment : GamePadFragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
super.onCreateView(inflater, container, savedInstanceState)
return inflater.inflate(R.layout.fragment_ps4, container, false)
}
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val rootView = inflater.inflate(R.layout.fragment_gamepad, container, false)
val gamePadView = LayoutInflater.from(requireContext()).inflate(onFragmentLayoutRequest(), rootView.gamePadContainer, true)
gamePadButtonsMap = onGamePadButtonMapRequest(gamePadView)

rootView.gamePadContainer.addView(gamePadView( 返回根视图 }

我一直在使用相同的膨胀器..现在它按预期工作。 在子片段(继承片段(中没有"onCreateView">

现在 我有一个基本片段,其中包含所有片段都有的一些视图和组件,以及每个片段的唯一视图:)

谢谢!

相关内容

最新更新