首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Android中防止notifyItemChanged后的回收站事件

在Android中防止notifyItemChanged后的回收站事件
EN

Stack Overflow用户
提问于 2020-06-13 10:10:22
回答 1查看 300关注 0票数 4

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

编辑:

问题是回收视图的项目视图的TextView xml:

代码语言:javascript
运行
复制
<?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,这和什么有什么关系?

回收站:

代码语言:javascript
运行
复制
<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(),没有问题。

EN

回答 1

Stack Overflow用户

发布于 2020-06-19 13:05:34

因此,阅读DrawerLayout文档时,它显式地声明:

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

因此,基本上您的DrawerLayout不能只包含一个视图,它首先需要有您的主要内容,然后是您的RecyclerView或其他东西,而RecyclerView需要有固定的宽度或潜在的wrap_content。现在,RecyclerView中的项目可以是您想要的任何内容。

下面是一个示例:

activity_main.xml: (注意到RecyclerView不是第一个项目,它有一个layout_width of wrap_content)

代码语言:javascript
运行
复制
<?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:

代码语言:javascript
运行
复制
<?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:

代码语言:javascript
运行
复制
<?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,因为我抽屉里有一组物品。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62358284

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档