【发布时间】:2021-05-31 19:19:20
【问题描述】:
我正在尝试将具有 findViewByIds 的现有项目转换为 ViewBinding。 ScrollView 之外的所有视图 ID 都可以在 Android Studio 中与“binding.viewId ...”一起使用.” 所以我无法访问位于布局中的 ViewFlipper 中的两个 我确认已创建自动生成的 ViewBinding 类。下面是我试图将 ClickListener 附加到 downArrow 但我无法访问 downArrow 引用的地方。我在这里错过了什么? ViewBinding 不能在 ScrollView 中工作吗?我在这里错过了什么?//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 -->
***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 类”——你看过它们包含的内容了吗?我已经成功地使用了
<include>的视图绑定,所以我不确定为什么expandMore不会在你的绑定中。 -
我查看了类,它包含:“@NonNull public final ExpandMoreActEditBinding expandMore;”和“@NonNull public final ViewFlipper expandVF;”和“this.expandVF = expandVF;”和“this.expandMore = expandMore;”所以看起来不错。
-
@CommonsWare 因此,在 Activity 中,我可以使用“binding.viewID...”访问所有视图 ID,除了
内的 ID。而expandVF 和expandMore 在 内部,也就是在ScrollView 内部。 ScrollView 和 LinearLayout 在自动生成的绑定类中看起来是正确的。关于如何解决的任何想法? -
"除了
内的 ID。expandVF 和 expandMore 在 内,并且在 ScrollView 内"——这似乎与您在your first comment 中写的内容。所以,我不确定要告诉你什么。 -
@CommonsWare 所有视图似乎都在自动生成的绑定类中正确设置。但是,当我尝试在 Activity 中为 ScrollView 中包含的 ID 添加“binding.viewID ...”时,该 ID 文本不会自动出现。那是 Studio 显示“无法解析符号错误”并且 ID 文本显示为红色的时候。
标签: android viewflipper android-viewbinding