;
30 | }
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/app/src/main/java/com/tencent/multiadapter/example/DataUtils.kt:
--------------------------------------------------------------------------------
1 | package com.tencent.multiadapter.example
2 |
3 | /**
4 |
5 | * Author:岑胜德 on 2021/8/30 15:19
6 |
7 | * 说明:
8 |
9 | */
10 |
--------------------------------------------------------------------------------
/app/src/main/java/com/tencent/multiadapter/example/bean/BeanA.java:
--------------------------------------------------------------------------------
1 | package com.tencent.multiadapter.example.bean;
2 |
3 | /**
4 | * Author:岑胜德 on 2021/2/22 17:17
5 | *
6 | * 说明:
7 | */
8 | public class BeanA {
9 |
10 | public String text;
11 |
12 | public BeanA(String text) {
13 | this.text = text;
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/app/src/main/java/com/tencent/multiadapter/example/bean/BeanB.java:
--------------------------------------------------------------------------------
1 | package com.tencent.multiadapter.example.bean;
2 |
3 | /**
4 | * Author:岑胜德 on 2021/2/22 17:17
5 | *
6 | * 说明:
7 | */
8 | public class BeanB {
9 |
10 | public String text;
11 |
12 | public BeanB(String text) {
13 | this.text = text;
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/app/src/main/java/com/tencent/multiadapter/example/bean/BeanC.java:
--------------------------------------------------------------------------------
1 | package com.tencent.multiadapter.example.bean;
2 |
3 | /**
4 | * Author:岑胜德 on 2021/5/12 17:24
5 | *
6 | * 说明:简单的列表单选/多选直接继承 SimpleCheckable 即可,
7 | * 更复杂的列表选择请实现Checkable接口
8 | * */
9 | public class BeanC {
10 |
11 | public String text="";
12 |
13 | public BeanC( String text) {
14 | this.text = text;
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/app/src/main/java/com/tencent/multiadapter/example/bean/ItemBean.java:
--------------------------------------------------------------------------------
1 | package com.tencent.multiadapter.example.bean;
2 |
3 | import android.text.TextUtils;
4 |
5 | import java.util.Objects;
6 |
7 | /**
8 | * Author:岑胜德 on 2021/1/6 18:05
9 | *
10 | * 说明:
11 | */
12 | public class ItemBean {
13 |
14 | //所有Item类型都在这里定义
15 | public static final int TYPE_00 = 0;
16 | public static final int TYPE_01 = 1;
17 | public static final int TYPE_02 = 2;
18 |
19 | public int id;
20 | // Item类型标识(很关键!)
21 | public int viewType;
22 |
23 |
24 | //item具体业务数据字段
25 | public String text = "";
26 |
27 |
28 | public ItemBean(int viewType, String text) {
29 | this.viewType = viewType;
30 | this.text = text;
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/app/src/main/java/com/tencent/multiadapter/example/item/AItemType.java:
--------------------------------------------------------------------------------
1 | package com.tencent.multiadapter.example.item;
2 |
3 | import android.view.View;
4 | import android.widget.Toast;
5 |
6 | import androidx.annotation.Keep;
7 | import androidx.annotation.NonNull;
8 | import androidx.annotation.Nullable;
9 |
10 | import com.tencent.lib.multi.core.MultiViewHolder;
11 | import com.tencent.lib.multi.core.SimpleItemType;
12 | import com.tencent.multiadapter.databinding.ItemABinding;
13 | import com.tencent.multiadapter.example.bean.BeanA;
14 |
15 | import org.jetbrains.annotations.NotNull;
16 |
17 | /**
18 | * Author:岑胜德 on 2021/1/6 18:04
19 | *
20 | * 说明:
21 | */
22 | public class AItemType extends SimpleItemType {
23 |
24 | public AItemType() {
25 | bind(this);
26 | }
27 |
28 | @Override
29 | public boolean isMatched(@Nullable Object bean, int position) {
30 | return bean instanceof BeanA;
31 | }
32 |
33 |
34 | @Override
35 | public void onBindView(@NonNull ItemABinding binding,
36 | @NotNull BeanA itemBean,
37 | int position) {
38 | binding.tvA.setText(itemBean.text);
39 | }
40 |
41 | /**
42 | * item点击事件
43 | * 注意 bean 类型,一定要与当前 ItemType 的 bean 类型对应。
44 | */
45 | @Keep
46 | private void onClickItem(View view, BeanA bean, int position) {
47 | Toast.makeText(view.getContext(), "点击事件:" + bean.text, Toast.LENGTH_SHORT).show();
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/app/src/main/java/com/tencent/multiadapter/example/item/BItemType.java:
--------------------------------------------------------------------------------
1 | package com.tencent.multiadapter.example.item;
2 |
3 | import android.util.Log;
4 | import android.view.View;
5 | import android.widget.Toast;
6 |
7 | import androidx.annotation.NonNull;
8 |
9 | import com.tencent.lib.multi.core.MultiViewHolder;
10 | import com.tencent.lib.multi.core.SimpleItemType;
11 | import com.tencent.multiadapter.databinding.ItemBBinding;
12 | import com.tencent.multiadapter.example.bean.BeanB;
13 |
14 | /**
15 | * Author:岑胜德 on 2021/1/6 18:04
16 | *
17 | * 说明:
18 | */
19 | public class BItemType extends SimpleItemType {
20 |
21 | public BItemType() {
22 | bind(this);
23 | }
24 |
25 | @Override
26 | public boolean isMatched(Object bean, int position) {
27 | return bean instanceof BeanB;
28 | }
29 |
30 |
31 | @Override
32 | public void onBindView(@NonNull ItemBBinding binding, @NonNull BeanB data, int position) {
33 | binding.tvB.setText(data.text);
34 |
35 | }
36 |
37 | /**
38 | * item点击事件
39 | * 注意 bean 类型,一定要与当前 ItemType 的 bean 类型对应。
40 | */
41 | private boolean onLongClick(View view, BeanB bean, int position) {
42 | Toast.makeText(view.getContext(), "长点击事件:" + bean.text, Toast.LENGTH_SHORT).show();
43 | return true;
44 | }
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/app/src/main/java/com/tencent/multiadapter/example/item/CItemType.java:
--------------------------------------------------------------------------------
1 | package com.tencent.multiadapter.example.item;
2 |
3 | import android.annotation.SuppressLint;
4 | import android.util.Log;
5 |
6 | import androidx.annotation.NonNull;
7 |
8 | import com.tencent.lib.multi.core.SimpleItemType;
9 | import com.tencent.lib.multi.core.MultiViewHolder;
10 | import com.tencent.multiadapter.databinding.ItemCBinding;
11 | import com.tencent.multiadapter.example.bean.BeanC;
12 |
13 | /**
14 | * Author:岑胜德 on 2021/1/6 18:04
15 | *
16 | * 说明:
17 | */
18 | public class CItemType extends SimpleItemType {
19 |
20 | @Override
21 | public boolean isMatched(Object bean, int position) {
22 | return bean instanceof BeanC;
23 | }
24 |
25 | @Override
26 | public void onBindView(@NonNull ItemCBinding binding,
27 | @NonNull BeanC bean, int position) {
28 | binding.tvC.setText(bean.text);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/app/src/main/java/com/tencent/multiadapter/example/item/ItemType00.kt:
--------------------------------------------------------------------------------
1 | package com.tencent.multiadapter.example.item
2 |
3 | import android.view.View
4 | import android.widget.Toast
5 | import androidx.annotation.Keep
6 | import com.tencent.lib.multi.core.MultiViewHolder
7 | import com.tencent.lib.multi.core.SimpleItemType
8 | import com.tencent.multiadapter.databinding.Item00Binding
9 | import com.tencent.multiadapter.example.bean.ItemBean
10 |
11 | /**
12 |
13 | * Author:岑胜德 on 2021/12/5 20:31
14 |
15 | * 说明:
16 |
17 | */
18 | class ItemType00 : SimpleItemType() {
19 |
20 | init {
21 | bind(this)
22 | }
23 |
24 | override fun isMatched(bean: Any?, position: Int): Boolean {
25 | return bean is ItemBean && bean.viewType == ItemBean.TYPE_00
26 | }
27 |
28 |
29 | override fun onBindView(vb: Item00Binding, bean: ItemBean, position: Int) {
30 | vb.tvA.text = bean.text
31 | }
32 |
33 | /**
34 | *item点击事件
35 | */
36 | @Keep
37 | private fun onClickItem(view: View, itemBean: ItemBean, position: Int) {
38 | Toast.makeText(
39 | view.context,
40 | "点击事件:ItemBean:${itemBean.text},position:$position",
41 | Toast.LENGTH_SHORT
42 | ).show()
43 | }
44 |
45 | /**
46 | * item 长点击事件
47 | */
48 | @Keep
49 | private fun onLongClick(view: View, itemBean: ItemBean, position: Int): Boolean {
50 | Toast.makeText(
51 | view.context,
52 | "长点击事件:ItemBean:${itemBean.text},position:$position",
53 | Toast.LENGTH_SHORT
54 | ).show()
55 | return true
56 | }
57 |
58 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/tencent/multiadapter/example/item/ItemType01.kt:
--------------------------------------------------------------------------------
1 | package com.tencent.multiadapter.example.item
2 |
3 | import com.tencent.lib.multi.core.SimpleItemType
4 | import com.tencent.multiadapter.databinding.Item01Binding
5 | import com.tencent.multiadapter.example.bean.ItemBean
6 |
7 | /**
8 |
9 | * Author:岑胜德 on 2021/12/5 20:31
10 |
11 | * 说明:
12 |
13 | */
14 | class ItemType01 : SimpleItemType() {
15 |
16 | override fun isMatched(bean: Any?, position: Int): Boolean {
17 | return bean is ItemBean && bean.viewType == ItemBean.TYPE_01
18 | }
19 |
20 | override fun onBindView(vb: Item01Binding, bean: ItemBean, position: Int) {
21 | vb.tvA.text = bean.text
22 | }
23 |
24 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/tencent/multiadapter/example/item/ItemType02.kt:
--------------------------------------------------------------------------------
1 | package com.tencent.multiadapter.example.item
2 |
3 | import com.tencent.lib.multi.core.SimpleItemType
4 | import com.tencent.multiadapter.databinding.Item02Binding
5 | import com.tencent.multiadapter.example.bean.ItemBean
6 |
7 | /**
8 |
9 | * Author:岑胜德 on 2021/12/5 20:31
10 |
11 | * 说明:
12 |
13 | */
14 | class ItemType02 : SimpleItemType() {
15 |
16 | override fun isMatched(bean: Any?, position: Int): Boolean {
17 | return bean is ItemBean && bean.viewType == ItemBean.TYPE_02
18 | }
19 |
20 | override fun onBindView(vb: Item02Binding, bean: ItemBean, position: Int) {
21 | vb.tvA.text = bean.text
22 | }
23 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/tencent/multiadapter/example/ui/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.tencent.multiadapter.example.ui
2 |
3 | import android.app.Activity
4 | import android.content.Intent
5 | import android.os.Bundle
6 | import android.view.View
7 | import android.widget.Button
8 | import androidx.appcompat.app.AppCompatActivity
9 | import com.tencent.multiadapter.R
10 |
11 | class MainActivity : AppCompatActivity() {
12 |
13 | override fun onCreate(savedInstanceState: Bundle?) {
14 | super.onCreate(savedInstanceState)
15 | setContentView(R.layout.activity_main)
16 | }
17 |
18 | fun onClickBtn(view: View) {
19 | val btn = (view as Button)
20 | when (btn.id) {
21 | R.id.demo_01 -> {
22 | goTo(MultiItemDemo01Activity::class.java)
23 | }
24 | R.id.demo_02 -> {
25 | goTo(MultiItemDemo02Activity::class.java)
26 | }
27 |
28 | }
29 |
30 | }
31 |
32 | private fun goTo(clazz: Class) {
33 | startActivity(Intent(this, clazz))
34 | }
35 |
36 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/tencent/multiadapter/example/ui/MultiItemDemo01Activity.kt:
--------------------------------------------------------------------------------
1 | package com.tencent.multiadapter.example.ui
2 |
3 | import android.os.Bundle
4 | import android.view.LayoutInflater
5 | import androidx.appcompat.app.AppCompatActivity
6 | import com.tencent.lib.multi.MultiAdapter
7 | import com.tencent.multiadapter.databinding.ActivityMultiItemBinding
8 | import com.tencent.multiadapter.example.bean.ItemBean
9 | import com.tencent.multiadapter.example.item.ItemType00
10 | import com.tencent.multiadapter.example.item.ItemType01
11 | import com.tencent.multiadapter.example.item.ItemType02
12 | import java.util.*
13 |
14 | /**
15 | * 单 bean 类型对应多样 item。
16 | */
17 | class MultiItemDemo01Activity : AppCompatActivity() {
18 |
19 | lateinit var adapter: MultiAdapter
20 | private val vb by lazy { ActivityMultiItemBinding.inflate(LayoutInflater.from(this)) }
21 |
22 | override fun onCreate(savedInstanceState: Bundle?) {
23 | super.onCreate(savedInstanceState)
24 | setContentView(vb.root)
25 | //初始化ItemType
26 | val item00 = ItemType00()
27 | val item01 = ItemType01()
28 | val item02 = ItemType02()
29 | /*初始化Adapter*/
30 | adapter = MultiAdapter(initialCapacity = 3)
31 | adapter.clearAllItemTypes()
32 | /*将所有ItemType添加到Adapter中*/
33 | adapter.addItemType(item00)
34 | .addItemType(item01)
35 | .addItemType(item02)
36 | /*设置数据*/
37 | adapter.setDataList(getData())
38 | vb.rvList.adapter = adapter
39 | }
40 |
41 | /**
42 | * 模拟数据
43 | */
44 | private fun getData(): List {
45 | val beans = ArrayList()
46 | for (i in 0..5) {
47 | beans.add(ItemBean(ItemBean.TYPE_00, "我是A_00类Item$i"))
48 | beans.add(ItemBean(ItemBean.TYPE_01, "我是A_01类Item${i + 1}"))
49 | beans.add(ItemBean(ItemBean.TYPE_02, "我是A_02类Item${i + 2}"))
50 | }
51 | return beans
52 | }
53 |
54 | override fun onDestroy() {
55 | adapter.clearDataList()
56 | super.onDestroy()
57 | }
58 |
59 | }
60 |
--------------------------------------------------------------------------------
/app/src/main/java/com/tencent/multiadapter/example/ui/MultiItemDemo02Activity.kt:
--------------------------------------------------------------------------------
1 | package com.tencent.multiadapter.example.ui
2 |
3 | import android.os.Bundle
4 | import android.view.LayoutInflater
5 | import androidx.appcompat.app.AppCompatActivity
6 | import androidx.lifecycle.lifecycleScope
7 | import androidx.paging.PagingData
8 | import androidx.recyclerview.widget.DiffUtil
9 | import com.csd.multi.paging.MultiPagingDataAdapter
10 | import com.tencent.multiadapter.databinding.ActivityMultiItemBinding
11 | import com.tencent.multiadapter.example.bean.BeanA
12 | import com.tencent.multiadapter.example.bean.BeanB
13 | import com.tencent.multiadapter.example.bean.BeanC
14 | import com.tencent.multiadapter.example.item.AItemType
15 | import com.tencent.multiadapter.example.item.BItemType
16 | import com.tencent.multiadapter.example.item.CItemType
17 | import kotlinx.coroutines.launch
18 | import java.util.*
19 | /**
20 | * 多 bean 类型对应多样 item。
21 | */
22 | class MultiItemDemo02Activity : AppCompatActivity() {
23 |
24 | private lateinit var adapter: MultiPagingDataAdapter
25 | private val vb by lazy { ActivityMultiItemBinding.inflate(LayoutInflater.from(this)) }
26 |
27 | override fun onCreate(savedInstanceState: Bundle?) {
28 | super.onCreate(savedInstanceState)
29 | setContentView(vb.root)
30 | //初始化ItemType
31 | val aItemType = AItemType()
32 | val bItemType = BItemType()
33 | val cItemType = CItemType()
34 | /*初始化Adapter*/
35 | adapter = MultiPagingDataAdapter(diffCallback = object :
36 | DiffUtil.ItemCallback() {
37 | override fun areItemsTheSame(oldItem: Any, newItem: Any): Boolean = false
38 | override fun areContentsTheSame(oldItem: Any, newItem: Any): Boolean = false
39 |
40 | }, 3)
41 | adapter.clearAllItemTypes()
42 | /*将所有ItemType添加到Adapter中*/
43 | adapter.addItemType(aItemType)
44 | .addItemType(bItemType)
45 | .addItemType(cItemType)
46 | vb.rvList.adapter = adapter
47 | /*设置数据*/
48 | lifecycleScope.launch {
49 | adapter.submitData(PagingData.from(getData()))
50 | }
51 |
52 | }
53 |
54 | /**
55 | * 模拟数据
56 | */
57 | private fun getData(): List {
58 | val beans = ArrayList()
59 | for (i in 0..5) {
60 | beans.add(BeanA( "我是A类Item$i"))
61 | beans.add(BeanB("我是B类Item${i + 1}"))
62 | beans.add(BeanC( "我是C类Item${i + 2}"))
63 | }
64 | return beans
65 | }
66 |
67 | }
68 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
17 |
18 |
25 |
26 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_multi_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_00.xml:
--------------------------------------------------------------------------------
1 |
2 |
12 |
13 |
22 |
23 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_01.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
18 |
19 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_02.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
17 |
18 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_a.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
19 |
20 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_b.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
20 |
21 |
28 |
29 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_c.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
16 |
17 |
24 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/censhengde/rv-multi-itemtype/51ff5779293d1171464d95377a509cbdca61c688/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/censhengde/rv-multi-itemtype/51ff5779293d1171464d95377a509cbdca61c688/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/censhengde/rv-multi-itemtype/51ff5779293d1171464d95377a509cbdca61c688/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/censhengde/rv-multi-itemtype/51ff5779293d1171464d95377a509cbdca61c688/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/censhengde/rv-multi-itemtype/51ff5779293d1171464d95377a509cbdca61c688/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/censhengde/rv-multi-itemtype/51ff5779293d1171464d95377a509cbdca61c688/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/censhengde/rv-multi-itemtype/51ff5779293d1171464d95377a509cbdca61c688/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/censhengde/rv-multi-itemtype/51ff5779293d1171464d95377a509cbdca61c688/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/censhengde/rv-multi-itemtype/51ff5779293d1171464d95377a509cbdca61c688/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/censhengde/rv-multi-itemtype/51ff5779293d1171464d95377a509cbdca61c688/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #6200EE
4 | #3700B3
5 | #03DAC5
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | rv-multi-itemtype
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | ext.kotlin_version = '1.4.30-M1'
5 |
6 | repositories {
7 | jcenter()
8 | mavenCentral()
9 | google()
10 |
11 |
12 | }
13 | dependencies {
14 | classpath 'com.android.tools.build:gradle:3.6.3'
15 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.20"
16 | classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.4'
17 |
18 | // NOTE: Do not place your application dependencies here; they belong
19 | // in the individual module build.gradle files
20 | }
21 | }
22 |
23 | allprojects {
24 | repositories {
25 | google()
26 | mavenCentral()
27 | }
28 | }
29 |
30 | task clean(type: Delete) {
31 | delete rootProject.buildDir
32 | }
33 |
--------------------------------------------------------------------------------
/core/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/core/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'kotlin-android'
3 | apply plugin: 'android-aspectjx'
4 |
5 |
6 | android {
7 | compileSdkVersion 30
8 | buildToolsVersion "30.0.3"
9 | viewBinding {
10 | enabled true
11 | }
12 | defaultConfig {
13 | minSdkVersion 19
14 | targetSdkVersion 30
15 | versionCode 1
16 | versionName "1.0"
17 |
18 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
19 | consumerProguardFiles 'consumer-rules.pro'
20 | }
21 |
22 | buildTypes {
23 | release {
24 | minifyEnabled false
25 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
26 | }
27 | }
28 | compileOptions {
29 | sourceCompatibility = 1.8
30 | targetCompatibility = 1.8
31 | }
32 |
33 | }
34 |
35 | dependencies {
36 | implementation fileTree(dir: 'libs', include: ['*.jar'])
37 |
38 | implementation 'androidx.appcompat:appcompat:1.2.0'
39 | compileOnly 'androidx.recyclerview:recyclerview:1.2.1'
40 |
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/core/consumer-rules.pro:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/censhengde/rv-multi-itemtype/51ff5779293d1171464d95377a509cbdca61c688/core/consumer-rules.pro
--------------------------------------------------------------------------------
/core/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/core/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
--------------------------------------------------------------------------------
/core/src/main/java/com/tencent/lib/multi/MultiAdapter.kt:
--------------------------------------------------------------------------------
1 | package com.tencent.lib.multi
2 |
3 | import android.annotation.SuppressLint
4 | import android.view.ViewGroup
5 | import androidx.recyclerview.widget.AsyncListDiffer
6 | import androidx.recyclerview.widget.DiffUtil
7 | import androidx.recyclerview.widget.RecyclerView
8 | import com.tencent.lib.multi.core.ItemManager
9 | import com.tencent.lib.multi.core.ItemType
10 |
11 | /**
12 | * Author:岑胜德 on 2021/1/6 14:57
13 | *
14 | *
15 | * 说明:未分页的Adapter
16 | */
17 | @Suppress("UNCHECKED_CAST")
18 | open class MultiAdapter(
19 | diffCallback: DiffUtil.ItemCallback<*>? = null,
20 | initialCapacity: Int = 0
21 | ) : RecyclerView.Adapter() {
22 |
23 | private var mAsyncListDiffer: AsyncListDiffer? = null
24 | private var _dataList: List? = null
25 | private val dataList: List
26 | get() {
27 | mAsyncListDiffer?.let {
28 | return it.currentList
29 | }
30 | return _dataList ?: emptyList()
31 | }
32 |
33 | init {
34 | diffCallback?.let {
35 | mAsyncListDiffer = AsyncListDiffer(this, diffCallback as DiffUtil.ItemCallback)
36 | }
37 | }
38 |
39 | private val mManager: ItemManager = object : ItemManager(this, initialCapacity) {
40 | override fun getItem(position: Int): Any? {
41 | return this@MultiAdapter.getItem(position)
42 | }
43 | }
44 |
45 | override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
46 | return mManager.onCreateViewHolder(parent, viewType)
47 | }
48 |
49 | override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {}
50 | override fun onBindViewHolder(
51 | holder: RecyclerView.ViewHolder,
52 | position: Int,
53 | payloads: List
54 | ) {
55 | mManager.onBindViewHolder(holder, position, payloads)
56 | }
57 |
58 | override fun onViewRecycled(holder: RecyclerView.ViewHolder) {
59 | mManager.onViewRecycled(holder)
60 | }
61 |
62 | override fun getItemViewType(position: Int): Int {
63 | return mManager.getItemViewType(position)
64 | }
65 |
66 | override fun getItemId(position: Int): Long {
67 | return mManager.getItemId(position)
68 | }
69 |
70 | override fun onFailedToRecycleView(holder: RecyclerView.ViewHolder): Boolean {
71 | return mManager.onFailedToRecycleView(holder)
72 | }
73 |
74 | override fun onViewAttachedToWindow(holder: RecyclerView.ViewHolder) {
75 | mManager.onViewAttachedToWindow(holder)
76 | }
77 |
78 | override fun onViewDetachedFromWindow(holder: RecyclerView.ViewHolder) {
79 | mManager.onViewAttachedToWindow(holder)
80 | }
81 |
82 | override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
83 | mManager.onAttachedToRecyclerView(recyclerView)
84 | }
85 |
86 | override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
87 | mManager.onDetachedFromRecyclerView(recyclerView)
88 | }
89 |
90 | override fun getItemCount(): Int {
91 | return dataList.size
92 | }
93 |
94 | fun getItem(position: Int): Any? {
95 | return if (position in dataList.indices) {
96 | dataList[position]
97 | } else {
98 | null
99 | }
100 | }
101 |
102 | fun addItemType(type: ItemType<*, *>): MultiAdapter {
103 | mManager.addItemType(type)
104 | return this
105 | }
106 |
107 | fun clearAllItemTypes() {
108 | mManager.clearAllItemTypes()
109 | }
110 |
111 | @SuppressLint("NotifyDataSetChanged")
112 | fun setDataList(list: List) {
113 | if (_dataList != list) {
114 | _dataList = list
115 | }
116 | notifyDataSetChanged()
117 | }
118 |
119 | fun submitList(list: List) {
120 | mAsyncListDiffer?.submitList(list)
121 | }
122 |
123 | fun submitList(list: List, runnable: Runnable) {
124 | mAsyncListDiffer?.submitList(list, runnable)
125 | }
126 |
127 |
128 | fun clearDataList() {
129 | if (_dataList is MutableList) {
130 | (_dataList as MutableList).clear()
131 | }
132 | _dataList = null
133 | mAsyncListDiffer?.currentList?.clear()
134 | }
135 |
136 | fun updateItem(position: Int, block: (bean: T) -> Any?) {
137 | mManager.updateItem(position, block)
138 | }
139 |
140 | }
--------------------------------------------------------------------------------
/core/src/main/java/com/tencent/lib/multi/core/ItemManager.kt:
--------------------------------------------------------------------------------
1 | package com.tencent.lib.multi.core
2 |
3 | import android.util.ArrayMap
4 | import android.view.ViewGroup
5 | import androidx.recyclerview.widget.RecyclerView
6 | import java.util.ArrayList
7 |
8 | /**
9 | * Author:岑胜德 on 2021/1/27 16:33
10 | *
11 | *
12 | * 说明:实现Item多样式的公共逻辑封装。本质上是Adapter 生命周期的代理类,
13 | * 将 Adapter 生命周期分发给了position对应的ItemType。
14 | */
15 | @Suppress("UNCHECKED_CAST")
16 | abstract class ItemManager(
17 | val adapter: RecyclerView.Adapter<*>,
18 | initialCapacity: Int = 0
19 | ) {
20 | // ItemType 池.
21 | private val itemTypePool = ArrayList>(initialCapacity)
22 |
23 | internal val cachePool by lazy(LazyThreadSafetyMode.NONE) { ArrayMap, ReceiverWrapper>() }
24 |
25 | fun getItemViewType(position: Int): Int {
26 | val data = getItem(position)
27 | return findCurrentItemViewType(data, position)
28 | }
29 |
30 | fun getItemId(position: Int): Long {
31 | val data = getItem(position) ?: RecyclerView.NO_ID
32 | findCurrentItemViewType(data, position).also {
33 | return itemTypePool[it].getItemId(data, position)
34 | }
35 | }
36 |
37 |
38 | private fun findCurrentItemViewType(data: Any?, position: Int): Int {
39 | itemTypePool.forEachIndexed { index, item ->
40 | if (item.isMatched(data, position)) {
41 | return index // index 直接作为 itemViewType 返回。
42 | }
43 | }
44 | // 未匹配到对应的 ItemType 则抛异常。
45 | throw java.lang.RuntimeException("ItemType is not found in position:$position")
46 | }
47 |
48 | fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
49 | return itemTypePool[viewType].onCreateViewHolder(parent)
50 | }
51 |
52 | fun onBindViewHolder(
53 | holder: RecyclerView.ViewHolder,
54 | position: Int,
55 | payloads: List
56 | ) {
57 | if (position == RecyclerView.NO_POSITION) {
58 | return
59 | }
60 | val bean = getItem(position) ?: return
61 | val type = itemTypePool[holder.itemViewType]
62 | type.onBindViewHolder(holder, bean, position, payloads)
63 | }
64 |
65 | fun onViewRecycled(holder: RecyclerView.ViewHolder) {
66 | itemTypePool[holder.itemViewType].onViewRecycled(holder)
67 | }
68 |
69 | fun onFailedToRecycleView(holder: RecyclerView.ViewHolder): Boolean {
70 | return itemTypePool[holder.itemViewType].onFailedToRecycleView(holder)
71 | }
72 |
73 | fun onViewAttachedToWindow(holder: RecyclerView.ViewHolder) {
74 | itemTypePool[holder.itemViewType].onViewAttachedToWindow(holder)
75 | }
76 |
77 | fun onViewDetachedFromWindow(holder: RecyclerView.ViewHolder) {
78 | itemTypePool[holder.itemViewType].onViewDetachedFromWindow(holder)
79 | }
80 |
81 | fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
82 | itemTypePool.forEach {
83 | it.onAttachedToRecyclerView(recyclerView)
84 | }
85 | }
86 |
87 | /**
88 | * Called by RecyclerView when it stops observing this Adapter.
89 | *
90 | * @param recyclerView The RecyclerView instance which stopped observing this adapter.
91 | * @see .onAttachedToRecyclerView
92 | */
93 | fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
94 | itemTypePool.forEach {
95 | it.onDetachedFromRecyclerView(recyclerView)
96 | }
97 | }
98 |
99 | abstract fun getItem(position: Int): Any?
100 |
101 | /**
102 | * 添加 ItemType
103 | *
104 | * @param itemType
105 | */
106 | @Suppress("UNCHECKED_CAST")
107 | fun addItemType(itemType: ItemType<*, *>) {
108 | // 关联
109 | itemType.onAttach(this)
110 | itemTypePool.add(itemType as ItemType)
111 | }
112 |
113 | /**
114 | * 在 addItemType 方法之前最好清空一遍 itemTypePool,确保 itemTypePool
115 | * 里一种item类型仅有一个ItemType实例。否则当Activity/Fragment重走创建视图生命周期
116 | * 会导致 ItemType 实例重复添加。
117 | */
118 | fun clearAllItemTypes() {
119 | itemTypePool.clear()
120 | }
121 |
122 | fun updateItem(position: Int, block: (bean: T) -> Any?) {
123 | getItem(position)?.run {
124 | adapter.notifyItemChanged(position, block(this as T))
125 | }
126 | }
127 | }
--------------------------------------------------------------------------------
/core/src/main/java/com/tencent/lib/multi/core/ItemType.kt:
--------------------------------------------------------------------------------
1 | package com.tencent.lib.multi.core
2 |
3 | import android.view.View
4 | import android.view.ViewGroup
5 | import androidx.recyclerview.widget.RecyclerView
6 | import androidx.recyclerview.widget.RecyclerView.NO_ID
7 | import java.lang.reflect.InvocationTargetException
8 | import java.lang.reflect.Method
9 |
10 | /**
11 | * Author:岑胜德 on 2021/2/22 16:23
12 | * 说明:某一种类型 item 的抽象。
13 | */
14 |
15 | abstract class ItemType() {
16 |
17 | companion object {
18 | private const val TAG = "ItemType"
19 | }
20 |
21 | private var mManager: ItemManager? = null
22 |
23 |
24 | internal fun onAttach(manager: ItemManager) {
25 | mManager = manager
26 | }
27 |
28 | protected val manager: ItemManager
29 | get() {
30 | checkNotNull(mManager) { "ItemType $this not attached to an ItemManager." }
31 | return mManager as ItemManager
32 | }
33 |
34 |
35 | /**
36 | * 当前 position 是否与当前 ItemType 匹配。这个方法是实现多样式 item 的关键!
37 | * 如若此方法实现错误,那将导致某position上匹配不到ItemType 而抛异常!
38 | *
39 | * @param bean 当前 position 对应的实体对象
40 | * @param position 当前 position
41 | * @return true 表示匹配;否则不匹配。
42 | */
43 | open fun isMatched(bean: Any?, position: Int): Boolean = true
44 |
45 | /*=========以下是代理 RecyclerView.Adapter 中的方法============================*/
46 |
47 | open fun getItemId(bean: T, position: Int) = NO_ID
48 |
49 | abstract fun onCreateViewHolder(parent: ViewGroup): VH
50 |
51 | open fun onBindViewHolder(
52 | holder: VH, bean: T, position: Int,
53 | payloads: List
54 | ) {
55 | onBindViewHolder(holder, bean, position)
56 | }
57 |
58 | abstract fun onBindViewHolder(holder: VH, bean: T, position: Int)
59 |
60 | open fun onViewRecycled(holder: VH) {}
61 |
62 | open fun onFailedToRecycleView(holder: VH): Boolean = false
63 |
64 | open fun onViewAttachedToWindow(holder: VH) {}
65 |
66 | open fun onViewDetachedFromWindow(holder: VH) {}
67 |
68 | open fun onAttachedToRecyclerView(recyclerView: RecyclerView) {}
69 |
70 | open fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {}
71 |
72 | /*=============================================================================*/
73 |
74 | /**
75 | * 注册 item view 点击事件。
76 | */
77 | fun registerClickEvent(receiver: Any, holder: VH, view: View, method: String) {
78 | view.setOnClickListener { v: View ->
79 | val position = holder.adapterPosition
80 | callTargetMethod(receiver, v, method, position, false)
81 | }
82 | }
83 |
84 | fun registerClickEvent(receiver: Any, position: Int, view: View, method: String) {
85 | view.setOnClickListener { v: View ->
86 | callTargetMethod(receiver, v, method, position, false)
87 | }
88 | }
89 |
90 | /**
91 | * 注册 item view 长点击事件。
92 | */
93 | fun registerLongClickEvent(receiver: Any, holder: VH, view: View, method: String) {
94 | view.setOnLongClickListener { v: View ->
95 | return@setOnLongClickListener callTargetMethod(
96 | receiver,
97 | v,
98 | method,
99 | holder.adapterPosition,
100 | true
101 | )
102 | ?: false
103 | }
104 | }
105 |
106 | fun registerLongClickEvent(receiver: Any, position: Int, view: View, method: String) {
107 | view.setOnLongClickListener { v: View ->
108 | return@setOnLongClickListener callTargetMethod(
109 | receiver,
110 | v, method, position, true
111 | ) ?: false
112 | }
113 | }
114 |
115 | /**
116 | * 反射回调点击方法
117 | *
118 | * @param v
119 | * @param data
120 | * @param position
121 | * @param errMsg
122 | */
123 | private fun callTargetMethod(
124 | receiver: Any,
125 | v: View,
126 | methodName: String,
127 | position: Int,
128 | long: Boolean
129 | ): Boolean? = try {
130 | var result: Boolean? = null
131 | val data = manager.getItem(position)
132 | findWrapper(receiver).also {
133 | findTargetMethod(it, methodName)?.run {
134 | if (!this.isAccessible) {
135 | this.isAccessible = true
136 | }
137 | if (long) {
138 | result = this.invoke(receiver, v, data, position) as Boolean
139 | } else {
140 | this.invoke(receiver, v, data, position)
141 | }
142 | }
143 | }
144 | result
145 | } catch (e: InvocationTargetException) {
146 | e.printStackTrace()
147 | null
148 | } catch (e: IllegalAccessException) {
149 | e.printStackTrace()
150 | null
151 | } catch (e: IllegalAccessException) {
152 | e.printStackTrace()
153 | null
154 | }
155 |
156 |
157 | private fun findWrapper(receiver: Any): ReceiverWrapper {
158 | var wrapper: ReceiverWrapper?
159 | receiver.javaClass.also {
160 | wrapper = manager.cachePool[it]
161 | if (wrapper == null) {
162 | wrapper = ReceiverWrapper(receiver)
163 | manager.cachePool[it] = wrapper
164 | }
165 | }
166 | return wrapper!!
167 | }
168 |
169 | private fun findTargetMethod(wrapper: ReceiverWrapper, methodName: String): Method? {
170 | var method = wrapper.methods[methodName]
171 | if (method == null) {
172 | wrapper.receiver.javaClass.declaredMethods.forEach {
173 | // 需要用户确保目标方法不能被混淆。
174 | if (it.name == methodName) {
175 | method = it
176 | wrapper.methods[methodName] = method
177 | return method
178 | }
179 | }
180 | }
181 | return method
182 | }
183 |
184 | fun updateItem(position: Int, block: (bean: T) -> Any?) {
185 | manager.updateItem(position, block)
186 | }
187 | }
--------------------------------------------------------------------------------
/core/src/main/java/com/tencent/lib/multi/core/ItemViewInflaterFactory.kt:
--------------------------------------------------------------------------------
1 | package com.tencent.lib.multi.core
2 |
3 | import android.content.Context
4 | import android.util.AttributeSet
5 | import android.view.LayoutInflater
6 | import android.view.View
7 | import java.lang.ref.WeakReference
8 |
9 | /**
10 |
11 | * Author:岑胜德 on 2022/5/20 23:51
12 |
13 | * 说明:
14 |
15 | */
16 | open class ItemViewInflaterFactory : LayoutInflater.Factory2 {
17 |
18 | private var viewClickRegistryRef: WeakReference? = null
19 |
20 | fun setViewClickRegistry(viewClickRegistry: ViewClickRegistry) {
21 | if (viewClickRegistryRef?.get() != viewClickRegistry) {
22 | viewClickRegistryRef = WeakReference(viewClickRegistry)
23 | }
24 | }
25 |
26 | override fun onCreateView(
27 | parent: View?,
28 | name: String,
29 | context: Context,
30 | attrs: AttributeSet
31 | ): View? = createView(context, name, attrs)?.also {
32 | viewClickRegistryRef?.get()?.collectView(it, name, attrs)
33 | }
34 |
35 | override fun onCreateView(name: String, context: Context, attrs: AttributeSet): View? = null
36 |
37 | private fun createView(context: Context, name: String, attrs: AttributeSet): View? {
38 | var view: View? = null
39 | try {
40 | if (-1 == name.indexOf('.')) {
41 | if ("View" == name) {
42 | view = LayoutInflater.from(context).createView(name, "android.view.", attrs)
43 | }
44 | if (view == null) {
45 | view = LayoutInflater.from(context).createView(name, "android.widget.", attrs)
46 | }
47 | if (view == null) {
48 | view = LayoutInflater.from(context).createView(name, "android.webkit.", attrs)
49 | }
50 | } else {
51 | view = LayoutInflater.from(context).createView(name, null, attrs)
52 | }
53 | } catch (e: Exception) {
54 | e.printStackTrace()
55 | view = null
56 | }
57 | return view
58 | }
59 | }
--------------------------------------------------------------------------------
/core/src/main/java/com/tencent/lib/multi/core/MultiViewHolder.kt:
--------------------------------------------------------------------------------
1 | package com.tencent.lib.multi.core
2 |
3 | import android.util.SparseArray
4 | import android.view.View
5 | import androidx.annotation.IdRes
6 | import androidx.recyclerview.widget.RecyclerView
7 | import androidx.viewbinding.ViewBinding
8 |
9 | /**
10 | * Author:岑胜德 on 2021/1/6 14:58
11 | *
12 | *
13 | * 说明:
14 | */
15 | open class MultiViewHolder(val vb:ViewBinding) : RecyclerView.ViewHolder(vb.root)
--------------------------------------------------------------------------------
/core/src/main/java/com/tencent/lib/multi/core/ReceiverWrapper.kt:
--------------------------------------------------------------------------------
1 | package com.tencent.lib.multi.core
2 |
3 | import android.util.ArrayMap
4 | import java.lang.reflect.Method
5 |
6 | /**
7 |
8 | * Author:岑胜德 on 2022/4/7 11:13
9 |
10 | * 说明:
11 |
12 | */
13 | internal class ReceiverWrapper(val receiver:Any) {
14 | val methods by lazy(LazyThreadSafetyMode.NONE) {
15 | ArrayMap()
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/core/src/main/java/com/tencent/lib/multi/core/SimpleItemType.kt:
--------------------------------------------------------------------------------
1 | package com.tencent.lib.multi.core
2 |
3 | import android.annotation.SuppressLint
4 | import android.content.Context
5 | import android.util.AttributeSet
6 | import android.view.LayoutInflater
7 | import android.view.View
8 | import android.view.ViewGroup
9 | import androidx.collection.SimpleArrayMap
10 | import androidx.core.view.LayoutInflaterCompat
11 | import androidx.recyclerview.widget.RecyclerView
12 | import androidx.viewbinding.ViewBinding
13 | import com.tencent.lib.itemType.R
14 | import java.lang.reflect.Method
15 | import java.lang.reflect.ParameterizedType
16 | import java.lang.reflect.Type
17 | import java.util.*
18 |
19 | /**
20 | * Author:岑胜德 on 2021/8/30 10:58
21 | *
22 | *
23 | * 说明:支持 ViewBinding用法。
24 | */
25 | @Suppress("UNCHECKED_CAST")
26 | abstract class SimpleItemType(private var clickEventReceiver: Any? = null) :
27 | ItemType() {
28 |
29 | private var mBindMethod: Method? = null
30 | protected var viewClickRegistry: ViewClickRegistry? = null
31 |
32 | /**
33 | * 绑定点击事件接收对象。
34 | */
35 | fun bind(clickEventReceiver: Any) {
36 | this.clickEventReceiver = clickEventReceiver
37 | }
38 |
39 | final override fun onCreateViewHolder(parent: ViewGroup): MultiViewHolder {
40 | val vb = onCreateViewBinding(parent, getLayoutInflater(parent))
41 | val holder = MultiViewHolder(vb)
42 | onViewHolderCreated(holder, vb)
43 | return holder
44 | }
45 |
46 | /**
47 | * 将来有可能需要配合动态换肤框架使用,届时有可能需要重写此方法。
48 | */
49 | protected open fun getLayoutInflater(parent: ViewGroup): LayoutInflater =
50 | LayoutInflater.from(parent.context).also {
51 | if (viewClickRegistry == null) {
52 | viewClickRegistry =
53 | ViewClickRegistry(this as ItemType)
54 | }
55 | // 如果已经设置了 ItemViewInflaterFactory,则更新 ViewClickRegistry
56 | if (it.factory2 is ItemViewInflaterFactory) {
57 | (it.factory2 as ItemViewInflaterFactory).setViewClickRegistry(viewClickRegistry!!)
58 | } else {
59 | forceSetFactory2(it, ItemViewInflaterFactory().also {
60 | it.setViewClickRegistry(viewClickRegistry!!)
61 | })
62 | }
63 | }
64 |
65 |
66 | protected open fun onCreateViewBinding(parent: ViewGroup, inflater: LayoutInflater): VB {
67 | var vb: VB? = null
68 | try {
69 | if (mBindMethod == null) {
70 | var clazz: Class<*>? = this.javaClass
71 | var type: Type? = clazz?.genericSuperclass
72 | while (type !is ParameterizedType) {
73 | if (clazz == Objects::class.java) {
74 | break
75 | }
76 | clazz = clazz?.superclass
77 | type = clazz?.genericSuperclass
78 | }
79 |
80 | val p = type as ParameterizedType
81 | // 孙类以下如果不透传 VB 泛型参数到其父类就会获取Class对象失败,
82 | // 此时解决方案就是全盘重写 onCreateViewBinding(ViewGroup parent) 方法,手动创建
83 | // ViewBinding 实现类的实例
84 | val c = p.actualTypeArguments[1] as Class
85 | // Gradle 开启 ViewBinding 后会自动生成 ViewBinding 的实现类,其中就有 inflate 静态方法,
86 | // 该方法用于创建 ViewBinding 实现类的实例。(注意配置忽略 ViewBinding 混淆!!!)
87 | mBindMethod = c.getMethod(
88 | "inflate",
89 | LayoutInflater::class.java,
90 | ViewGroup::class.java,
91 | Boolean::class.javaPrimitiveType
92 | )
93 | }
94 | vb = mBindMethod!!.invoke(null, inflater, parent, false) as VB
95 | } catch (e: Exception) {
96 | e.printStackTrace()
97 | throw IllegalStateException("反射创建 ViewBinding 失败:" + e.message)
98 | }
99 | return vb
100 | }
101 |
102 | protected open fun onViewHolderCreated(
103 | holder: MultiViewHolder,
104 | vb: VB
105 | ) {
106 | clickEventReceiver ?: return
107 | viewClickRegistry?.register(clickEventReceiver!!, holder)
108 | viewClickRegistry?.clearAllNeedRegisterViews()
109 | }
110 |
111 | /**
112 | * 加 final修饰向子类屏蔽此方法。
113 | *
114 | * @param holder
115 | * @param bean
116 | * @param position
117 | * @param payloads
118 | */
119 | final override fun onBindViewHolder(
120 | holder: MultiViewHolder, bean: T, position: Int,
121 | payloads: List
122 | ) {
123 | /*这里直接 将ViewHolder 转换成 ViewBinding,让子类获取控件代码更简洁!*/
124 | onBindView(holder.vb as VB, bean, position, payloads)
125 | }
126 |
127 |
128 | final override fun onBindViewHolder(holder: MultiViewHolder, bean: T, position: Int) {
129 | }
130 |
131 | protected open fun onBindView(vb: VB, bean: T, position: Int, payloads: List) {
132 | onBindView(vb, bean, position)
133 | }
134 |
135 | protected abstract fun onBindView(vb: VB, bean: T, position: Int)
136 |
137 |
138 | /**
139 | * 强行设置 Factory2。
140 | * 由于在 AppCompatActivity 中 LayoutInflater 已经被系统设置过了 Factory2,
141 | * 这里再次通过 LayoutInflater.setFactory2(...)方法设置Factory2会抛异常,故只能反射强行设置。
142 | */
143 | @SuppressLint("DiscouragedPrivateApi")
144 | protected fun forceSetFactory2(inflater: LayoutInflater, factory2: LayoutInflater.Factory2) {
145 | val inflaterClass = LayoutInflater::class.java
146 | try {
147 | val mFactory2 = inflaterClass.getDeclaredField("mFactory2")
148 | mFactory2.isAccessible = true
149 | mFactory2.set(inflater, factory2)
150 | } catch (e: IllegalAccessException) {
151 | e.printStackTrace()
152 | } catch (e: NoSuchFieldException) {
153 | e.printStackTrace()
154 | }
155 | }
156 |
157 | }
--------------------------------------------------------------------------------
/core/src/main/java/com/tencent/lib/multi/core/ViewClickRegistry.kt:
--------------------------------------------------------------------------------
1 | package com.tencent.lib.multi.core
2 |
3 | import android.util.AttributeSet
4 | import android.util.Log
5 | import android.view.View
6 | import androidx.collection.SimpleArrayMap
7 | import androidx.recyclerview.widget.RecyclerView
8 | import com.tencent.lib.itemType.R
9 |
10 | /**
11 |
12 | * Author:岑胜德 on 2022/5/20 23:32
13 |
14 | * 说明:
15 |
16 | */
17 | class ViewClickRegistry(private val itemType: ItemType) {
18 | companion object {
19 | private const val TAG = "ViewClickRegistry"
20 | }
21 | // xml文件中声明了 linkClick 属性的View
22 | private var needRegisterClickEventViews: SimpleArrayMap? = null
23 |
24 | // xml文件中声明了 linkLongClick 属性的View
25 | private var needRegisterLongClickEventViews: SimpleArrayMap? = null
26 |
27 | fun register(clickEventReceiver: Any, holder: RecyclerView.ViewHolder) {
28 | // 注册点击事件
29 | repeat(needRegisterClickEventViews?.size() ?: 0) {
30 | needRegisterClickEventViews?.keyAt(it)?.apply {
31 | needRegisterClickEventViews?.valueAt(it)?.let { name ->
32 | itemType.registerClickEvent(clickEventReceiver, holder, this, name)
33 | }
34 | }
35 |
36 | }
37 | // 注册长点击事件
38 | repeat(needRegisterLongClickEventViews?.size() ?: 0) {
39 | needRegisterLongClickEventViews?.keyAt(it)?.apply {
40 | needRegisterLongClickEventViews?.valueAt(it)?.let { name ->
41 | itemType.registerLongClickEvent(clickEventReceiver, holder, this, name)
42 | }
43 | }
44 | }
45 | }
46 |
47 | fun collectView(view: View, name: String, attrs: AttributeSet) {
48 | val a = view.context.obtainStyledAttributes(attrs, R.styleable.ItemView)
49 | a.getString(R.styleable.ItemView_linkClick)?.apply {
50 | if (needRegisterClickEventViews == null) {
51 | needRegisterClickEventViews = SimpleArrayMap()
52 | }
53 | needRegisterClickEventViews?.put(view, this) // 将需要注册点击事件的 View 收集起来。
54 |
55 | }
56 | a.getString(R.styleable.ItemView_linkLongClick)?.apply {
57 | if (needRegisterLongClickEventViews == null) {
58 | needRegisterLongClickEventViews = SimpleArrayMap()
59 | }
60 | needRegisterLongClickEventViews?.put(view, this)
61 | }
62 | a.recycle()
63 | }
64 |
65 | fun clearAllNeedRegisterViews() {
66 | needRegisterClickEventViews?.clear()
67 | needRegisterLongClickEventViews?.clear()
68 | }
69 | }
--------------------------------------------------------------------------------
/core/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app's APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Automatically convert third-party libraries to use AndroidX
19 | android.enableJetifier=true
20 |
21 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/censhengde/rv-multi-itemtype/51ff5779293d1171464d95377a509cbdca61c688/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Sat Dec 25 00:27:17 GMT+08:00 2021
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/image/MultiAdapter问题反馈群群聊二维码.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/censhengde/rv-multi-itemtype/51ff5779293d1171464d95377a509cbdca61c688/image/MultiAdapter问题反馈群群聊二维码.png
--------------------------------------------------------------------------------
/image/单bean类型对应多样item类型.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/censhengde/rv-multi-itemtype/51ff5779293d1171464d95377a509cbdca61c688/image/单bean类型对应多样item类型.jpg
--------------------------------------------------------------------------------
/image/多bean类型对应多样item类型.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/censhengde/rv-multi-itemtype/51ff5779293d1171464d95377a509cbdca61c688/image/多bean类型对应多样item类型.jpg
--------------------------------------------------------------------------------
/paging/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/paging/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id 'com.android.library'
3 | id 'kotlin-android'
4 | }
5 |
6 | android {
7 | compileSdkVersion 30
8 | buildToolsVersion "30.0.3"
9 | defaultConfig {
10 | minSdkVersion 19
11 | targetSdkVersion 30
12 | versionCode 1
13 | versionName "1.0"
14 |
15 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
16 | }
17 |
18 | buildTypes {
19 | release {
20 | minifyEnabled false
21 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
22 | }
23 | }
24 | compileOptions {
25 | sourceCompatibility JavaVersion.VERSION_1_8
26 | targetCompatibility JavaVersion.VERSION_1_8
27 | }
28 | kotlinOptions {
29 | jvmTarget = '1.8'
30 | }
31 | }
32 |
33 | dependencies {
34 |
35 | implementation 'androidx.core:core-ktx:1.6.0'
36 | implementation project(path: ':core')
37 | compileOnly ('androidx.paging:paging-runtime:3.0.1'){
38 | exclude group: "org.jetbrains.kotlinx"
39 | }
40 | compileOnly("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.2")
41 | compileOnly("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2")
42 | }
--------------------------------------------------------------------------------
/paging/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
--------------------------------------------------------------------------------
/paging/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
--------------------------------------------------------------------------------
/paging/src/main/java/com/csd/multi/paging/MultiPagingDataAdapter.kt:
--------------------------------------------------------------------------------
1 | package com.csd.multi.paging
2 |
3 | import android.view.ViewGroup
4 | import androidx.paging.PagingDataAdapter
5 | import androidx.recyclerview.widget.DiffUtil
6 | import androidx.recyclerview.widget.RecyclerView
7 | import com.tencent.lib.multi.core.ItemType
8 | import com.tencent.lib.multi.core.ItemManager
9 |
10 | /**
11 |
12 | * Author:岑胜德 on 2021/4/25 19:08
13 |
14 | * 说明:
15 |
16 | */
17 | @Suppress("UNCHECKED_CAST")
18 | open class MultiPagingDataAdapter(
19 | diffCallback: DiffUtil.ItemCallback<*>,
20 | initialCapacity: Int = 0
21 | ) : PagingDataAdapter(diffCallback as DiffUtil.ItemCallback) {
22 |
23 | private val mDelegate = object : ItemManager(this, initialCapacity) {
24 | override fun getItem(position: Int): Any? {
25 | return this@MultiPagingDataAdapter.getItem(position)
26 | }
27 | }
28 |
29 | override fun getItemViewType(position: Int): Int {
30 | return mDelegate.getItemViewType(position)
31 | }
32 |
33 | override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
34 | return mDelegate.onCreateViewHolder(parent, viewType)
35 | }
36 |
37 | override fun onBindViewHolder(
38 | holder: RecyclerView.ViewHolder,
39 | position: Int,
40 | payloads: List
41 | ) {
42 | mDelegate.onBindViewHolder(holder, position, payloads)
43 | }
44 |
45 | final override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
46 |
47 | }
48 |
49 | override fun onViewRecycled(holder: RecyclerView.ViewHolder) {
50 | mDelegate.onViewRecycled(holder)
51 | }
52 |
53 | override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
54 | mDelegate.onAttachedToRecyclerView(recyclerView)
55 | }
56 |
57 | override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
58 | mDelegate.onDetachedFromRecyclerView(recyclerView)
59 | }
60 |
61 | override fun onViewAttachedToWindow(holder: RecyclerView.ViewHolder) {
62 | mDelegate.onViewAttachedToWindow(holder)
63 | }
64 |
65 | override fun onViewDetachedFromWindow(holder: RecyclerView.ViewHolder) {
66 | mDelegate.onViewDetachedFromWindow(holder)
67 | }
68 |
69 | override fun onFailedToRecycleView(holder: RecyclerView.ViewHolder): Boolean {
70 | return mDelegate.onFailedToRecycleView(holder)
71 | }
72 |
73 | fun addItemType(itemType: ItemType<*, *>): MultiPagingDataAdapter {
74 | mDelegate.addItemType(itemType)
75 | return this
76 | }
77 |
78 | fun clearAllItemTypes() {
79 | mDelegate.clearAllItemTypes()
80 | }
81 |
82 | fun updateItem(position: Int, block: (bean: T) -> Any?) {
83 | mDelegate.updateItem(position, block)
84 | }
85 | }
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | rootProject.name='rv-multi-itemtype'
2 | include ':app'
3 | include ':core'
4 | include ':paging'
5 |
--------------------------------------------------------------------------------