gpt4 book ai didi

java - Recyclerview在Android中notifyItemChanged后防止触摸事件

转载 作者:行者123 更新时间:2023-12-01 13:45:25 24 4
gpt4 key购买 nike

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

编辑:

问题是 Recyclerview 的项目 View 的 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_widthTextViewWRAP_CONTENT , 但必须是 MATCH_PARENT ,这和什么有什么关系?

回收站 View :
<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) 通知项目.使用 notifyDataSetChanged() 时不会出现问题。不使用 Wrap_Content在项目中。

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

最佳答案

因此,请阅读 DrawerLayout 的文档它明确指出:

To use a DrawerLayout, position your primary content view as the firstchild with width and height of match_parent and no <layout_gravity>.Add drawers as child views after the main content view and set thelayout_gravity appropriately. Drawers commonly use match_parent forheight with a fixed width.


所以基本上你的 DrawerLayout不能只包含 1 个 View ,它需要首先包含您的主要内容,然后是您的 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仅仅因为我的抽屉里有一定数量的元素。

关于java - Recyclerview在Android中notifyItemChanged后防止触摸事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62358284/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com