Java Recyclerview防止Android中notifyItemChanged后发生触摸事件

Java Recyclerview防止Android中notifyItemChanged后发生触摸事件,java,android,android-layout,android-recyclerview,Java,Android,Android Layout,Android Recyclerview,Recyclerview有一个ViewHolder,通常包含TextView。文本内容可选择,长按文本可打开选择菜单。notifyItemChanged函数正在从适配器的外部调用,以调整Textview字体的大小。文本选择菜单在调整大小之前可用,但在调整大小之后,项目将不可选择。长按时不打开文本选择菜单。没有任何请求不允许事件,但问题发生在notifyItemChanged之后 编辑: 问题在于Recyclerview的项目视图中包含的TextView xml: <?xml version

Recyclerview
有一个
ViewHolder
,通常包含
TextView
。文本内容可选择,长按文本可打开选择菜单。
notifyItemChanged
函数正在从
适配器的外部调用,以调整
Textview
字体的大小。文本选择菜单在调整大小之前可用,但在调整大小之后,项目将不可选择。长按时不打开文本选择菜单。没有任何请求不允许事件,但问题发生在
notifyItemChanged
之后

编辑:

问题在于Recyclerview的项目视图中包含的TextView xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textRow"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textIsSelectable="true"
    android:textSize="15sp" />
<androidx.drawerlayout.widget.DrawerLayout
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.ReadBookActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</androidx.drawerlayout.widget.DrawerLayout>
编辑-2:

我所有的文本内容都包含许多跨距,字体大小也有相对的跨距。更改字体大小时正在编辑。并使用
notifyItemChanged(i)
通知项目。在项目中使用
notifyDataSetChanged()
而不使用
Wrap\u Content
时,有时不会出现问题


Edit-3:问题源于完全编辑RelativeSizeSpan,因为当更改字体大小时,它会使用新的大小进行更新。删除了续订,现在它正在使用Textview.setTextSize(),没问题。

因此,阅读文档明确说明:

要使用抽屉布局,请将主内容视图定位为第一个 具有匹配父项的宽度和高度且不匹配的子项。 在主内容视图之后添加抽屉作为子视图,并设置 布局要适当。抽屉通常用于匹配父项 具有固定宽度的高度。

因此,基本上你的
DrawerLayout
不能只包含一个视图,它需要先有你的主要内容,然后是你的
RecyclerView
或其他什么,并且
RecyclerView
需要有一个固定的宽度或潜在的
wrap\u内容。
RecyclerView
中的项目现在可以是您想要的任何内容

以下是一个例子:

activity\u main.xml:(注意,RecyclerView不是第一个项目,它的布局宽度为
wrap\u content


app\u bar\u main.xml:


content\u main.xml:



我自己并没有实际测试过这个,但它应该可以工作,因为我有完全相同的设置,除了我使用的是
NavigationView
而不是
RecyclerView
,因为我的抽屉里有一定数量的物品。

请发布一个最小可复制的示例(旁白:当呈现
内联代码时,请注意,您可以使用单反勾号,并且不需要三反勾号,尽管三反勾号确实可以工作。三反勾号仅用于块格式。在可能的情况下,最好使用单反勾号,因为这使材料更易于编写和编辑)。
<?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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />

</androidx.drawerlayout.widget.DrawerLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <include layout="@layout/content_main" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/main_fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right|end"
        android:layout_margin="16dp"
        android:elevation="@dimen/elevation"
        app:srcCompat="@drawable/ic_add" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/app_bar_main">

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>