当包含带有 带有直接子元素的标记的布局时,Android视图绑定问题

问题描述 投票:0回答:1

我正在尝试包括仅包含recyclerView的布局。

home_recycler_view.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/home_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:paddingStart="@dimen/_8sdp"
android:paddingEnd="@dimen/_8sdp" />

并且将其包含在下面的主布局中。

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include
        android:id="@+id/slider_recyclerView"
        layout="@layout/home_recycler_view" />

    <include
        android:id="@+id/boards_recyclerview"
        layout="@layout/home_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/_12sdp" />

    <include
        android:id="@+id/news_recyclerview"
        layout="@layout/home_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/_12sdp" />

    <include
        android:id="@+id/video_recyclerview"
        layout="@layout/home_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/_12sdp"
        android:layout_marginBottom="@dimen/_2sdp" />

</LinearLayout>

在这种情况下,它给我一个类似的错误,

java.lang.NullPointerException: Missing required view with ID: homeRecyclerView

home_recycler_view.xml的生成类

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;
import com.backendme.arduinolearn.R;
import java.lang.NullPointerException;
import java.lang.Override;
import java.lang.String;

public final class HomeRecyclerViewBinding implements ViewBinding {
  @NonNull
  private final RecyclerView rootView;

  @NonNull
  public final RecyclerView homeRecyclerView;

  private HomeRecyclerViewBinding(@NonNull RecyclerView rootView,
      @NonNull RecyclerView homeRecyclerView) {
    this.rootView = rootView;
    this.homeRecyclerView = homeRecyclerView;
  }

  @Override
  @NonNull
  public RecyclerView getRoot() {
    return rootView;
  }

  @NonNull
  public static HomeRecyclerViewBinding inflate(@NonNull LayoutInflater inflater) {
    return inflate(inflater, null, false);
  }

  @NonNull
  public static HomeRecyclerViewBinding inflate(@NonNull LayoutInflater inflater,
      @Nullable ViewGroup parent, boolean attachToParent) {
    View root = inflater.inflate(R.layout.home_recycler_view, parent, false);
    if (attachToParent) {
      parent.addView(root);
    }
    return bind(root);
  }

  @NonNull
  public static HomeRecyclerViewBinding bind(@NonNull View rootView) {
    // The body of this method is generated in a way you would not otherwise write.
    // This is done to optimize the compiled bytecode for size and performance.
    String missingId;
    missingId: {
      RecyclerView homeRecyclerView = rootView.findViewById(R.id.home_recycler_view);
      if (homeRecyclerView == null) {
        missingId = "homeRecyclerView";
        break missingId;
      }
      return new HomeRecyclerViewBinding((RecyclerView) rootView, homeRecyclerView);
    }
    throw new NullPointerException("Missing required view with ID: ".concat(missingId));
  }
}

如果我将recyclerView用框架布局之类的任何父布局包装,则效果很好,但我不想在任何其他视图中包装recyclerView。

请为此提供一个解决方案,在这种情况下,如何在不包含任何根或父viewGroup的情况下在直接子项(RecyclerView)中包含布局?

android android-recyclerview android-viewbinding
1个回答
0
投票

[给ID表示将ID设置为所包含布局的根视图。在这种情况下,您需要将ID设置为您的回收站视图,这就是为什么它无法使用其原始ID找到回收站视图的原因。您需要做的是

  1. 使用共享布局中的<merge>
© www.soinside.com 2019 - 2024. All rights reserved.