标签: android-viewbinding

快速片段切换视图绑定 null

我在选项卡布局中有 2 个片段,使用 ViewBinding 在它们之间快速切换会导致视图返回为 null。这是因为构建 FragmentXBinding 类的延迟吗?

使用示例:

chatadapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
            @Override
            public void onItemRangeInserted(int positionStart, int itemCount) {
                binding.chatRecyclerView.smoothScrollToPosition(0);
            }
        });
Run Code Online (Sandbox Code Playgroud)

错误:

java.lang.NullPointerException: Attempt to read from field 'androidx.recyclerview.widget.RecyclerView com.iku.databinding.FragmentChatBinding.chatRecyclerView' on a null object reference
Run Code Online (Sandbox Code Playgroud)

data-binding android android-fragments android-viewbinding

11
推荐指数
1
解决办法
4451
查看次数

如何忽略我不想在 Android 视图绑定库中创建绑定的视图组件?

我正在探索新的Android 视图绑定库

在我的布局 XML 文件中,一些我不想包含在我的绑定类中的视图。

是否存在将视图排除在生成的 Binding 类中的任何属性或机制?

android android-layout android-viewbinder android-viewbinding

10
推荐指数
1
解决办法
2399
查看次数

如何在 BottomSheetDialog 上使用视图绑定

我正在尝试在 MainActivity 中创建一个 BottomSheetDialog,但现在我正在使用合成来绑定我的视图。但现在我被如何附加ViewBinding到我的BottomSheetDialog.

这就是我所做的,使用合成材料。

dialog = BottomSheetDialog(this, R.style.BottomDialogStyle)
dialogView = LayoutInflater.from(this).inflate(R.layout.test_dialog, dialog.mainContainer, false)
dialog.setContentView(dialogView)
Run Code Online (Sandbox Code Playgroud)

这就是让我困惑的地方ViewBinding

dialog = BottomSheetDialog(this, R.style.BottomDialogStyle)
binding = TestDialogBinding.inflate(?)
dialog.setContentView(binding.root)
Run Code Online (Sandbox Code Playgroud)

从上面的示例中,我想知道应该用什么填充参数,因为与在活动中我可以只用layoutInflater或在RecyclerView 适配器中填充该参数不同,而在适配器中我不能用

LayoutInflater.from(parent.context), parent, and false

android kotlin android-viewbinding android-bottomsheetdialog

10
推荐指数
1
解决办法
7698
查看次数

具有合成视图的ViewBinding vs Kotlin Android扩展

新的ViewBinding与带有合成视图绑定的Kotlin Android扩展相比如何?

除了新ViewBindings提供的NullSafety和TypeSafety之外,我们为什么还要考虑放弃在Views上使用合成绑定的Kotlin方法。

由于新的ViewBinding事先生成了Binding类,因此它的性能更高吗?

android kotlin kotlin-android-extensions android-viewbinding

9
推荐指数
4
解决办法
357
查看次数

片段中的视图绑定

我想使用 ViewBinding 来处理 Fragment 中的视图。

FragmentBlankBinding binding;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    binding = FragmentBlankBinding.inflate(inflater, container, false);
    return binding.getRoot();
}
Run Code Online (Sandbox Code Playgroud)

但是当我尝试从这样的绑定中获取 RecyclerView 时:

binding.notesRecyclerView.setAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)

我得到空指针异常:

java.lang.NullPointerException:尝试从空对象引用上的字段“androidx.recyclerview.widget.RecyclerView com.myapps.notes.databinding.FragmentBlankBinding.notesRecyclerView”读取

PS这里是fragment_blank.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".BlankFragment">

    <com.getbase.floatingactionbutton.FloatingActionButton
        android:id="@+id/newNoteActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_margin="3dp"
        android:elevation="10dp"
        app:fab_icon="@drawable/ic_baseline_add_24" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/notesRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" /> 
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

android android-fragments android-viewbinding

9
推荐指数
3
解决办法
7195
查看次数

在 ArrayAdapter 上使用 ViewBinding

我正在尝试重构我的应用程序以使用ViewBinding. 我已经浏览了所有的片段和活动;但是,我不确定ArrayAdapter使用视图绑定来防止内存泄漏的正确约定。

viewbinding在 ArrayAdapter 中使用 a 的正确方法是什么?

我一直在使用这种方法来处理片段:

private var _binding: BINDING_FILE_NAME? = null
private val binding get() = _binding!!

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    _binding = BINDING_FILE_NAME.inflate(inflater, container, false)
    return binding.root
}

override fun onDestroyView() {
    super.onDestroyView()
    _binding = null
}
Run Code Online (Sandbox Code Playgroud)

我这样称呼我的适配器:

   var myadapter : MyCustomAdapter = MyCustomAdapter(requireContext(), R.layout.row_autocomplete_item, myListOfStrings())
Run Code Online (Sandbox Code Playgroud)

MyCustomAdapter


class MyCustomAdapter(ctx: Context, private val layout: Int, private val allItems: List<String>) : ArrayAdapter<String>(ctx, layout, allItems) {

    var …
Run Code Online (Sandbox Code Playgroud)

android android-arrayadapter kotlin android-viewbinding

9
推荐指数
1
解决办法
3614
查看次数

ViewBinding - 包含的布局绑定导致未解析的引用

我正在我的片段之一中实现 ViewBinding。该片段的布局如下所示:

...
<androidx.core.widget.NestedScrollView
        android:id="@+id/sv_sudf_container"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/btn_sudf_continue"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/eav_sudf_avatar">

        <include
            android:id="@+id/l_sudf_details"
            layout="@layout/layout_sign_up_details_fields"/>

    </androidx.core.widget.NestedScrollView>
...
Run Code Online (Sandbox Code Playgroud)

我已经遵循了这个答案,但它也不起作用。

为片段生成的视图绑定类内部有绑定,但是属性的类型是View。当我然后引用View使用binding.lSudfDetails类型是LayoutSignUpDetailFieldsBinding. 我无法确定这种类型来自哪里,因为没有生成具有该名称的类,但是我希望它会为其分配正确的绑定类型。这是 中的属性FragmentSignUpDetailsBinding.java

@NonNull
public final View lSudfDetails;
Run Code Online (Sandbox Code Playgroud)

然而,绑定都已正确设置,它允许我引用嵌套布局中的视图,但当我开始构建时,我遇到了未解决的引用错误。当我像这样引用它们时,Lint 不会抱怨:

binding.lSudfDetails.etSudfDob

但是编译器确实会失败,并出现诸如此类的错误

未解决的参考:etSudfDob

绑定本身是根据Android 文档创建的:

private var _binding : FragmentSignUpDetailsBinding? = null
private val binding get() = _binding!!

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        _binding = FragmentSignUpDetailsBinding.inflate(inflater,container,false)
        return binding.root
    }

override fun onViewCreated(view: View, savedInstanceState: …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-fragments android-viewbinding

8
推荐指数
1
解决办法
6779
查看次数

从 Activity 访问 Fragment 中的视图绑定

我正在升级一个应用程序以删除所有合成内容并使用新的 ViewBinding 功能。到目前为止,只要您位于正在访问的类/布局内,一切都可以正常工作,但我在主活动中对布局元素有综合引用。

课堂片段:

    private var _binding: FragmentBinding? = null
    val binding get() = _binding!!
    private val compositeDisposable = CompositeDisposable()

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        App.instance.getAppComponent().inject(this)
        _binding = FragmentBinding.inflate(inflater, container, false)
        return binding.root
    }
Run Code Online (Sandbox Code Playgroud)

在 MainActivity 中我有:

    private lateinit var mainBinding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(null)
        mainBinding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(mainBinding.root)
    }
Run Code Online (Sandbox Code Playgroud)

我有一些对话框方法,它们使用合成材料来调暗片段中的布局元素。例如。

//textView on fragment layout.xml
textView?.alpha = someFloat
Run Code Online (Sandbox Code Playgroud)

这在合成时工作得很好,但是我无法根据 Android 的文档让新的视图绑定工作。如何使用 ViewBinding 轻松模仿合成行为?到目前为止,它已经增加了大量的代码,除非创建了 Fragment 的视图,否则它无法工作,并且确实使代码变得更糟,更难以理解。我尝试使用主活动布局根视图的绑定,但出现“视图必须有标签”错误,并且所有布局都以<layout>. 任何帮助表示赞赏。

android android-fragments synthetic android-activity android-viewbinding

8
推荐指数
2
解决办法
6120
查看次数

片段中的 ViewBinding 属性是否应该始终具有支持的不可为空属性?

我一直在阅读https://developer.android.com/topic/libraries/view-binding并正在考虑他们如何在片段中定义视图绑定属性。

这是示例代码:

private var _binding: ResultProfileBinding? = null
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    _binding = ResultProfileBinding.inflate(inflater, container, false)
    val view = binding.root
    return view
}

override fun onDestroyView() {
    super.onDestroyView()
    _binding = null
}
Run Code Online (Sandbox Code Playgroud)

可为空也是如此_binding,因为对视图的引用将在 中被销毁onDestroyView()。然而,它们用作binding支持属性,它检索_binding并使其不可为 null(无论 Kotlin 中调用的强制展开的等效项是什么)。

问题是为什么我们应该拥有_binding以及binding应该使用哪一个?感觉就像如果您试图使其可为_binding空,那么为什么要使其本质上不可为空 …

android kotlin android-jetpack android-viewbinding

8
推荐指数
1
解决办法
3962
查看次数

为什么使用绑定类的视图引用被标记为@Nullable?

我有一个带有一些 TextView 和 CardView 的布局。只有引用 binding.mycardview 返回一个对象CardView?,但根据文档

空安全:由于视图绑定创建对视图的直接引用,因此不存在因视图 ID 无效而导致空指针异常的风险。此外,当视图仅出现在布局的某些配置中时,绑定类中包含其引用的字段将标记为 @Nullable。

我的布局 row.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="wrap_content">

    <androidx.cardview.widget.CardView
        android:id="@+id/cVComune"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/space"
        android:layout_marginTop="@dimen/space"
        android:layout_marginEnd="@dimen/space"
        android:layout_marginBottom="@dimen/space"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/tvComune"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/space"
                android:layout_marginTop="@dimen/space"
                android:textSize="@dimen/text_label_medium"
                android:textStyle="bold"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/tvCap"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/space"
                android:layout_marginTop="@dimen/space"
                android:textSize="@dimen/text_label_medium"
                app:layout_constraintStart_toEndOf="@+id/ivMail"
                app:layout_constraintTop_toBottomOf="@+id/tvComune" />

            <TextView
                android:id="@+id/tvPrefisso"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/space"
                android:layout_marginTop="@dimen/space"
                android:textSize="@dimen/text_label_medium"
                app:layout_constraintStart_toEndOf="@+id/ivPhone"
                app:layout_constraintTop_toBottomOf="@+id/tvComune" />

            <TextView
                android:id="@+id/tvAbitanti"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/space"
                android:layout_marginTop="@dimen/space"
                android:textSize="@dimen/text_label_medium"
                app:layout_constraintStart_toEndOf="@+id/ivAbitanti"
                app:layout_constraintTop_toBottomOf="@+id/tvComune" …
Run Code Online (Sandbox Code Playgroud)

xml android kotlin-null-safety android-viewbinding

8
推荐指数
1
解决办法
1337
查看次数