RecyclerView内部抽屉布局



我试图在DrawerLayout中插入RecyclerView,但打开具有设计的活动时应用程序崩溃。

抽屉布局对于NavigationView的工作非常重要。RecyclerView应位于包含工具栏的ConstraintLayout下。

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawerlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/artboard">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--toolbar-->
<Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:orientation="horizontal"
android:paddingStart="15dp"
android:paddingEnd="15dp"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/menuIcon"
android:layout_width="36dp"
android:layout_height="36dp"
android:src="@drawable/ic_menu"
app:itemIconTint="@color/colorWhite" />
<ImageView
android:id="@+id/cartIcon"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_gravity="end"
android:src="@drawable/ic_cart" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="22dp"
android:fontFamily="@font/caveat_bold"
android:text="storePerfect"
android:textColor="@color/colorWhite"
android:textSize="26sp" />
</Toolbar>
<!--end of tool bar-->
</androidx.constraintlayout.widget.ConstraintLayout>
<!--        design here-->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="55dp">
</androidx.recyclerview.widget.RecyclerView>

<com.google.android.material.navigation.NavigationView
android:id="@+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/layout_navigation_header"
app:menu="@menu/navigation_menu" />
</androidx.drawerlayout.widget.DrawerLayout>

如何在不影响其他布局的情况下解决此问题?

我得到错误。。。


01/11 00:31:46: Launching 'app' on Nexus 6 API 28.
Install successfully finished in 688 ms.
$ adb shell am start -n "com.example.payment_screen/com.example.payment_screen.Store_Activity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 15174 on device 'Nexus_6_API_28 [emulator-5554]'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
W/ComponentDiscovery: Class com.google.firebase.dynamicloading.DynamicLoadingRegistrar is not an found.
I/FirebaseApp: Device unlocked: initializing all Firebase APIs for app [DEFAULT]
D/FirebaseAuth: Notifying id token listeners about user ( APcX0F7Se1ffN57bTkC73QdNcwz2 ).
I/FirebaseInitProvider: FirebaseApp initialization successful
I/FirebaseAuth: [FirebaseAuth:] Preparing to create service connection to fallback implementation
W/.payment_scree: Accessing hidden method Landroid/graphics/drawable/Drawable;->getOpticalInsets()Landroid/graphics/Insets; (light greylist, linking)
Accessing hidden field Landroid/graphics/Insets;->left:I (light greylist, linking)
Accessing hidden field Landroid/graphics/Insets;->right:I (light greylist, linking)
Accessing hidden field Landroid/graphics/Insets;->top:I (light greylist, linking)
Accessing hidden field Landroid/graphics/Insets;->bottom:I (light greylist, linking)
W/.payment_scree: Accessing hidden method Landroid/view/View;->getAccessibilityDelegate()Landroid/view/View$AccessibilityDelegate; (light greylist, linking)
W/.payment_scree: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
W/.payment_scree: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
W/.payment_scree: Accessing hidden method Landroid/graphics/FontFamily;-><init>()V (light greylist, reflection)
W/.payment_scree: Accessing hidden method Landroid/graphics/FontFamily;->addFontFromAssetManager(Landroid/content/res/AssetManager;Ljava/lang/String;IZIII[Landroid/graphics/fonts/FontVariationAxis;)Z (light greylist, reflection)
Accessing hidden method Landroid/graphics/FontFamily;->addFontFromBuffer(Ljava/nio/ByteBuffer;I[Landroid/graphics/fonts/FontVariationAxis;II)Z (light greylist, reflection)
Accessing hidden method Landroid/graphics/FontFamily;->freeze()Z (light greylist, reflection)
Accessing hidden method Landroid/graphics/FontFamily;->abortCreation()V (light greylist, reflection)
Accessing hidden method Landroid/graphics/Typeface;->createFromFamiliesWithDefault([Landroid/graphics/FontFamily;Ljava/lang/String;II)Landroid/graphics/Typeface; (light greylist, reflection)
I/.payment_scree: NativeAlloc concurrent copying GC freed 265(47KB) AllocSpace objects, 0(0B) LOS objects, 50% free, 1581KB/3MB, paused 484us total 113.036ms
D/OpenGLRenderer: Skia GL Pipeline
D/: HostConnection::get() New Host Connection established 0xc9cf13c0, tid 15206
I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
D/OpenGLRenderer: Swap behavior 0
D/EGL_emulation: eglCreateContext: 0xdd37b7a0: maj 3 min 1 rcv 4
D/EGL_emulation: eglMakeCurrent: 0xdd37b7a0: ver 3 1 (tinfo 0xdd3901a0)
E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
glUtilsParamSize: unknow param 0x000082da
W/.payment_scree: Accessing hidden field Landroid/os/Trace;->TRACE_TAG_APP:J (light greylist, reflection)
Accessing hidden method Landroid/os/Trace;->isTagEnabled(J)Z (light greylist, reflection)
Accessing hidden method Landroid/os/Trace;->asyncTraceBegin(JLjava/lang/String;I)V (light greylist, reflection)
Accessing hidden method Landroid/os/Trace;->asyncTraceEnd(JLjava/lang/String;I)V (light greylist, reflection)
Accessing hidden method Landroid/os/Trace;->traceCounter(JLjava/lang/String;I)V (light greylist, reflection)
D/EGL_emulation: eglMakeCurrent: 0xdd37b7a0: ver 3 1 (tinfo 0xdd3901a0)
W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@9810d75
W/.payment_scree: Accessing hidden field Landroid/view/WindowInsets;->CONSUMED:Landroid/view/WindowInsets; (light greylist, reflection)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.payment_screen, PID: 15174
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.payment_screen/com.example.payment_screen.My_Product_List}: android.view.InflateException: Binary XML file line #62: Binary XML file line #62: Error inflating class <unknown>
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: android.view.InflateException: Binary XML file line #62: Binary XML file line #62: Error inflating class <unknown>
Caused by: android.view.InflateException: Binary XML file line #62: Error inflating class <unknown>
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at android.view.LayoutInflater.createView(LayoutInflater.java:647)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170)
at com.example.payment_screen.My_Product_List.onCreate(My_Product_List.java:29)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: android.view.InflateException: Couldn't resolve menu item onClick handler onNavigationItemSelected in class com.example.payment_screen.My_Product_List
at androidx.appcompat.view.SupportMenuInflater$InflatedOnMenuItemClickListener.<init>(SupportMenuInflater.java:255)
at androidx.appcompat.view.SupportMenuInflater$MenuState.setItem(SupportMenuInflater.java:484)
at androidx.appcompat.view.SupportMenuInflater$MenuState.addItem(SupportMenuInflater.java:531)
E/AndroidRuntime:     at androidx.appcompat.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:207)
at androidx.appcompat.view.SupportMenuInflater.inflate(SupportMenuInflater.java:129)
at com.google.android.material.navigation.NavigationView.inflateMenu(NavigationView.java:362)
at com.google.android.material.navigation.NavigationView.<init>(NavigationView.java:230)
at com.google.android.material.navigation.NavigationView.<init>(NavigationView.java:119)
... 28 more
Caused by: java.lang.NoSuchMethodException: onNavigationItemSelected [interface android.view.MenuItem]
at java.lang.Class.getMethod(Class.java:2068)
at java.lang.Class.getMethod(Class.java:1690)
at androidx.appcompat.view.SupportMenuInflater$InflatedOnMenuItemClickListener.<init>(SupportMenuInflater.java:251)
... 35 more
I/Process: Sending signal. PID: 15174 SIG: 9

导航头文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:padding="20dp">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints"
android:orientation="horizontal">
<com.makeramen.roundedimageview.RoundedImageView
android:id="@+id/image_profile"
android:layout_width="70dp"
android:layout_height="70dp"
android:scaleType="centerCrop"
android:src="@drawable/ic_image_profile_default"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:riv_oval="true"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Username"
android:textSize="16sp"
android:paddingStart="12dp"
android:textStyle="bold"
android:gravity="center_vertical"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="someone@email.com"
android:textSize="14sp"
android:paddingStart="12dp"
android:gravity="center_vertical"/>
</LinearLayout>

</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

活动最终设计

这可能是因为DrawerLayout中有两个以上的子级。抽屉布局只允许的2个子视图

  1. 内容视图
  2. 导航视图

所以你的第一个视图需要是你将在这个布局中显示的内容。将RecylcerView放置在ConstraintLayout中可以解决此问题,您只需要将RecylcerView设置在ConstraintLayout之下。它应该是这样的:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawerlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/artboard">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--toolbar-->
<Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:orientation="horizontal"
android:paddingStart="15dp"
android:paddingEnd="15dp"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/menuIcon"
android:layout_width="36dp"
android:layout_height="36dp"
android:src="@drawable/ic_menu"
app:itemIconTint="@color/colorWhite" />
<ImageView
android:id="@+id/cartIcon"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_gravity="end"
android:src="@drawable/ic_cart" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="22dp"
android:fontFamily="@font/caveat_bold"
android:text="storePerfect"
android:textColor="@color/colorWhite"
android:textSize="26sp" />
</Toolbar>
<!--end of tool bar-->
<!--        design here-->
<androidx.recyclerview.widget.RecyclerView //RECYCLER VIEW INSIDE CONSTRAINTLAYOUT
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="55dp">
</androidx.recyclerview.widget.RecyclerView>
</androidx.constraintlayout.widget.ConstraintLayout>

<com.google.android.material.navigation.NavigationView
android:id="@+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/layout_navigation_header"
app:menu="@menu/navigation_menu" />
</androidx.drawerlayout.widget.DrawerLayout>

因为您正在手动配置工具栏。请执行以下操作:

  1. 确保应用程序主题没有工具栏。

    例如:styles.xml/theme.xml中应为:<style name="Theme.AAD" parent="Theme.MaterialComponents.Light.NoActionBar">

    请注意最后一个字符串NoActionBar

  2. 其次,在活动onCreate中,使用setSupportActionBar(binding.toolbar)配置工具栏

    注意:您也可以使用findViewByID而不是binding

之后,在Android Studio中,单击文件>使缓存无效/重新启动并再次运行应用程序。

这应该可以解决上述问题
如果没有,请发布Main Activity、Theme.xml和所有其他代码。

创建一个新的.xml文件并添加到activity_main.xml

包括

最新更新