2

我正在尝试包含一个包含其中唯一 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 包装在任何其他视图中。

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

4

1 回答 1

3

当您提供 ID 时,<include>意味着您将 id 设置为您所包含的布局的根视图。在这种情况下,您将 id 设置为您的回收站视图,这就是为什么它无法找到具有原始 ID 的回收站视图。你需要做的是

  1. <merge>在共享布局中使用。
  2. 把你包裹RecyclerView起来<merge>
  3. 不要提供<include>任何身份证件。
  4. 调用bind()生成的合并布局类的方法,并传递您包含布局的布局的根视图。
  5. 从合并绑定的对象访问您的视图

例如,您home_recycler_view.xml已经HomeRecyclerView.class生成了,这就是您从该布局访问视图的方式。

class TestActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding = ActivityTestBinding.inflate(layoutInflater)
        val homeRecyclerBinding= HomeRecyclerView.bind(binding.root)
        setContentView(binding.root)
        homeRecyclerBinding.homeRecyclerView.adapter= SomeAdapter()
    }
}
于 2020-04-11T04:15:31.867 回答