Kar*_*lad 4 android exoplayer android-viewbinding
我在我的 Fragment 之一中使用 Android Exoplayer。在 Exoplayer 中,我使用自定义控件布局“@layout/custom_player”作为控件。我在布局中有不同的元素,例如我有一个按钮元素“optionBtn”,我想从我的 Kotlin 代码连接到 onclicklistener。不幸的是,这对于视图绑定来说并不是很顺利。
这是 XML Exoplayer
<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/playerVIew"
app:resize_mode="fill"
android:animateLayoutChanges="true"
app:controller_layout_id="@layout/custom_player"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
Run Code Online (Sandbox Code Playgroud)
这是科特林代码
...
private var binding: FragmentVideoBinding? = null
private var btnsheetOptions: SheetOptionsBinding? = null
private var sheetDialog: BottomSheetDialog? = null
private var customPlayer: CustomPlayerBinding? = null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
btnsheetOptions = SheetOptionsBinding.inflate(inflater, null, false)
sheetDialog = BottomSheetDialog(requireContext(), R.style.BottomSheetDialogTheme)
binding = FragmentVideoBinding.inflate(inflater, container, false)
customPlayer = CustomPlayerBinding.inflate(inflater, binding!!.root, true)
return binding!!.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val simpleExoPlayer = SimpleExoPlayer.Builder(requireContext()).build()
binding!!.playerVIew.player = simpleExoPlayer
val mediaItem = MediaItem.fromUri(video.toString())
simpleExoPlayer.addMediaItem(mediaItem)
simpleExoPlayer.prepare()
simpleExoPlayer.playWhenReady = true
customPlayer!!.optionBtn.setOnClickListener {
...
}
}
override fun onDestroy() {
super.onDestroy()
binding = null
btnsheetOptions = null
sheetDialog= null
customPlayer = null
}
}
...
Run Code Online (Sandbox Code Playgroud)
这样,布局就会双重膨胀,一个布局可以与 onclick 侦听器一起使用,而另一个则不能,这不是很有用。
有谁知道正确的解决方案吗,我几乎整个下午都在研究这个问题。
您不能将视图绑定与 ExoPlayer 的自定义 HUD 布局一起使用。视图绑定仅适用于专门为活动/片段布局扩展的布局。自定义 HUD 布局不属于玩家所在的父布局。它以独立方式膨胀,并且不包含在布局中(因此是双重膨胀)。由于自定义布局已膨胀并且不是原始布局的一部分,因此您无法将视图绑定与其中包含的所有 id 一起使用。
那么,如果视图绑定不适用于自定义布局的按钮,您该怎么办?
您应该使用findViewById属于 Activity 类的函数。它非常容易使用,我想您也已经知道如何使用:
findViewById<ImageButton>(R.id.optionBtn).setOnClickListener {...}
//The next line is for usage inside a fragment class
activity?.findViewById<ImageButton>(R.id.optionBtn).setOnClickListener {...}
Run Code Online (Sandbox Code Playgroud)
确保在布局中为按钮提供 ID,例如:
android:id="@id/optionBtn"
Run Code Online (Sandbox Code Playgroud)
如果找不到 (R.id.optionBtn) 怎么办?这是一个常见的问题,有两个R目录需要注意。有android.R通常仅用作的R。还有应用程序的R目录。为了区分两者并避免出现Unresolved reference问题,您应该以不同的名称导入应用程序的资源,这是在import类代码开始之前的部分中完成的,添加以下内容:
import com.example.app.R as appR
Run Code Online (Sandbox Code Playgroud)
然后你可以尝试使用appR.id.optionBtn替代。您遇到此特定R.id问题的可能性非常低,但如果发生这种情况,请按照上述解决方案进行操作。
底线:
1-视图绑定仅适用于连接到其上下文类的活动/片段布局,它将父布局的 id 及其所有子视图与实际的绑定变量绑定在一起。2-如果您想直接达到不属于活动/片段布局一部分的布局,则应该使用findViewById。3- 如果您在使用“R.id”时遇到问题,您应该以不同的名称导入应用程序的资源。我通常使用“X”而不是“R”。但这都是个人喜好..
| 归档时间: |
|
| 查看次数: |
2160 次 |
| 最近记录: |