簡體   English   中英

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

[英]Recyclerview preventing touch events after notifyItemChanged in Android

Recyclerview有一個ViewHolder ,通常包含TextView 文本內容是可選擇的,當長按文本打開選擇菜單時。 notifyItemChanged function 從Adapter外部調用以調整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" />

問題正在解決,如果android:layout_width of TextViewWRAP_CONTENT ,但它必須是MATCH_PARENT ,這有什么關系?

回收站視圖:

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

我所有的文本內容都包含許多跨度,並且字體大小有 RelativeSizeSpan。 更改字體大小時正在編輯。 並使用notifyItemChanged(i)通知項目。 在項目中不使用Wrap_Content的情況下,使用notifyDataSetChanged()不會出現問題。

Edit-3:問題源於完全編輯RelativeSizeSpan,因為在更改字體大小時它會更新新大小。 刪除更新,它現在使用 Textview.setTextSize(),沒問題。

因此,閱讀DrawerLayout的文檔時,它明確指出:

要使用 DrawerLayout,position 將您的主要內容視圖作為第一個子視圖,其寬度和高度為 match_parent 並且沒有 <layout_gravity>。 在主要內容視圖之后添加抽屜作為子視圖,並適當地設置 layout_gravity。 抽屜通常使用 match_parent 作為固定寬度的高度。

所以基本上你的DrawerLayout不能只包含 1 個視圖,它需要首先有你的主要內容,然后是你的RecyclerView或其他任何東西,並且RecyclerView需要有一個固定的寬度或可能wrap_content RecyclerView中的項目現在可以是任何你想要的。

這是一個例子:

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只是因為我的抽屜里有一定數量的物品。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM