在使用 ViewBinding 时,我遇到了一些没有记录的案例。
第一:如何获取包含的通用视图布局部分的绑定,主绑定只看到主布局中的项目?
第二:如何获得包含的合并类型布局部分的绑定,再次主绑定只看到主布局中的项目?
自 Jetpack 发布以来,我们一直在使用DataBinding。Android 文档表明ViewBinding已添加到Android Studio 3.6 Canary 11+ 中。
我阅读了文档,但它看起来类似于DataBinding。
谁能解释一下这两个概念之间的区别?
data-binding android android-databinding android-jetpack android-viewbinding
我可以使用 ViewBindings 来替换findViewById这个典型的RecyclerView.Adapter初始化代码吗?我无法binding在对象中设置val,因为每个单元格的 ViewHolders 都不同。
class CardListAdapter(private val cards: LiveData<List<Card>>) : RecyclerView.Adapter<CardListAdapter.CardViewHolder>() {
class CardViewHolder(val cardView: View) : RecyclerView.ViewHolder(cardView)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CardViewHolder {
val binding = CardBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return CardViewHolder(binding.root)
}
override fun onBindViewHolder(holder: CardViewHolder, position: Int) {
val title = holder.cardView.findViewById<TextView>(R.id.title)
val description = holder.cardView.findViewById<TextView>(R.id.description)
val value = holder.cardView.findViewById<TextView>(R.id.value)
// ...
}
Run Code Online (Sandbox Code Playgroud) 在将 Android Studio 更新到Arctic Fox并将 Android Gradle 插件更新到7.0.0 后,我遇到了这个警告,我的意思是尽管有这个警告,但我的意思是可以成功构建应用程序,但我在这里缺少什么?这里有什么问题?
根据官方视图绑定参考,我以正确的方式启用它。如果有人有兴趣检查,这是我的build.gradle。
有一些相关的问题,但我认为它们与这种情况无关。
android intellij-idea android-studio android-gradle-plugin android-viewbinding
View Binding 作为 Android Jetpack 的一部分发布
文档:https : //developer.android.com/topic/libraries/view-binding
我的问题是,如何将视图绑定与自定义视图一起使用。Google 文档仅展示了 Activity 和 Fragment。
我试过这个,但没有显示任何内容。
LayoutInflater inflater = LayoutInflater.from(getContext());
Run Code Online (Sandbox Code Playgroud)
然后,我使用了这个,但同样没有运气。
LayoutInflater inflater = (LayoutInflater)
getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Run Code Online (Sandbox Code Playgroud)
我想也许我没有针对我的观点选择正确的布局充气器,但不确定。
安卓工作室 3.6
在 app/build.gradle 中:
android {
viewBinding.enabled = true
Run Code Online (Sandbox Code Playgroud)
这是我的 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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/bluetoothBottonMainContainer"
android:layout_width="0dp"
android:layout_height="104dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<View
android:id="@+id/viewPointNotSelect"
android:layout_width="16dp"
android:layout_height="16dp"
android:background="@drawable/circle_transparent"
app:layout_constraintBottom_toBottomOf="@+id/separator"
app:layout_constraintEnd_toStartOf="@+id/separator"
app:layout_constraintTop_toTopOf="parent" />
Run Code Online (Sandbox Code Playgroud)
和另一个 xml unclude prev。xml:
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/bottonContainer"
android:layout_width="0dp"
android:layout_height="104dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<include
android:id="@+id/qrBottonContainer"
layout="@layout/qr_bottom_container"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
Run Code Online (Sandbox Code Playgroud)
这是我的活动:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = QrBluetoothSwipeActivityBinding.inflate(layoutInflater)
setContentView(binding.root)
}
Run Code Online (Sandbox Code Playgroud)
该应用程序正在构建和运行。好的。
现在我移动 id - android:id="@+id/bluetoothBottonMainContainer"
到这样的外部容器:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" …Run Code Online (Sandbox Code Playgroud) 我开始使用 ViewBinding。在搜索了一些示例或建议后,如何将 ViewBinding 与抽象基类一起使用,该抽象基类应该处理预期出现在每个孩子的布局中的视图的相同逻辑,我最终在这里发布了这个问题。
场景:
我有一个基类public abstract class BaseFragment。有多个 Fragment 扩展了这个基类。这些 Fragment 具有从基类实现(使用 "old" findViewById())处理的公共视图。例如,每个片段的布局都应该包含一个 ID 为 text_title 的 TextView。这是从BaseFragment's处理它的方式onViewCreated():
TextView title = view.findViewById(R.id.text_title);
// Do something with the view from the base class
Run Code Online (Sandbox Code Playgroud)
现在 ViewBinding-API 为每个子片段生成绑定类。我可以使用绑定引用视图。但是我不能使用基类中的具体绑定。即使在基类中引入泛型,也有太多类型的片段绑定,我现在放弃了这个解决方案。
从抽象基类处理绑定视图的推荐方法是什么?是否有最佳实践?没有在 API 中找到内置机制来优雅地处理这种情况。
当期望子片段包含公共视图时,我可以提供抽象方法,这些方法从片段的具体绑定中返回视图,并使它们可以从基类访问。(例如protected abstract TextView getTitleView();)。但这是一个优势而不是使用findViewById()吗?你怎么认为?任何其他(更好的)解决方案?请让我们开始一些讨论。
如何在适配器中执行最新的喷气背包“视图绑定”,以自动绑定视图。我没有使用 findVeiwById 或 Butterknife 或 Kotlin 合成工具???我已经使用了新的视图绑定,并且对于 Activity 和 Fragment 工作正常。在 build.gradle 文件中启用 viewBinding 后,我能够看到 ActivityHomeBinding 和 FragmentHomeBinding 文件。另外,我看到了用于项目 xml 的 ItemListBinding 类,即 item_list.xml。但是如何在recyclerview的adapter中使用这个文件
viewBinding {
enabled = true
}
Run Code Online (Sandbox Code Playgroud)
主页活动文件
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityHomeBinding.inflate(layoutInflater)
setContentView(binding.root)
}
Run Code Online (Sandbox Code Playgroud)
分段
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
binding = FragmentHomeBinding.inflate(inflater, container, false)
val view = binding.root
return view
}
Run Code Online (Sandbox Code Playgroud)
Base Adapter:想在这里使用视图绑定。我能够看到 ItemListBinding,但不知道如何使用它。
class BaseAdapter @Inject constructor(
private val context: Context,
private val picasso: …Run Code Online (Sandbox Code Playgroud) 安卓工作室 3.6。金丝雀 12
构建.gradle:
buildscript {
ext.kotlin_version = '1.3.50'
ext.RETROFIT_VERSION = '2.6.0'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.0-alpha12'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
Run Code Online (Sandbox Code Playgroud)
在 app/build.gradle 中:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: "kotlin-kapt"
android {
viewBinding {
enabled = true
}
dataBinding {
enabled = true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
compileSdkVersion …Run Code Online (Sandbox Code Playgroud) android android-databinding android-studio-3.6 android-viewbinding
在 DialogFragment() 中使用 Android 视图绑定的正确方法是什么?
官方文档只提到了 Activity 和 Fragment:https : //developer.android.com/topic/libraries/view-binding