我正在尝试包含一个包含其中唯一 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的情况下包含带有直接子级的布局?