From e8c6be2b42f57d90101276318e8b009dccc4e452 Mon Sep 17 00:00:00 2001 From: qinhao Date: Fri, 16 Apr 2021 11:47:10 +0800 Subject: [PATCH] Feat:1.Refactor RcvBaseAdapter by kotlin,and add demo. --- .idea/caches/build_file_checksums.ser | Bin 541 -> 594 bytes .idea/compiler.xml | 6 + .idea/gradle.xml | 4 + .idea/jarRepositories.xml | 40 +++ .idea/misc.xml | 2 +- app/build.gradle | 37 ++- app/src/main/AndroidManifest.xml | 9 +- .../rcvbaseadapterdemo/IntegerItemView.java | 25 -- .../rcvbaseadapterdemo/MainActivity.java | 38 --- .../rcvbaseadapterdemo/MessageItemView1.java | 34 --- .../rcvbaseadapterdemo/MessageItemView2.java | 34 --- .../rcvbaseadapterdemo/StringItemView.java | 25 -- .../rcvbaseadapterdemo/TestAdapter.java | 14 - .../adapter/RcvBaseAdapter.java | 109 -------- .../adapter/RcvMultipleBaseAdapter.java | 66 ----- .../adapter/RcvSingleBaseAdapter.java | 16 -- .../entity/MessageEntity.java | 39 --- .../holder/BaseViewHolder.java | 193 -------------- .../itemview/BaseItemView.java | 53 ---- .../listener/OnItemClickListener.java | 14 - .../kotlin/com/qinshou/demo/MainActivity.kt | 24 ++ .../com/qinshou/demo/ManyToManyActivity.kt | 55 ++++ .../main/kotlin/com/qinshou/demo/NewsBean.kt | 39 +++ .../com/qinshou/demo/OneToManyActivity.kt | 58 ++++ .../com/qinshou/demo/OneToOneActivity.kt | 35 +++ .../com/qinshou/demo/SpecialItemActivity.kt | 108 ++++++++ .../layout/activity/layout/activity_main.xml | 57 ++++ .../activity/layout/activity_many_to_many.xml | 17 ++ .../activity/layout/activity_one_to_many.xml | 17 ++ .../activity/layout/activity_one_to_one.xml | 17 ++ app/src/main/res/layout/activity_main.xml | 14 - .../layout_empty_view_of_rcv_one_to_one.xml | 24 ++ .../layout_footer_view_of_rcv_one_to_one.xml | 15 ++ .../layout_header_view_of_rcv_one_to_one.xml | 15 ++ .../layout/item_rcv_integer.xml} | 5 +- .../res/layout/item/layout/item_rcv_news.xml | 74 ++++++ .../layout/item_rcv_one_to_many_1.xml} | 3 +- .../layout/item_rcv_one_to_many_2.xml} | 3 +- .../layout/item_rcv_string.xml} | 3 +- .../main/res/layout/item_rv_test_integer.xml | 16 -- build.gradle | 8 +- gradle.properties | 6 + gradle/wrapper/gradle-wrapper.properties | 4 +- rcvbaseadapter/.gitignore | 1 + rcvbaseadapter/build.gradle | 51 ++++ rcvbaseadapter/consumer-rules.pro | 0 rcvbaseadapter/proguard-rules.pro | 21 ++ .../qinshou/rcv/ExampleInstrumentedTest.kt | 24 ++ rcvbaseadapter/src/main/AndroidManifest.xml | 5 + .../com/qinshou/rcv/adapter/RcvBaseAdapter.kt | 110 ++++++++ .../qinshou/rcv/adapter/RcvMultipleAdapter.kt | 249 ++++++++++++++++++ .../qinshou/rcv/adapter/RcvSimpleAdapter.kt | 12 + .../com/qinshou/rcv/itemview/BaseItemView.kt | 39 +++ .../com/qinshou/rcv/itemview/EmptyItemView.kt | 26 ++ .../qinshou/rcv/itemview/FooterItemView.kt | 38 +++ .../qinshou/rcv/itemview/HeaderItemView.kt | 33 +++ .../qinshou/rcv/itemview/SkeletonItemView.kt | 49 ++++ .../rcv/listener/OnItemClickListener.kt | 14 + .../rcv/listener/OnItemLongClickListener.kt | 14 + .../qinshou/rcv/viewholder/BaseViewHolder.kt | 214 +++++++++++++++ .../java/com/qinshou/rcv/ExampleUnitTest.kt | 17 ++ settings.gradle | 1 + 62 files changed, 1578 insertions(+), 715 deletions(-) create mode 100644 .idea/compiler.xml create mode 100644 .idea/jarRepositories.xml delete mode 100644 app/src/main/java/com/qinshou/rcvbaseadapterdemo/IntegerItemView.java delete mode 100644 app/src/main/java/com/qinshou/rcvbaseadapterdemo/MainActivity.java delete mode 100644 app/src/main/java/com/qinshou/rcvbaseadapterdemo/MessageItemView1.java delete mode 100644 app/src/main/java/com/qinshou/rcvbaseadapterdemo/MessageItemView2.java delete mode 100644 app/src/main/java/com/qinshou/rcvbaseadapterdemo/StringItemView.java delete mode 100644 app/src/main/java/com/qinshou/rcvbaseadapterdemo/TestAdapter.java delete mode 100644 app/src/main/java/com/qinshou/rcvbaseadapterdemo/adapter/RcvBaseAdapter.java delete mode 100644 app/src/main/java/com/qinshou/rcvbaseadapterdemo/adapter/RcvMultipleBaseAdapter.java delete mode 100644 app/src/main/java/com/qinshou/rcvbaseadapterdemo/adapter/RcvSingleBaseAdapter.java delete mode 100644 app/src/main/java/com/qinshou/rcvbaseadapterdemo/entity/MessageEntity.java delete mode 100644 app/src/main/java/com/qinshou/rcvbaseadapterdemo/holder/BaseViewHolder.java delete mode 100644 app/src/main/java/com/qinshou/rcvbaseadapterdemo/itemview/BaseItemView.java delete mode 100644 app/src/main/java/com/qinshou/rcvbaseadapterdemo/listener/OnItemClickListener.java create mode 100644 app/src/main/kotlin/com/qinshou/demo/MainActivity.kt create mode 100644 app/src/main/kotlin/com/qinshou/demo/ManyToManyActivity.kt create mode 100644 app/src/main/kotlin/com/qinshou/demo/NewsBean.kt create mode 100644 app/src/main/kotlin/com/qinshou/demo/OneToManyActivity.kt create mode 100644 app/src/main/kotlin/com/qinshou/demo/OneToOneActivity.kt create mode 100644 app/src/main/kotlin/com/qinshou/demo/SpecialItemActivity.kt create mode 100644 app/src/main/res/layout/activity/layout/activity_main.xml create mode 100644 app/src/main/res/layout/activity/layout/activity_many_to_many.xml create mode 100644 app/src/main/res/layout/activity/layout/activity_one_to_many.xml create mode 100644 app/src/main/res/layout/activity/layout/activity_one_to_one.xml delete mode 100644 app/src/main/res/layout/activity_main.xml create mode 100644 app/src/main/res/layout/include/layout/layout_empty_view_of_rcv_one_to_one.xml create mode 100644 app/src/main/res/layout/include/layout/layout_footer_view_of_rcv_one_to_one.xml create mode 100644 app/src/main/res/layout/include/layout/layout_header_view_of_rcv_one_to_one.xml rename app/src/main/res/layout/{item_rv_test.xml => item/layout/item_rcv_integer.xml} (81%) create mode 100644 app/src/main/res/layout/item/layout/item_rcv_news.xml rename app/src/main/res/layout/{item_rv_test_message1.xml => item/layout/item_rcv_one_to_many_1.xml} (86%) rename app/src/main/res/layout/{item_rv_test_message2.xml => item/layout/item_rcv_one_to_many_2.xml} (86%) rename app/src/main/res/layout/{item_rv_test_string.xml => item/layout/item_rcv_string.xml} (87%) delete mode 100644 app/src/main/res/layout/item_rv_test_integer.xml create mode 100644 rcvbaseadapter/.gitignore create mode 100644 rcvbaseadapter/build.gradle create mode 100644 rcvbaseadapter/consumer-rules.pro create mode 100644 rcvbaseadapter/proguard-rules.pro create mode 100644 rcvbaseadapter/src/androidTest/java/com/qinshou/rcv/ExampleInstrumentedTest.kt create mode 100644 rcvbaseadapter/src/main/AndroidManifest.xml create mode 100644 rcvbaseadapter/src/main/java/com/qinshou/rcv/adapter/RcvBaseAdapter.kt create mode 100644 rcvbaseadapter/src/main/java/com/qinshou/rcv/adapter/RcvMultipleAdapter.kt create mode 100644 rcvbaseadapter/src/main/java/com/qinshou/rcv/adapter/RcvSimpleAdapter.kt create mode 100644 rcvbaseadapter/src/main/java/com/qinshou/rcv/itemview/BaseItemView.kt create mode 100644 rcvbaseadapter/src/main/java/com/qinshou/rcv/itemview/EmptyItemView.kt create mode 100644 rcvbaseadapter/src/main/java/com/qinshou/rcv/itemview/FooterItemView.kt create mode 100644 rcvbaseadapter/src/main/java/com/qinshou/rcv/itemview/HeaderItemView.kt create mode 100644 rcvbaseadapter/src/main/java/com/qinshou/rcv/itemview/SkeletonItemView.kt create mode 100644 rcvbaseadapter/src/main/java/com/qinshou/rcv/listener/OnItemClickListener.kt create mode 100644 rcvbaseadapter/src/main/java/com/qinshou/rcv/listener/OnItemLongClickListener.kt create mode 100644 rcvbaseadapter/src/main/java/com/qinshou/rcv/viewholder/BaseViewHolder.kt create mode 100644 rcvbaseadapter/src/test/java/com/qinshou/rcv/ExampleUnitTest.kt diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index 96063c1e809330cf668c1abe939621c3af9f29fd..e7e7e51e2ce2570e41192c57b48bc28d12c6d6f5 100644 GIT binary patch delta 227 zcmbQsa*1Wabk>R+N$c-Voa4jDKJlfNO=|!3+Z)dxJ5}bdGZvk`jwk6H^ikN>Ynrl1ejkQZOtw4mfJmrL6n;nQ!hYe`&GvB@BX-KQc-Srk_d; TShaTIt49*KtovRbEw2CoA~;;* delta 174 zcmV;f08#(a1f2wsmjz~6^8gc(oJ;`*k?ATtBxJe=MI0q_j%`1|W)9((bN~-?Wps3D zZfA2YXL4a=Y-M$E0Db@l0000Gp4ni&Jl_XyEnqGhS3<*~lTQH|5KFc!C=V1|h<^x! zCwpp}OOtp39TR-a3wr2_9UnS&Nxh#5vT<|(5tG0H8zu9N1FF1WrB1RvW;5o^5Mp!y cEJHe6RdZ!>b6ibwQE6^-Xm53sw*fQ(csz+e7XSbN diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..61a9130 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml index 7ac24c7..9181e0c 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -1,17 +1,21 @@ + diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..60a4e99 --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 99202cc..cc51e58 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -25,7 +25,7 @@ - + diff --git a/app/build.gradle b/app/build.gradle index 436fcec..4efc60a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,11 +1,15 @@ apply plugin: 'com.android.application' +apply plugin: 'kotlin-android' +apply plugin: 'kotlin-android-extensions' android { - compileSdkVersion 26 + compileSdkVersion 30 + buildToolsVersion "30.0.2" + defaultConfig { - applicationId "demo.com.qinshou.demo" - minSdkVersion 15 - targetSdkVersion 26 + applicationId "com.qinshou.demo" + minSdkVersion 21 + targetSdkVersion 30 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" @@ -16,14 +20,29 @@ android { proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } + // 资源目录 + sourceSets { + main { + res.srcDirs('src/main/res/', + 'src/main/res/layout/activity', + 'src/main/res/layout/include', + 'src/main/res/layout/item') + java.srcDirs('src/main/java' + , 'src/main/kotlin') + } + } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) - implementation 'com.android.support:appcompat-v7:26.1.0' - implementation 'com.android.support.constraint:constraint-layout:1.1.3' + implementation 'androidx.appcompat:appcompat:1.2.0' + implementation project(path: ':rcvbaseadapter') testImplementation 'junit:junit:4.12' - androidTestImplementation 'com.android.support.test:runner:1.0.2' - androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' - implementation 'com.android.support:design:26.1.0' + androidTestImplementation 'androidx.test.ext:junit:1.1.2' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' + implementation 'com.google.android.material:material:1.3.0' + // Kotlin + implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" + implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.1' + implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.1' } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index d990651..bcf1c7f 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,6 +1,6 @@ + package="com.qinshou.demo"> - + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/qinshou/rcvbaseadapterdemo/IntegerItemView.java b/app/src/main/java/com/qinshou/rcvbaseadapterdemo/IntegerItemView.java deleted file mode 100644 index db75555..0000000 --- a/app/src/main/java/com/qinshou/rcvbaseadapterdemo/IntegerItemView.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.qinshou.rcvbaseadapterdemo; - -import android.content.Context; - -import com.qinshou.rcvbaseadapterdemo.holder.BaseViewHolder; -import com.qinshou.rcvbaseadapterdemo.itemview.BaseItemView; - -import demo.com.qinshou.rcvbaseadapterdemo.R; - -/** - * Description: - * Created by 禽兽先生 - * Created on 2018/10/9 - */ -public class IntegerItemView extends BaseItemView { - - public IntegerItemView(Context context) { - super(context, R.layout.item_rv_test_integer); - } - - @Override - public void bindViewHolder(BaseViewHolder holder, Integer itemData, int position) { - holder.setTvText(R.id.tv_test_integer, "Integer 的 ItemView" + itemData); - } -} diff --git a/app/src/main/java/com/qinshou/rcvbaseadapterdemo/MainActivity.java b/app/src/main/java/com/qinshou/rcvbaseadapterdemo/MainActivity.java deleted file mode 100644 index 6850323..0000000 --- a/app/src/main/java/com/qinshou/rcvbaseadapterdemo/MainActivity.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.qinshou.rcvbaseadapterdemo; - -import android.os.Bundle; -import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.LinearLayoutManager; -import android.support.v7.widget.RecyclerView; - -import com.qinshou.rcvbaseadapterdemo.entity.MessageEntity; - -import java.util.ArrayList; -import java.util.List; - -import demo.com.qinshou.rcvbaseadapterdemo.R; - -public class MainActivity extends AppCompatActivity { - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main); - RecyclerView rvTest = (RecyclerView) findViewById(R.id.rv_test); - TestAdapter testAdapter = new TestAdapter(this); - rvTest.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)); - rvTest.setAdapter(testAdapter); - testAdapter.setDataList(getList()); - } - - private List getList() { - List list = new ArrayList<>(); - for (int i = 0; i < 100; i++) { - MessageEntity messageEntity = new MessageEntity(); - messageEntity.setMessage("你好"); - messageEntity.setUserType(i % 2); - list.add(messageEntity); - } - return list; - } -} diff --git a/app/src/main/java/com/qinshou/rcvbaseadapterdemo/MessageItemView1.java b/app/src/main/java/com/qinshou/rcvbaseadapterdemo/MessageItemView1.java deleted file mode 100644 index 8287f79..0000000 --- a/app/src/main/java/com/qinshou/rcvbaseadapterdemo/MessageItemView1.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.qinshou.rcvbaseadapterdemo; - -import android.content.Context; - -import com.qinshou.rcvbaseadapterdemo.entity.MessageEntity; -import com.qinshou.rcvbaseadapterdemo.holder.BaseViewHolder; -import com.qinshou.rcvbaseadapterdemo.itemview.BaseItemView; - -import demo.com.qinshou.rcvbaseadapterdemo.R; - -/** - * Description: - * Created by 禽兽先生 - * Created on 2018/10/9 - */ -public class MessageItemView1 extends BaseItemView { - - public MessageItemView1(Context context) { - super(context, R.layout.item_rv_test_message1); - } - - @Override - public boolean isForViewType(MessageEntity item, int position) { - if (item.getUserType() == 0) { - return true; - } - return false; - } - - @Override - public void bindViewHolder(BaseViewHolder holder, MessageEntity itemData, int position) { - holder.setTvText(R.id.tv_test_message1, "发出的消息" + itemData.getMessage()); - } -} diff --git a/app/src/main/java/com/qinshou/rcvbaseadapterdemo/MessageItemView2.java b/app/src/main/java/com/qinshou/rcvbaseadapterdemo/MessageItemView2.java deleted file mode 100644 index 332fdbf..0000000 --- a/app/src/main/java/com/qinshou/rcvbaseadapterdemo/MessageItemView2.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.qinshou.rcvbaseadapterdemo; - -import android.content.Context; - -import com.qinshou.rcvbaseadapterdemo.entity.MessageEntity; -import com.qinshou.rcvbaseadapterdemo.holder.BaseViewHolder; -import com.qinshou.rcvbaseadapterdemo.itemview.BaseItemView; - -import demo.com.qinshou.rcvbaseadapterdemo.R; - -/** - * Description: - * Created by 禽兽先生 - * Created on 2018/10/9 - */ -public class MessageItemView2 extends BaseItemView { - - public MessageItemView2(Context context) { - super(context, R.layout.item_rv_test_message2); - } - - @Override - public boolean isForViewType(MessageEntity item, int position) { - if (item.getUserType() == 1) { - return true; - } - return false; - } - - @Override - public void bindViewHolder(BaseViewHolder holder, MessageEntity itemData, int position) { - holder.setTvText(R.id.tv_test_message2, "接收的消息" + itemData.getMessage()); - } -} diff --git a/app/src/main/java/com/qinshou/rcvbaseadapterdemo/StringItemView.java b/app/src/main/java/com/qinshou/rcvbaseadapterdemo/StringItemView.java deleted file mode 100644 index f8c5442..0000000 --- a/app/src/main/java/com/qinshou/rcvbaseadapterdemo/StringItemView.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.qinshou.rcvbaseadapterdemo; - -import android.content.Context; - -import com.qinshou.rcvbaseadapterdemo.holder.BaseViewHolder; -import com.qinshou.rcvbaseadapterdemo.itemview.BaseItemView; - -import demo.com.qinshou.rcvbaseadapterdemo.R; - -/** - * Description: - * Created by 禽兽先生 - * Created on 2018/10/9 - */ -public class StringItemView extends BaseItemView { - - public StringItemView(Context context) { - super(context, R.layout.item_rv_test_string); - } - - @Override - public void bindViewHolder(BaseViewHolder holder, String itemData, int position) { - holder.setTvText(R.id.tv_test_string, "String 的 ItemView" + itemData); - } -} diff --git a/app/src/main/java/com/qinshou/rcvbaseadapterdemo/TestAdapter.java b/app/src/main/java/com/qinshou/rcvbaseadapterdemo/TestAdapter.java deleted file mode 100644 index f98a013..0000000 --- a/app/src/main/java/com/qinshou/rcvbaseadapterdemo/TestAdapter.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.qinshou.rcvbaseadapterdemo; - -import android.content.Context; - -import com.qinshou.rcvbaseadapterdemo.adapter.RcvMultipleBaseAdapter; - -public class TestAdapter extends RcvMultipleBaseAdapter { - - public TestAdapter(Context context) { - super(context); - addItemView(new MessageItemView1(context)); - addItemView(new MessageItemView2(context)); - } -} diff --git a/app/src/main/java/com/qinshou/rcvbaseadapterdemo/adapter/RcvBaseAdapter.java b/app/src/main/java/com/qinshou/rcvbaseadapterdemo/adapter/RcvBaseAdapter.java deleted file mode 100644 index ac18cd8..0000000 --- a/app/src/main/java/com/qinshou/rcvbaseadapterdemo/adapter/RcvBaseAdapter.java +++ /dev/null @@ -1,109 +0,0 @@ -package com.qinshou.rcvbaseadapterdemo.adapter; - -import android.content.Context; -import android.support.v7.widget.RecyclerView; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; - -import com.qinshou.rcvbaseadapterdemo.holder.BaseViewHolder; -import com.qinshou.rcvbaseadapterdemo.listener.OnItemClickListener; - -import java.util.ArrayList; -import java.util.List; - -/** - * Description:RecyclerView 的适配器的基类 - * Created by 禽兽先生 - * Created on 2017/11/22 - */ - -public abstract class RcvBaseAdapter extends RecyclerView.Adapter { - - private Context context; - private int layoutId; - private List dataList = new ArrayList<>(); - private OnItemClickListener mOnItemClickListener; - - public RcvBaseAdapter(Context context, int layoutId) { - this.context = context; - this.layoutId = layoutId; - } - - @Override - public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { - View itemView = LayoutInflater.from(context).inflate(layoutId, parent, false); - return new BaseViewHolder(context, itemView); - } - - @Override - public void onBindViewHolder(final BaseViewHolder holder, int position) { - if (mOnItemClickListener != null) { - holder.getItemView().setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - int position = holder.getLayoutPosition(); - mOnItemClickListener.onItemClick(holder, dataList.get(position), position); - } - }); - } - bindViewHolder(holder, dataList.get(position), position); - } - - @Override - public int getItemCount() { - return dataList == null ? 0 : dataList.size(); - } - - public abstract void bindViewHolder(BaseViewHolder holder, T itemData, int position); - - public Context getContext() { - return context; - } - - public int getLayoutId() { - return layoutId; - } - - public List getDataList() { - return dataList; - } - - /** - * Description:设置数据,所有数据将被替换 - * Date:2018/3/9 - * - * @param dataList 需要添加的数据 - */ - public void setDataList(List dataList) { - this.dataList = dataList; - notifyDataSetChanged(); - } - - /** - * Description:添加数据 - * Date:2018/3/9 - * - * @param dataList 需要添加的数据 - * @param isRefresh 是否是刷新,如果为 true 则会先清空当前数据再添加,为 false 则会追加 - */ - public void addDataList(List dataList, boolean isRefresh) { - if (isRefresh) { - this.dataList.clear(); - } - this.dataList.addAll(dataList); - if (isRefresh) { - notifyDataSetChanged(); - } else { - notifyItemRangeInserted(this.dataList.size() - dataList.size(), this.dataList.size()); - } - } - - /** - * Description:设置 item 的点击监听器 - * Date:2018/3/9 - */ - public void setOnItemClickListener(OnItemClickListener onItemClickListener) { - this.mOnItemClickListener = onItemClickListener; - } -} diff --git a/app/src/main/java/com/qinshou/rcvbaseadapterdemo/adapter/RcvMultipleBaseAdapter.java b/app/src/main/java/com/qinshou/rcvbaseadapterdemo/adapter/RcvMultipleBaseAdapter.java deleted file mode 100644 index 589775a..0000000 --- a/app/src/main/java/com/qinshou/rcvbaseadapterdemo/adapter/RcvMultipleBaseAdapter.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.qinshou.rcvbaseadapterdemo.adapter; - -import android.content.Context; -import android.util.SparseArray; -import android.view.ViewGroup; - -import com.qinshou.rcvbaseadapterdemo.holder.BaseViewHolder; -import com.qinshou.rcvbaseadapterdemo.itemview.BaseItemView; - - -/** - * Description:item 为多布局类型的 RecyclerView 的适配器 - * 可以实现一对多(一个实体类对应多种布局)和多对多(多个实体类对应多个布局) - * 使用时创建该适配器实例,创建不同的 ItemView 继承 BaseItemView, - * 然后调用 addItemView() 方法传入 ItemView 即可 - * {@link com.qinshou.rcvbaseadapterdemo.itemview.BaseItemView} - * BaseItemView 中有一个方法 isForViewType() 用来判断何时引入哪种子布局,多对多时不用覆盖该方法,内部会根据泛型的真实类型和 - * 该 position 的数据的类型判断,一对多时需覆盖该方法加入自己的逻辑判断,否则同一类型只会使用传入的第一个 ItemView - *

- * Created by 禽兽先生 - * Created on 2018/4/9 - */ - -public class RcvMultipleBaseAdapter extends RcvBaseAdapter { - private SparseArray itemViewSparseArray; - - public RcvMultipleBaseAdapter(Context context) { - super(context, 0); - itemViewSparseArray = new SparseArray<>(); - } - - @Override - public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { - return itemViewSparseArray.get(viewType).onCreateViewHolder(parent); - } - - @Override - public int getItemViewType(int position) { - for (int i = 0; i < itemViewSparseArray.size(); i++) { - BaseItemView baseItemView = itemViewSparseArray.valueAt(i); - if (baseItemView.isForViewType(getDataList().get(position), position)) { - return itemViewSparseArray.keyAt(i); - } - } - throw new IllegalArgumentException("No ItemView added that matches position=" + position + " in data source"); - } - - @Override - public void bindViewHolder(BaseViewHolder holder, Object itemData, int position) { - for (int i = 0; i < itemViewSparseArray.size(); i++) { - BaseItemView baseItemView = itemViewSparseArray.valueAt(i); - if (baseItemView.isForViewType(itemData, position)) { - baseItemView.bindViewHolder(holder, itemData, position); - return; - } - } - } - - public void addItemView(BaseItemView baseItemView) { - itemViewSparseArray.put(baseItemView.hashCode(), baseItemView); - } - - public void removeItemView(BaseItemView baseItemView) { - itemViewSparseArray.remove(baseItemView.hashCode()); - } -} diff --git a/app/src/main/java/com/qinshou/rcvbaseadapterdemo/adapter/RcvSingleBaseAdapter.java b/app/src/main/java/com/qinshou/rcvbaseadapterdemo/adapter/RcvSingleBaseAdapter.java deleted file mode 100644 index c4cf92d..0000000 --- a/app/src/main/java/com/qinshou/rcvbaseadapterdemo/adapter/RcvSingleBaseAdapter.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.qinshou.rcvbaseadapterdemo.adapter; - -import android.content.Context; - -/** - * Description:item 为单布局的 RecyclerView 的适配器,只是简单继承了 RcvBaseAdapter - * 如果列表只是相同布局的 item,只需要继承该适配器,实现 public abstract void bindViewHolder(BaseViewHolder holder, T t) 这一个方法即可. - * Created by 禽兽先生 - * Created on 2017/12/14 - */ - -public abstract class RcvSingleBaseAdapter extends RcvBaseAdapter { - public RcvSingleBaseAdapter(Context context, int layoutId) { - super(context, layoutId); - } -} diff --git a/app/src/main/java/com/qinshou/rcvbaseadapterdemo/entity/MessageEntity.java b/app/src/main/java/com/qinshou/rcvbaseadapterdemo/entity/MessageEntity.java deleted file mode 100644 index fa90d08..0000000 --- a/app/src/main/java/com/qinshou/rcvbaseadapterdemo/entity/MessageEntity.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.qinshou.rcvbaseadapterdemo.entity; - -/** - * Description: - * Created by 禽兽先生 - * Created on 2018/10/9 - */ -public class MessageEntity { - private String message; - private int userType; //0 表示发送的消息,1 表示接收的消息 - - public MessageEntity() { - - } - - public String getMessage() { - return message; - } - - public void setMessage(String message) { - this.message = message; - } - - public int getUserType() { - return userType; - } - - public void setUserType(int userType) { - this.userType = userType; - } - - @Override - public String toString() { - return "MessageEntity{" + - "message='" + message + '\'' + - ", userType=" + userType + - '}'; - } -} diff --git a/app/src/main/java/com/qinshou/rcvbaseadapterdemo/holder/BaseViewHolder.java b/app/src/main/java/com/qinshou/rcvbaseadapterdemo/holder/BaseViewHolder.java deleted file mode 100644 index b63665f..0000000 --- a/app/src/main/java/com/qinshou/rcvbaseadapterdemo/holder/BaseViewHolder.java +++ /dev/null @@ -1,193 +0,0 @@ -package com.qinshou.rcvbaseadapterdemo.holder; - -import android.content.Context; -import android.graphics.Bitmap; -import android.support.annotation.ColorInt; -import android.support.annotation.DrawableRes; -import android.support.annotation.StringRes; -import android.support.v7.widget.RecyclerView; -import android.util.SparseArray; -import android.view.View; -import android.widget.Button; -import android.widget.ImageView; -import android.widget.TextView; - -/** - * Description:RecyclerView 的 ViewHolder 的基类,可以封装各种通用的方法 - * 比如 View 的背景设置,是否选中的状态设置 - * TextView 的文字设置,字体大小设置,字体颜色设置 - * ImageView 的图片设置,当然,如果一般是网络加载的话,需要获得 ImageView 自己异步加载,也可以在该类中内置 Glide 等第三方库异步加载网络图片 - * 设置 OnClickListener,OnLongClickListener 等 - * Created by 禽兽先生 - * Created on 2017/11/22 - */ - -public class BaseViewHolder extends RecyclerView.ViewHolder { - private Context context; - private View itemView; - private SparseArray viewSparseArray; - - public BaseViewHolder(Context context, View itemView) { - super(itemView); - this.context = context; - this.itemView = itemView; - this.viewSparseArray = new SparseArray(); - } - - public View getItemView() { - return itemView; - } - - public T findViewById(int viewId) { - View view = viewSparseArray.get(viewId); - if (view == null) { - view = itemView.findViewById(viewId); - viewSparseArray.put(viewId, view); - } - return (T) view; - } - - /** - * Description:设置控件是否可见 - * Date:2018/3/9 - */ - public BaseViewHolder setVisibility(int viewId, int visibility) { - View mView = findViewById(viewId); - if (mView != null) { - mView.setVisibility(visibility); - } - return this; - } - - /** - * Description:设置 TextView 文本 - * Date:2018/3/9 - */ - public BaseViewHolder setTvText(int viewId, String text) { - TextView mTextView = findViewById(viewId); - if (mTextView != null) { - mTextView.setText(text); - } - return this; - } - - /** - * Description:设置 TextView 文本 - * Date:2018/3/9 - */ - public BaseViewHolder setTvText(int viewId, @StringRes int resId) { - TextView mTextView = findViewById(viewId); - if (mTextView != null) { - mTextView.setText(context.getResources().getString(resId)); - } - return this; - } - - /** - * Description:设置 TextView 文本 - * Date:2018/3/9 - */ - public BaseViewHolder setTvText(int viewId, CharSequence text) { - TextView mTextView = findViewById(viewId); - if (mTextView != null) { - mTextView.setText(text); - } - return this; - } - - /** - * Description:设置 Button 文本 - * Date:2018/3/9 - */ - public BaseViewHolder setBtnText(int viewId, String text) { - Button mButton = findViewById(viewId); - if (mButton != null) { - mButton.setText(text); - } - return this; - } - - /** - * Description:设置 Button 文本 - * Date:2018/3/9 - */ - public BaseViewHolder setBtnText(int viewId, @StringRes int resId) { - Button mButton = findViewById(viewId); - if (mButton != null) { - mButton.setText(context.getResources().getString(resId)); - } - return this; - } - - /** - * Description:设置 ImageView 图片 - * Date:2018/3/9 - */ - public BaseViewHolder setIvImage(int viewId, Bitmap bitmap) { - ImageView mImageView = findViewById(viewId); - if (mImageView != null) { - mImageView.setImageBitmap(bitmap); - } - return this; - } - - /** - * Description:设置 ImageView 图片 - * Date:2018/3/9 - */ - public BaseViewHolder setIvImage(int viewId, @DrawableRes int resId) { - ImageView mImageView = findViewById(viewId); - if (mImageView != null) { - mImageView.setImageResource(resId); - } - return this; - } - - /** - * Description:设置 View 的点击事件监听器 - * Date:2018/3/9 - */ - public BaseViewHolder setOnClickListener(int viewId, View.OnClickListener onClickListener) { - View mView = findViewById(viewId); - if (mView != null) { - mView.setOnClickListener(onClickListener); - } - return this; - } - - /** - * Description:设置 View 的 selected 状态 - * Date:2018/3/9 - */ - public BaseViewHolder setSelected(int viewId, boolean selected) { - View mView = findViewById(viewId); - if (mView != null) { - mView.setSelected(selected); - } - return this; - } - - /** - * Description:设置 View 的背景色 - * Date:2018/3/9 - */ - public BaseViewHolder setBackgroundColor(int viewId, @ColorInt int color) { - View mView = findViewById(viewId); - if (mView != null) { - mView.setBackgroundColor(color); - } - return this; - } - - /** - * Description:设置 View 的背景 - * Date:2018/3/9 - */ - public BaseViewHolder setBackgroundResource(int viewId, @DrawableRes int resId) { - View mView = findViewById(viewId); - if (mView != null) { - mView.setBackgroundResource(resId); - } - return this; - } -} diff --git a/app/src/main/java/com/qinshou/rcvbaseadapterdemo/itemview/BaseItemView.java b/app/src/main/java/com/qinshou/rcvbaseadapterdemo/itemview/BaseItemView.java deleted file mode 100644 index 8bfd490..0000000 --- a/app/src/main/java/com/qinshou/rcvbaseadapterdemo/itemview/BaseItemView.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.qinshou.rcvbaseadapterdemo.itemview; - -import android.content.Context; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; - -import com.qinshou.rcvbaseadapterdemo.holder.BaseViewHolder; - -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.Type; - -/** - * Description:RecyclerView 单数据对多类型布局,多数据对多类型布局的适配器的 BaseItemView - * Created by 禽兽先生 - * Created on 2018/4/9 - */ - -public abstract class BaseItemView { - private Context context; - private int layoutId; - - public BaseItemView(Context context, int layoutId) { - this.context = context; - this.layoutId = layoutId; - } - - public BaseViewHolder onCreateViewHolder(ViewGroup parent) { - View itemView = LayoutInflater.from(context).inflate(layoutId, parent, false); - return new BaseViewHolder(context, itemView); - } - - /** - * Description: 子类可以覆蓋此方法决定引用该子布局的时机 - * Date:2018/8/6 - * - * @param item 该position对应的数据 - * @param position position - * @return 是否属于子布局 - */ - public boolean isForViewType(T item, int position) { - Type type = getClass().getGenericSuperclass(); - ParameterizedType parameterizedType = (ParameterizedType) type; - Class clazz = (Class) parameterizedType.getActualTypeArguments()[0]; - return item.getClass() == clazz; - } - - /** - * Description:绑定 UI 和数据 - * Date:2018/8/6 - */ - public abstract void bindViewHolder(BaseViewHolder holder, T itemData, int position); -} diff --git a/app/src/main/java/com/qinshou/rcvbaseadapterdemo/listener/OnItemClickListener.java b/app/src/main/java/com/qinshou/rcvbaseadapterdemo/listener/OnItemClickListener.java deleted file mode 100644 index 03427df..0000000 --- a/app/src/main/java/com/qinshou/rcvbaseadapterdemo/listener/OnItemClickListener.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.qinshou.rcvbaseadapterdemo.listener; - - -import com.qinshou.rcvbaseadapterdemo.holder.BaseViewHolder; - -/** - * Description: - * Created by 禽兽先生 - * Created on 2018/3/8 - */ - -public interface OnItemClickListener { - void onItemClick(BaseViewHolder holder, T t, int position); -} diff --git a/app/src/main/kotlin/com/qinshou/demo/MainActivity.kt b/app/src/main/kotlin/com/qinshou/demo/MainActivity.kt new file mode 100644 index 0000000..a2a1234 --- /dev/null +++ b/app/src/main/kotlin/com/qinshou/demo/MainActivity.kt @@ -0,0 +1,24 @@ +package com.qinshou.demo + +import android.content.Intent +import android.os.Bundle +import android.view.View +import androidx.appcompat.app.AppCompatActivity +import kotlinx.android.synthetic.main.activity_main.* + +/** + * Author: QinHao + * Email:qinhao@jeejio.com + * Date: 2021/4/15 16:02 + * Description: + */ +class MainActivity : AppCompatActivity() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main) + btn_one_to_one.setOnClickListener(View.OnClickListener { startActivity(Intent(this, OneToOneActivity::class.java)) }) + btn_many_to_many.setOnClickListener(View.OnClickListener { startActivity(Intent(this, ManyToManyActivity::class.java)) }) + btn_one_to_many.setOnClickListener(View.OnClickListener { startActivity(Intent(this, OneToManyActivity::class.java)) }) + btn_special_item.setOnClickListener(View.OnClickListener { startActivity(Intent(this, SpecialItemActivity::class.java)) }) + } +} \ No newline at end of file diff --git a/app/src/main/kotlin/com/qinshou/demo/ManyToManyActivity.kt b/app/src/main/kotlin/com/qinshou/demo/ManyToManyActivity.kt new file mode 100644 index 0000000..d3844b4 --- /dev/null +++ b/app/src/main/kotlin/com/qinshou/demo/ManyToManyActivity.kt @@ -0,0 +1,55 @@ +package com.qinshou.demo + +import android.content.Context +import android.os.Bundle +import androidx.appcompat.app.AppCompatActivity +import com.qinshou.rcv.adapter.RcvBaseAdapter +import com.qinshou.rcv.adapter.RcvMultipleAdapter +import com.qinshou.rcv.itemview.BaseItemView +import com.qinshou.rcv.viewholder.BaseViewHolder +import kotlinx.android.synthetic.main.activity_many_to_many.* + +/** + * Author: QinHao + * Email:qinhao@jeejio.com + * Date: 2021/4/15 16:02 + * Description: + */ +class ManyToManyActivity : AppCompatActivity() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_many_to_many) + val rcvManyToManyAdapter = RcvManyToManyAdapter(this) + rcv_many_to_many.adapter = rcvManyToManyAdapter + val list = ArrayList() + for (i in 0 until 100) { + if (i % 2 == 0) { + list.add(i) + } else { + list.add("测试文字$i") + } + } + rcvManyToManyAdapter.dataList = list + } + + private class RcvManyToManyAdapter(context: Context) : RcvMultipleAdapter(context) { + init { + addItemView(IntegerItemView(context, this)) + addItemView(StringItemView(context, this)) + } + } + + private class IntegerItemView(context: Context, rcvBaseAdapter: RcvBaseAdapter) : BaseItemView(context, R.layout.item_rcv_integer, rcvBaseAdapter) { + + override fun bindViewHolder(holder: BaseViewHolder, itemData: Int?, position: Int) { + holder.setTvText(R.id.text_view, "" + itemData) + } + } + + private class StringItemView(context: Context, rcvBaseAdapter: RcvBaseAdapter) : BaseItemView(context, R.layout.item_rcv_string, rcvBaseAdapter) { + + override fun bindViewHolder(holder: BaseViewHolder, itemData: String?, position: Int) { + holder.setTvText(R.id.text_view, itemData) + } + } +} \ No newline at end of file diff --git a/app/src/main/kotlin/com/qinshou/demo/NewsBean.kt b/app/src/main/kotlin/com/qinshou/demo/NewsBean.kt new file mode 100644 index 0000000..5635776 --- /dev/null +++ b/app/src/main/kotlin/com/qinshou/demo/NewsBean.kt @@ -0,0 +1,39 @@ +package com.qinshou.demo + +/** + * Author: QinHao + * Email:qinhao@jeejio.com + * Date: 2021/4/16 10:59 + * Description:类描述 + */ +class NewsBean { + var id: Int? = null + var href: String? = null + var imgSmall: String? = null + var imgBig: String? = null + var titleChinese: String? = null + var titleEnglish: String? = null + var publishTime: Long? = null + var introduction: String? = null + var sourceChinese: String? = null + var sourceEnglish: String? = null + var translatorAndEditor: String? = null + var htmlContent: String? = null + + override fun toString(): String { + return "NewsBean{" + + "id=" + id + + ", href='" + href + '\'' + + ", imgSmall='" + imgSmall + '\'' + + ", imgBig='" + imgBig + '\'' + + ", titleChinese='" + titleChinese + '\'' + + ", titleEnglish='" + titleEnglish + '\'' + + ", publishTime='" + publishTime + '\'' + + ", introduction='" + introduction + '\'' + + ", sourceChinese='" + sourceChinese + '\'' + + ", sourceEnglish='" + sourceEnglish + '\'' + + ", translatorAndEditor='" + translatorAndEditor + '\'' + + ", htmlContent='" + htmlContent + '\'' + + '}' + } +} \ No newline at end of file diff --git a/app/src/main/kotlin/com/qinshou/demo/OneToManyActivity.kt b/app/src/main/kotlin/com/qinshou/demo/OneToManyActivity.kt new file mode 100644 index 0000000..06c1dc7 --- /dev/null +++ b/app/src/main/kotlin/com/qinshou/demo/OneToManyActivity.kt @@ -0,0 +1,58 @@ +package com.qinshou.demo + +import android.content.Context +import android.os.Bundle +import androidx.appcompat.app.AppCompatActivity +import com.qinshou.rcv.adapter.RcvBaseAdapter +import com.qinshou.rcv.adapter.RcvMultipleAdapter +import com.qinshou.rcv.itemview.BaseItemView +import com.qinshou.rcv.viewholder.BaseViewHolder +import kotlinx.android.synthetic.main.activity_one_to_many.* + +/** + * Author: QinHao + * Email:qinhao@jeejio.com + * Date: 2021/4/15 16:02 + * Description: + */ +class OneToManyActivity : AppCompatActivity() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_one_to_many) + val rcvOneToManyAdapter = RcvOneToManyAdapter(this) + rcv_one_to_many.adapter = rcvOneToManyAdapter + val list = ArrayList() + for (i in 0 until 100) { + list.add("测试文字$i") + } + rcvOneToManyAdapter.dataList = list + } + + private class RcvOneToManyAdapter(context: Context) : RcvMultipleAdapter(context) { + init { + addItemView(OneToManyItemView1(context, this)) + addItemView(OneToManyItemView2(context, this)) + } + } + + private class OneToManyItemView1(context: Context, rcvBaseAdapter: RcvBaseAdapter) : BaseItemView(context, R.layout.item_rcv_one_to_many_1, rcvBaseAdapter) { + + override fun isForViewType(item: Any?, position: Int): Boolean { + return position % 2 == 0 + } + + override fun bindViewHolder(holder: BaseViewHolder, itemData: String?, position: Int) { + holder.setTvText(R.id.text_view, itemData) + } + } + + private class OneToManyItemView2(context: Context, rcvBaseAdapter: RcvBaseAdapter) : BaseItemView(context, R.layout.item_rcv_one_to_many_2, rcvBaseAdapter) { + override fun isForViewType(item: Any?, position: Int): Boolean { + return position % 2 != 0 + } + + override fun bindViewHolder(holder: BaseViewHolder, itemData: String?, position: Int) { + holder.setTvText(R.id.text_view, itemData) + } + } +} \ No newline at end of file diff --git a/app/src/main/kotlin/com/qinshou/demo/OneToOneActivity.kt b/app/src/main/kotlin/com/qinshou/demo/OneToOneActivity.kt new file mode 100644 index 0000000..ce169a3 --- /dev/null +++ b/app/src/main/kotlin/com/qinshou/demo/OneToOneActivity.kt @@ -0,0 +1,35 @@ +package com.qinshou.demo + +import android.content.Context +import android.os.Bundle +import androidx.appcompat.app.AppCompatActivity +import com.qinshou.rcv.adapter.RcvSimpleAdapter +import com.qinshou.rcv.viewholder.BaseViewHolder +import kotlinx.android.synthetic.main.activity_one_to_one.* + +/** + * Author: QinHao + * Email:qinhao@jeejio.com + * Date: 2021/4/15 16:02 + * Description: + */ +class OneToOneActivity : AppCompatActivity() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_one_to_one) + val rcvOneToOneAdapter = RcvOneToOneAdapter(this) + rcv_one_to_one.adapter = rcvOneToOneAdapter + val list = ArrayList() + for (i in 0 until 100) { + list.add("测试文字$i") + } + rcvOneToOneAdapter.dataList = list + } + + private class RcvOneToOneAdapter(context: Context) : RcvSimpleAdapter(context, R.layout.item_rcv_string) { + + override fun bindViewHolder(holder: BaseViewHolder, itemData: String?, position: Int) { + holder.setTvText(R.id.text_view, itemData) + } + } +} \ No newline at end of file diff --git a/app/src/main/kotlin/com/qinshou/demo/SpecialItemActivity.kt b/app/src/main/kotlin/com/qinshou/demo/SpecialItemActivity.kt new file mode 100644 index 0000000..4a90b72 --- /dev/null +++ b/app/src/main/kotlin/com/qinshou/demo/SpecialItemActivity.kt @@ -0,0 +1,108 @@ +package com.qinshou.demo + +import android.content.Context +import android.os.Bundle +import android.view.View +import androidx.appcompat.app.AppCompatActivity +import com.qinshou.rcv.adapter.RcvBaseAdapter +import com.qinshou.rcv.adapter.RcvMultipleAdapter +import com.qinshou.rcv.itemview.* +import com.qinshou.rcv.viewholder.BaseViewHolder +import kotlinx.android.synthetic.main.activity_one_to_one.* +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch +import java.text.SimpleDateFormat +import java.util.* + +/** + * Author: QinHao + * Email:qinhao@jeejio.com + * Date: 2021/4/15 16:02 + * Description: + */ +class SpecialItemActivity : AppCompatActivity() { + private val rcvNewsAdapter: RcvNewsAdapter by lazy { + return@lazy RcvNewsAdapter(this, this) + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_one_to_one) + rcv_one_to_one.adapter = rcvNewsAdapter + } + + private class RcvNewsAdapter(context: Context, specialItemActivity: SpecialItemActivity) : RcvMultipleAdapter(context) { + + init { + // EmptyItemView,需要自定义 UI,声明成员变量再重写里面的方法,否则如果有多个匿名内部类的话,kotlin 会报错 + val emptyItemView: EmptyItemView = object : EmptyItemView(context, R.layout.layout_empty_view_of_rcv_one_to_one, this) { + override fun bindViewHolder(holder: BaseViewHolder, itemData: Any?, position: Int) { + super.bindViewHolder(holder, itemData, position) + holder.setOnClickListener(R.id.btn_refresh, View.OnClickListener { + // 刷新数据,具体怎么发送请求,可以根据自己情况来,可以传一个 v 层或 p 层的实例进来,也 + // 可以通过 Handler 发送消息通知 + specialItemActivity.refresh() + }) + } + } + addItemView(emptyItemView) + // 骨架屏 item + addItemView(SkeletonItemView(context, R.layout.item_rcv_news, this)) + // HeaderItemView + addItemView(HeaderItemView(context, R.layout.layout_header_view_of_rcv_one_to_one, this)) + // HeaderItemView,需要自定义 UI,声明成员变量再重写里面的方法,否则如果有多个匿名内部类的话,kotlin 会报错 + val headerItemView2: HeaderItemView = object : HeaderItemView(context, R.layout.layout_header_view_of_rcv_one_to_one, this) { + override fun bindViewHolder(holder: BaseViewHolder, itemData: Any?, position: Int) { + super.bindViewHolder(holder, itemData, position) + holder.setTvText(R.id.text_view, "Header View 2") + } + } + addItemView(headerItemView2) + // FooterItemView + addItemView(FooterItemView(context, R.layout.layout_footer_view_of_rcv_one_to_one, this)) + // FooterItemView,需要自定义 UI,声明成员变量再重写里面的方法,否则如果有多个匿名内部类的话,kotlin 会报错 + val footerItemView2: FooterItemView = object : FooterItemView(context, R.layout.layout_footer_view_of_rcv_one_to_one, this) { + override fun bindViewHolder(holder: BaseViewHolder, itemData: Any?, position: Int) { + super.bindViewHolder(holder, itemData, position) + holder.setTvText(R.id.text_view, "Footer View 2") + } + } + addItemView(footerItemView2) + // 真正要显示的数据 + addItemView(NewsItemView(context, this)) + } + } + + private class NewsItemView(context: Context, rcvBaseAdapter: RcvBaseAdapter) : BaseItemView(context, R.layout.item_rcv_news, rcvBaseAdapter) { + private val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA) + + override fun bindViewHolder(holder: BaseViewHolder, itemData: NewsBean?, position: Int) { + holder.setTvText(R.id.tv_title, itemData?.titleChinese) + holder.setTvText(R.id.tv_source, "作者:" + itemData?.sourceChinese) + holder.setTvText(R.id.tv_translator, "翻译者:" + itemData?.translatorAndEditor) + if (itemData?.publishTime != null) { + holder.setTvText(R.id.tv_time, "翻译者:" + simpleDateFormat.format(Date(itemData.publishTime!!))) + } + } + } + + private fun refresh() { + GlobalScope.launch(Dispatchers.Main) { + rcvNewsAdapter.skeletonItemViewCount = 10 + delay(3000) + val list = ArrayList() + for (i in 0 until 100) { + val newsBean = NewsBean() + newsBean.titleChinese = "测试标题测试标题测试标题测试标题" + newsBean.sourceChinese = "测试来源" + newsBean.translatorAndEditor = "测试翻译者" + newsBean.publishTime = System.currentTimeMillis() + list.add(newsBean) + } + rcvNewsAdapter.skeletonItemViewCount = 0 + rcvNewsAdapter.dataList = list + } + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity/layout/activity_main.xml b/app/src/main/res/layout/activity/layout/activity_main.xml new file mode 100644 index 0000000..3c910da --- /dev/null +++ b/app/src/main/res/layout/activity/layout/activity_main.xml @@ -0,0 +1,57 @@ + + + +