• Stars
    star
    173
  • Rank 213,198 (Top 5 %)
  • Language
    Kotlin
  • License
    Apache License 2.0
  • Created over 8 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

AssemblyAdapter 是 Android 上的一个为各种 Adapter 提供开箱即用实现的库。AssemblyAdapter is a library on Android that provides out-of-the-box implementations for various Adapters.

AssemblyAdapter

Platform API Release License

AssemblyAdapter 是 Android 上的一个为各种 Adapter 提供开箱即用实现的库。

特性

  • Item 复用. 只需为你的 item 写一个 ItemFactory,然后就可以到处使用了. 了解更多
  • 支持多类型. 只需给 Adapter 添加多个 ItemFactory 即可轻松实现多类型 Adapter
  • 支持全部 Adapter. 支持 BaseAdapterRecyclerView.Adapter 等常用 Adapter. 了解更多
  • 更多 ConcatAdapter 支持. 为 BaseAdapter 等更多 Adapter 提供了 Concat 支持. 了解更多
  • 支持 Paging. 为 Paging 提供了多类型支持. 了解更多
  • 支持 ViewPager 和 ViewPager2. 为 ViewPager 和 ViewPager2 提供了多类型及 Paging 分页支持. 了解更多
  • 支持 spanSize 和 fullSpan. 提供了专用的 LayoutManager,可以根据 ItemFactory 设置 spanSize 和 fullSpan. 了解更多
  • 支持 divider. 为 RecyclerView 提供了强大的 divider 支持,还可以根据 position/spanIndex/ItemFactory 个性化或禁用 divider. 了解更多
  • 支持占位符 Placeholder. 通过固定的占位符数据类型支持占位符. 了解更多

导入

该库已发布到 mavenCentral

你可以直接导入所有模块,如下:

dependencies {
    implementation("io.github.panpf.assemblyadapter4:assemblyadapter:${LAST_VERSION}")
}

你还可以按需导入所需模块,如下:

dependencies {
    implementation("io.github.panpf.assemblyadapter4:assemblyadapter-list:${LAST_VERSION}")
    implementation("io.github.panpf.assemblyadapter4:assemblyadapter-pager:${LAST_VERSION}")
    implementation("io.github.panpf.assemblyadapter4:assemblyadapter-pager2:${LAST_VERSION}")
    implementation("io.github.panpf.assemblyadapter4:assemblyadapter-pager2-paging:${LAST_VERSION}")
    implementation("io.github.panpf.assemblyadapter4:assemblyadapter-recycler:${LAST_VERSION}")
    implementation("io.github.panpf.assemblyadapter4:assemblyadapter-recycler-paging:${LAST_VERSION}")
}

每个模块包含哪些 Adapter,可以参考后面 '支持的 Adapter' 部分

${LAST_VERSION}Release Version (no include 'v')

使用指南

在传统的自定义 Adapter 的过程中我们一般需要以下几个步骤(以 RecyclerView.Adapter 为例,其它 Adapter 大同小异):

  1. 定义 data 列表
  2. 重写 getItemCount、getItemId 方法
  3. 重写 getItemViewType、onCreateViewHolder、onBindViewHolder 方法根据不同的 data 提供不同的结果或实现

AssemblyAdapter 将这一传统定义过程拆分为两个组件,其职责分别如下:

  1. Adapter:
    • 定义 data 列表
    • 重写 getItemCount、getItemId 方法
    • 根据不同的 data 匹配不同的 ItemFactory
    • 使用匹配的 ItemFactory 重写 getItemViewType、onCreateViewHolder、onBindViewHolder 方法
  2. ItemFactory
    • 定义目标 data 的 class
    • 创建 item view
    • 绑定 data

支持的 Adapter

AssemblyAdapter 只是一个接口,不可以直接使用,你需要针对不同的 Adapter 使用具体的实现类,如下所示:

定义 ItemFactory

下面演示继承 BindingItemFactory 来定义我们的 ItemFactory

更多自定义 ItemFactory 详细内容请参考 ItemFactory 自定义详解

item 布局定义如下 (item_app_info.xml):

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView android:id="@+id/appItemNameText" />
    <TextView android:id="@+id/appItemVersionText" />
    <TextView android:id="@+id/appItemSizeText" />
</androidx.constraintlayout.widget.ConstraintLayout>

数据类定义如下:

data class AppInfo(
    val name: String,
    val packageName: String,
    val versionName: String,
    val apkSize: Long,
)
class AppInfoItemFactory : BindingItemFactory<AppInfo, ItemAppInfoBinding>(AppInfo::class) {

    override fun createItemViewBinding(
        context: Context, inflater: LayoutInflater, parent: ViewGroup
    ): ItemAppInfoBinding {
        /*
         * 在此处创建 ViewBinding。这个方法只执行一次
         */
        return ItemAppInfoBinding.inflate(inflater, parent, false)
    }

    override fun initItem(
        context: Context, binding: ItemAppInfoBinding, item: BindingItem<AppInfo, ItemAppBinding>
    ) {
        /*
         * 在此处初始化 item 并绑定 click 事件。这个方法只执行一次
         */
        binding.root.setOnClickListener {
            // 事件发生时从 item 获取 position 和 数据
            val data: AppInfo = item.dataOrThrow
            val bindingAdapterPosition: Int = item.bindingAdapterPosition
            val absoluteAdapterPosition: Int = item.absoluteAdapterPosition
            val launchIntent =
                context.packageManager.getLaunchIntentForPackage(data.packageName)
            if (launchIntent != null) {
                context.startActivity(launchIntent)
            }
        }
    }

    override fun bindItemData(
        context: Context,
        binding: ItemAppInfoBinding,
        item: BindingItem<AppInfo, ItemAppInfoBinding>,
        bindingAdapterPosition: Int,
        absoluteAdapterPosition: Int,
        data: AppInfo
    ) {
        /*
         * 在此处绑定 item view 的数据。这个方法会经常执行
         */
        binding.appItemNameText.text = data.name
        binding.appItemVersionText.text = "v${data.versionName}"
        binding.appItemSizeText.text = Formatter.formatFileSize(context, data.apkSize)
    }
}

使用 ItemFactory 创建多类型 Adapter

只需在创建 Adapter 时通过构造函数传入 ItemFactory 即可,传入多个 ItemFactory 就可以实现多类型 Adapter,如下:

// ListSeparatorItemFactory 是一个列表分割符 ItemFactory 具体实现就不写了
val appAdapter = AssemblyRecyclerAdapter(
    listOf(AppInfoItemFactory(), ListSeparatorItemFactory())
)

appAdapter.submitList(
    listOf(
        ListSeparator("A"),
        AppInfo("AirPortal", "cn.airportal", "4.21", 1258291L),
        AppInfo("Apex Legends Mobile", "com.ea.gp.apex", "1.2", 100258291L),
        AppInfo("APKPure", "com.apkpure.aegon", "3.17.23", 157879798L),
        ListSeparator("B"),
        AppInfo("Block Earth", "com.craft.earth", "2.42", 57879798L),
        AppInfo("Bluestack", "app.bluestack", "1.0.0", 41534523L),
        ListSeparator("C"),
        AppInfo("Craft Pixel Art Rain", "com.lucky.fairy", "15", 4247204L),
        AppInfo("Cutting Edge!", "com.cuttingedge", "0.16", 4289472412L),
        AppInfo("Cyber Knights", "com..cyberknightselite", "2.9.4", 6174924L),
        AppInfo("Guardians", "com.emagroups.cs", "1.2.3", 7782423L),
    )
)

RecyclerView(activity).adapter = appAdapter

更多功能

更新日志

Please view the CHANGELOG.md file

License

Copyright (C) 2021 panpf <[email protected]>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

More Repositories

1

sketch

Sketch is a powerful and comprehensive image load library on Android, in addition to the basic functions, it also supports Jetpack Compose, GIF, SVG, video thumbnails, gesture zoom, huge images sampling, ExifInterface and other functions. Sketch 是 Android 上的一个强大且全面的图片加载库,除了基础功能外,还支持 Jetpack Compose、GIF、SVG、视频缩略图、手势缩放、超大图采样、ExifInterface 等功能。
Kotlin
1,934
star
2

spider-web-score-view

SpiderWebScoreView 是 Android 上的一个蛛网评分控件 SpiderWebScoreView Android is used on a cobweb score view
Java
640
star
3

switch-button

SwitchButton 是 Android 上的一个开关按钮控件 【Deprecated】【Stop maintenance】
Java
332
star
4

pager-indicator

这是 Android 上的一个 ViewPager 页面指示器组件,用于标识当前显示的页面
Java
139
star
5

bintray-publish

Super easy way to publish your Android and Java artifacts to bintray.
Groovy
97
star
6

scratch-award-view

这是一个刮刮卡组件,用于实现刮奖效果
Java
52
star
7

stickyitemdecoration

RecyclerView 黏性 item 实现。RecyclerView sticky item implementation.
Kotlin
18
star
8

android-sheller

Sheller 是 Android 上的一个 shell 库,可帮助开发者方便的执行 shell 命令
Java
11
star
9

view-expander

ViewExpander 用来快速实现 View 展开关闭效果,使用非常简单,适用于所有的 View
Java
10
star
10

barcode-utils

BarcodeUtils 是一个条码解析、生成、扫描库,基于 zxing 封装,适合快速在项目中集成条码相关功能
Java
7
star
11

android-activitymonitor

Android, Activity, Monitor
Java
4
star
12

zoomimage

Android library for scaling images, supporting double-tap zoom, gesture zoom, single-finger drag, inertial swipe, location, rotate, huge image sub-sampling loading, and more. Both View and Compose are supported. 用于缩放图像的 Android 库,支持双击缩放、手势缩放、单指拖动、惯性滑动、定位、旋转、超大图采样加载等功能。支持 View 和 Compose。
Kotlin
3
star
13

tools4a

Extensions to the Android standard library and support libraries and some basic tools
Java
2
star
14

maven-publish-gradle-plugin

Gradle plugin that configures an uploadArchives task to automatically upload all of your Java, Kotlin or Android libraries to any Maven instance.
Kotlin
2
star
15

tools4k

Extensions to the Kotlin standard library
Kotlin
2
star
16

tools4j

Extensions to the Java standard library, some tool methods related to File, IO, primitive types, String, Array, and Collection
Java
2
star
17

android-liveevent

Java
1
star
18

jsonx

Extensions to the org.json standard library
Java
1
star