Recyclerview在Android中的notifyItemChanged之后防止触摸事件

时间:2020-06-13 10:10:22

标签: java android android-layout android-recyclerview

Recyclerview的{​​{1}}通常包含ViewHolder。文本内容是可选的,长按文本可打开选择菜单。 TextView函数正在从notifyItemChanged外部调用以调整Adapter字体的大小。文本选择菜单在调整大小之前可用,但之后无法选择。长按不打开文本选择菜单。没有任何禁止事件的请求,但是问题发生在Textview之后。

编辑:

问题是Recyclerview的TextView xml即项目视图:

notifyItemChanged

问题是要解决<?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" /> 中的android:layout_widthTextView,但必须是WRAP_CONTENT,这与什么有什么关系?

Recyclerview:

MATCH_PARENT

编辑2:

我所有的文本内容都包含许多跨度,并且字体大小有RelativeSizeSpan。更改字体大小时正在编辑。然后用<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> 通知该项目。在项目中同时使用notifyItemChanged(i)和使用notifyDataSetChanged()都不会出现问题。

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

1 个答案:

答案 0 :(得分:0)

因此,阅读DrawerLayout的文档时会明确指出:

要使用DrawerLayout,请将主内容视图定位为第一个 宽度和高度为match_parent且没有的子级。 在主要内容视图之后添加抽屉作为子视图,并设置 layout_gravity适当。抽屉通常使用match_parent 高度并具有固定宽度。

因此,基本上您的DrawerLayout不能仅包含1个视图,它需要首先包含您的主要内容,然后是您的RecyclerView或其他任何内容,并且RecyclerView需要具有固定的宽度或可能wrap_contentRecyclerView内的物品现在可以随您便。

这里是一个例子:

activity_main.xml: (注意,RecyclerView不是第一项,它的layout_width为wrap_content

<?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>

app_bar_main.xml:

<?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>

content_main.xml:

<?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>

我自己还没有实际测试过它,但是它应该和我完全一样的设置才能工作,除了我使用NavigationView而不是RecyclerView只是因为我有一定​​数量的我的抽屉里的物品。