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 中不起作用吗?我在这里错过了什么?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)