Giter Club home page Giter Club logo

ocnyang / recyclerviewevent Goto Github PK

View Code? Open in Web Editor NEW
305.0 8.0 52.0 8.33 MB

RecyclerView onItemClick、onItemLongClick、drag、swipe、divider、reuse disorder RecyclerView 梳理:点击&长按事件、分割线、拖曳排序、滑动删除、优雅解决 EditText 和 CheckBox 复用错乱问题、定向刷新、DiffUtil、局部刷新

Home Page: https://juejin.im/post/5a320ffcf265da43200342a3

License: Apache License 2.0

Java 47.36% Kotlin 52.64%
recyclerview itemtouchhelper swipe drag recyclerview-adapter recyclerviewadapter recyclerview-divider reuse disorder diffutil

recyclerviewevent's Introduction

RecyclerViewEvent

本项目展示内容:

  • 点击事件、长按事件
  • 分割线
  • 拖曳排序、侧滑删除
  • 拖曳排序 & 首个固定
  • 复用错乱(复选框、编辑框)
  • 定向刷新
  • DiffUtil 的使用
  • Paging 3 的使用
  • DiffUtil & 定向刷新 结合使用
  • 条目长按的上下文菜单按钮
  • ConcatAdapter (旧名:MergeAdapter)




效果图  

  1. swipe and drag.拖曳 + 滑动删除
  2. drag. 拖曳 + 首个 item 固定

PointRefreshPointRefresh

1. 基础讲解

这次主要是把 RecyclerView 比较常用的基本的点,在这里集中整理一下。从这篇文章主要梳理以下几点:

  • 优雅的实现:item 点击事件 & item 长点击事件
  • RecyclerView 添加 divider 的标准姿势
  • RecyclerView 实现 item 的拖曳排序和滑动删除
  • 拖曳排序时,限制首个 item 固定的实现

详解介绍请看本项目对应的讲解文章:
RecyclerView 梳理:点击&长按事件、分割线、拖曳排序、滑动删除

2. 解决 RecyclerView 复用错乱 之 优雅方式

当 RcyclerView 中存在 CheckBox 或 EditText 时,因为复用机制的存在,会在滚动时造成数据混乱。 虽然网上流传的有多种方法来解决,但都比较繁琐或存在一定缺陷。 这里给大家提供一种比较优雅而合理的方式来解决复用错乱的问题:

reuse disorder
图片显示失败?点击查看

方法详情和讲解请看源码

3. 使用 notifyItemChanged(int position, @Nullable Object payload) 定向刷新

PointRefresh

仔细观察效果图,会发现:非定向刷新时条目会有闪动;定向刷新不会有闪动

具体查看源码说明:NotifyItemAdapter.onBindViewHolder(holder: NotifyItemViewHolder, position: Int, payloads: MutableList)

4. 使用 DiffUtil 实现局部刷新

DiffUtil

具体查看源码说明:Activity AdapterDiffCallback

5. 使用 Paging 3 分页加载

Paging3

具体查看源码说明:paging3 相关代码

6. ConcatAdapter 实现多个不同的 Adapter 合并成一个

ConcatAdapter

使用方式:

    recyclerView.adapter = ConcatAdapter(mHeaderAdapter, mAdapter, mFooterAdapter)
  • 可以使不同的 Adapter (不同数据、不同布局) 合并成一个 Adapter 设置给 RecyclerView 显示;
  • 功能有点类似多 ItemViewType 的 Adapter,不同的是每个 Adapter 是从上到下按顺序显示的,不能混排
  • 您可能使用过 ViewHolder.getAdapterPosition 来获得 Adapter 中某个 ViewHolder 的位置。现在,因为我们合并了多个 Adapter,作为代替,您需要调用 ViewHolder.getBindingAdapterPosition ()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.