Android:在用于 ViewBinding 的 ScrollView 中没有可用的视图 ID如何访问?

问题描述

我正在尝试将带有 findViewByIds 的现有项目转换为 ViewBinding。 ScrollView 之外的所有视图 ID 在 Android Studio 中都可用于“binding.viewId...” 问题是 ScrollView 内具有 ID 的所有视图都不能用于“binding.viewID.. 。”所以我无法访问位于两个

//Activity
private ActivityEditBinding binding;

protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    binding = ActivityEditBinding.inflate(getLayoutInflater());
    View view = binding.getRoot();
    setContentView(view);

// activity_edit.xml
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:focusableInTouchMode="true"
    tools:context=".AddorUpdateCardActivity">
 ...
 <RelativeLayout
    android:id="@+id/secondLinearLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/toolbar"  >

<ScrollView
    android:id="@+id/ScrollView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/secondLinearLayout"
    ...  >        
<LinearLayout
    android:id="@+id/lowerVFRow"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingRight="5dp"
    android:paddingEnd="5dp"
    android:orientation="horizontal"  >

    <ViewFlipper
        android:id="@+id/expandVF"
        android:layout_width="wrap_content"
        android:layout_height="44dp"
        android:layout_marginBottom="5dp"  >

        <include layout="@layout/expand_more_act_edit"
            android:id="@+id/expandMore"  />

        <include layout="@layout/expand_less_act_edit"
            android:id="@+id/expandLess"  />

     </ViewFlipper>

</LinearLayout>
</ScrollView>
</RelativeLayout>

// expand_more_act_edit.xml
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/downArrow"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:contentDescription="sd"
android:clickable="true"
android:focusable="true"
android:src="@drawable/ic_expand_more"  />

<!-- the down arrow -->

我确认已创建自动生成的 ViewBinding 类。下面是我尝试将 ClickListener 附加到 downArrow 但我无法访问 downArrow 引用的地方。我在这里错过了什么?

***Android Studio says "Cannot resolve symbol 'expandMore'***
binding.expandMore.downArrow.setonClickListener(v -> {
        
    // change the down arrow to the up arrow and show the 1-20 EditText lines.
    expandVF.setdisplayedChild(expandVF.indexOfChild(findViewById(R.id.expandLess)));
    moreViewStub.setVisibility(View.VISIBLE);
});

ViewBinding 在 ScrollView 中不起作用吗?我在这里错过了什么?

解决方法

视图绑定将根据您在布局中提供的 ID 生成驼峰命名法。如果您的视图 ID 已经在布局中的驼峰命名法(expandVF)中,有时 android studio 将无法识别,因为布局视图 ID 和绑定类视图 ID 相同。可能会产生“无法解析符号错误”。

所以代替 expandVF,expandMore,expandLess 使用 expand_vf,expand_more,expand_less。重新构建并运行,可能会工作。

,

您需要使用 binding.expandVF 而不是直接使用 expandVF 要使用已包含的布局 ID,您需要使用 binding.expandLess.ID_YOU_WANT_TO_USE

,

用于数据绑定

activity.java

private ActivityEditBinding binding;

protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
binding= DataBindingUtil.setContentView(this,R.layout.activity_edit);
    
}

activity_edit.xml

<layout  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:focusableInTouchMode="true"
    tools:context=".AddorUpdateCardActivity">//...
<ScrollView
<LinearLayout
    android:id="@+id/lowerVFRow"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingRight="5dp"
    android:paddingEnd="5dp"
    android:orientation="horizontal"  >

    <ViewFlipper
        android:id="@+id/expandVF"
        android:layout_width="wrap_content"
        android:layout_height="44dp"
        android:layout_marginBottom="5dp"  >

        <include layout="@layout/expand_more_act_edit"
            android:id="@+id/expandMore"  />

        <include layout="@layout/expand_less_act_edit"
            android:id="@+id/expandLess"  />

</LinearLayout>
</ScrollView>
</RelativeLayout>
</layout>

现在

binding.expandMore.downArrow.setOnClickListener(v -> {
        
});

对于 ViewBinding here

,

在你的情况下,我没有在代码中看到问题,所以我猜测可能是 Android 构建工具版本很旧 - ViewBinding 不能很好地工作

尝试将项目的 Notification::fake() 中的构建工具更新到最新版本 4.2.0

build.gradle

dependencies { // At least it should be from 4.0.0 classpath 'com.android.tools.build:gradle:4.2.0' } 中的 distributionUrl

gradle-wrapper.properties

如果与下面的代码不同,请更改 viewBinding 声明:

# tools.build 4.2.0 requruired gradle distribution at least 6.7.1+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8-bin.zip

然后清理重建您的项目。

查看Compatibility Android Gradle plugin - Release Notes

,

通过包装到布局标记将您的 activity_edit.xmlexpand_less_act_edit.xmlexpand_more_act_edit.xml 布局转换为数据绑定。

你的activity_edit.xml看起来像

 <layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:focusableInTouchMode="true"
    tools:context=".AddorUpdateCardActivity">

    <ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
    <LinearLayout
    android:id="@+id/lowerVFRow"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingRight="5dp"
    android:paddingEnd="5dp"
    android:orientation="horizontal"  >

    <ViewFlipper
    android:id="@+id/expandVF"
    android:layout_width="wrap_content"
    android:layout_height="44dp"
    android:layout_marginBottom="5dp"  >

        <include layout="@layout/expand_more_act_edit"
            android:id="@+id/expandMore"  />

        <include layout="@layout/expand_less_act_edit"
            android:id="@+id/expandLess"  />

    </LinearLayout>
    </ScrollView>
    </RelativeLayout>
    </layout>

你的expand_more_act_edit.xml看起来像

<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <ImageView  
    android:id="@+id/downArrow"
    android:layout_width="45dp"
    android:layout_height="45dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:contentDescription="sd"
    android:clickable="true"
    android:focusable="true"
    android:src="@drawable/ic_expand_more"  />
</layout>

您访问 ImageView 的主要类将是。

private ActivityEditBinding binding;

protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
binding= DataBindingUtil.setContentView(this,R.layout.activity_edit);
//binding.expandMore.downArrow this is how you will gonna access the view. now do what ever you want with this.
    
}
,

您似乎错过了 ViewFlipper 结束标记,而且 xml 中还有其他布局问题。检查是否是这个问题。

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:focusableInTouchMode="true"
tools:context=".AddorUpdateCardActivity">
<RelativeLayout
    android:id="@+id/secondLinearLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/toolbar"  >

    <ScrollView
        android:id="@+id/ScrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/secondLinearLayout">
    <LinearLayout
        android:id="@+id/lowerVFRow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingRight="5dp"
        android:paddingEnd="5dp"
        android:orientation="horizontal"  >

        <ViewFlipper
            android:id="@+id/expandVF"
            android:layout_width="wrap_content"
            android:layout_height="44dp"
            android:layout_marginBottom="5dp"  >

        <include layout="@layout/expand_more_act_edit"
            android:id="@+id/expandMore"  />

        <include layout="@layout/expand_less_act_edit"
            android:id="@+id/expandLess"  />
        </ViewFlipper>

    </LinearLayout>
</ScrollView>

我已经纠正了一些但不确定,因为我不知道整个布局。由于您提供的布局中有一些省略号。除此之外,ViewBindingScrollView AFAIK 没有任何问题。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...