Skip to content

Commit

Permalink
feat: 限制泛型T,不能为 null 类型
Browse files Browse the repository at this point in the history
  • Loading branch information
limuyang2 committed Oct 30, 2023
1 parent ef8fbc3 commit 8cde08d
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class DragAndSwipeDifferActivity : BaseViewBindingActivity<ActivityUniversalRecy
private fun initRv() {
viewBinding.rv.adapter = mAdapter

mAdapter.submitList(DataServer.getDiffUtilDemoEntities())
mAdapter.submitList(DataServer.diffUtilDemoEntities)
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import com.chad.library.adapter.base.BaseQuickAdapter
* https://github.com/CymChad/BaseRecyclerViewAdapterHelper
*/
class HeaderAndFooterAdapter(list: List<Status>) :
BaseQuickAdapter<Status?, HeaderAndFooterAdapter.VH>(list) {
BaseQuickAdapter<Status, HeaderAndFooterAdapter.VH>(list) {

class VH(var binding: ItemHeaderAndFooterBinding) : RecyclerView.ViewHolder(binding.root)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class HomeTopHeaderAdapter : BaseSingleItemAdapter<Any, HomeTopHeaderAdapter.VH>
override fun onBindViewHolder(holder: VH, item: Any?) {
}

override fun getItemViewType(position: Int, list: List<Any?>): Int {
override fun getItemViewType(position: Int, list: List<Any>): Int {
return HEAD_VIEWTYPE
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import com.chad.library.adapter.base.BaseQuickAdapter
* @date: 2019-12-06
* @Description:
*/
class UpFetchAdapter : BaseQuickAdapter<Movie?, UpFetchAdapter.VH>() {
class UpFetchAdapter : BaseQuickAdapter<Movie, UpFetchAdapter.VH>() {
class VH(
parent: ViewGroup,
val viewBinding: ItemHeaderAndFooterBinding = ItemHeaderAndFooterBinding.inflate(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,77 +1,70 @@
package com.chad.baserecyclerviewadapterhelper.data;
package com.chad.baserecyclerviewadapterhelper.data


import com.chad.baserecyclerviewadapterhelper.entity.DiffEntity;
import com.chad.baserecyclerviewadapterhelper.entity.Status;

import java.util.ArrayList;
import java.util.List;
import com.chad.baserecyclerviewadapterhelper.entity.DiffEntity
import com.chad.baserecyclerviewadapterhelper.entity.Status

/**
* https://github.com/CymChad/BaseRecyclerViewAdapterHelper
*/
public class DataServer {

public static final String HTTPS_AVATARS1_GITHUBUSERCONTENT_COM_LINK = "https://avatars1.githubusercontent.com/u/7698209?v=3&s=460";
public static final String CYM_CHAD = "CymChad";
public static final String CHAY_CHAN = "ChayChan";

private DataServer() {
}

public static List<Status> getSampleData(int lenth) {
List<Status> list = new ArrayList<>();
for (int i = 0; i < lenth; i++) {
Status status = new Status();
status.setUserName("Chad" + i);
status.setCreatedAt("04/05/" + i);
status.setRetweet(i % 2 == 0);
status.setUserAvatar("https://avatars1.githubusercontent.com/u/7698209?v=3&s=460");
status.setText("BaseRecyclerViewAdpaterHelper https://www.recyclerview.org");
list.add(status);
object DataServer {
const val HTTPS_AVATARS1_GITHUBUSERCONTENT_COM_LINK =
"https://avatars1.githubusercontent.com/u/7698209?v=3&s=460"
const val CYM_CHAD = "CymChad"
const val CHAY_CHAN = "ChayChan"
fun getSampleData(lenth: Int): List<Status> {
val list: MutableList<Status> = ArrayList()
for (i in 0 until lenth) {
val status = Status()
status.userName = "Chad$i"
status.createdAt = "04/05/$i"
status.isRetweet = i % 2 == 0
status.userAvatar = "https://avatars1.githubusercontent.com/u/7698209?v=3&s=460"
status.text = "BaseRecyclerViewAdpaterHelper https://www.recyclerview.org"
list.add(status)
}
return list;
return list
}

public static List<Status> addData(List list, int dataSize) {
for (int i = 0; i < dataSize; i++) {
Status status = new Status();
status.setUserName("Chad" + i);
status.setCreatedAt("04/05/" + i);
status.setRetweet(i % 2 == 0);
status.setUserAvatar("https://avatars1.githubusercontent.com/u/7698209?v=3&s=460");
status.setText("Powerful and flexible RecyclerAdapter https://github.com/CymChad/BaseRecyclerViewAdapterHelper");
list.add(status);
fun addData(list: MutableList<Status>, dataSize: Int): List<Status> {
for (i in 0 until dataSize) {
val status = Status()
status.userName = "Chad$i"
status.createdAt = "04/05/$i"
status.isRetweet = i % 2 == 0
status.userAvatar = "https://avatars1.githubusercontent.com/u/7698209?v=3&s=460"
status.text =
"Powerful and flexible RecyclerAdapter https://github.com/CymChad/BaseRecyclerViewAdapterHelper"
list.add(status)
}

return list;
return list
}



public static List<String> getStrData() {
List<String> list = new ArrayList<>();
for (int i = 0; i < 20; i++) {
String str = HTTPS_AVATARS1_GITHUBUSERCONTENT_COM_LINK;
if (i % 2 == 0) {
str = CYM_CHAD;
val strData: List<String>
get() {
val list: MutableList<String> = ArrayList()
for (i in 0..19) {
var str = HTTPS_AVATARS1_GITHUBUSERCONTENT_COM_LINK
if (i % 2 == 0) {
str = CYM_CHAD
}
list.add(str)
}
list.add(str);
return list
}
return list;
}


public static List<DiffEntity> getDiffUtilDemoEntities() {
List<DiffEntity> list = new ArrayList<>();
for (int i = 0; i < 10; i++){
list.add(new DiffEntity(
i,
"Item " + i,
"This item " + i + " content",
"06-12")
);
@JvmStatic
val diffUtilDemoEntities: List<DiffEntity>
get() {
val list: MutableList<DiffEntity> = ArrayList()
for (i in 0..9) {
list.add(
DiffEntity(
i,
"Item $i",
"This item $i content",
"06-12"
)
)
}
return list
}
return list;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.chad.library.adapter.base

import androidx.annotation.IntRange
import androidx.recyclerview.widget.*
import androidx.recyclerview.widget.AsyncListDiffer.ListListener
import java.util.*
Expand All @@ -12,7 +13,7 @@ import java.util.*
* @param T 数据类型
* @param VH ViewHolder 类型
*/
abstract class BaseDifferAdapter<T, VH : RecyclerView.ViewHolder>(
abstract class BaseDifferAdapter<T : Any, VH : RecyclerView.ViewHolder>(
config: AsyncDifferConfig<T>, items: List<T>
) : BaseQuickAdapter<T, VH>() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import java.lang.ref.WeakReference
* 多类型布局
*
*/
abstract class BaseMultiItemAdapter<T>(items: List<T> = emptyList()) :
abstract class BaseMultiItemAdapter<T : Any>(items: List<T> = emptyList()) :
BaseQuickAdapter<T, RecyclerView.ViewHolder>(items) {

private val typeViewHolders =
Expand Down Expand Up @@ -142,7 +142,7 @@ abstract class BaseMultiItemAdapter<T>(items: List<T> = emptyList()) :
* @param V
* @constructor Create empty On multi item
*/
abstract class OnMultiItem<T, V : RecyclerView.ViewHolder> : OnMultiItemAdapterListener<T, V> {
abstract class OnMultiItem<T : Any, V : RecyclerView.ViewHolder> : OnMultiItemAdapterListener<T, V> {
internal var weakA: WeakReference<BaseMultiItemAdapter<T>>? = null

val adapter: BaseMultiItemAdapter<T>?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import java.util.Collections
* @param VH : BaseViewHolder
* @constructor layoutId, data(Can null parameters, the default is empty data)
*/
abstract class BaseQuickAdapter<T, VH : RecyclerView.ViewHolder>(
abstract class BaseQuickAdapter<T : Any, VH : RecyclerView.ViewHolder>(
open var items: List<T> = emptyList()
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

Expand Down Expand Up @@ -742,19 +742,19 @@ abstract class BaseQuickAdapter<T, VH : RecyclerView.ViewHolder>(
}


fun interface OnItemClickListener<T> {
fun interface OnItemClickListener<T : Any> {
fun onClick(adapter: BaseQuickAdapter<T, *>, view: View, position: Int)
}

fun interface OnItemLongClickListener<T> {
fun interface OnItemLongClickListener<T : Any> {
fun onLongClick(adapter: BaseQuickAdapter<T, *>, view: View, position: Int): Boolean
}

fun interface OnItemChildClickListener<T> {
fun interface OnItemChildClickListener<T : Any> {
fun onItemClick(adapter: BaseQuickAdapter<T, *>, view: View, position: Int)
}

fun interface OnItemChildLongClickListener<T> {
fun interface OnItemChildLongClickListener<T : Any> {
fun onItemLongClick(adapter: BaseQuickAdapter<T, *>, view: View, position: Int): Boolean
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import androidx.recyclerview.widget.RecyclerView
* @param VH viewHolder类型 type of the viewHolder
* @property mItem 数据 data
*/
abstract class BaseSingleItemAdapter<T, VH : RecyclerView.ViewHolder>(private var mItem: T? = null) :
BaseQuickAdapter<Any?, VH>() {
abstract class BaseSingleItemAdapter<T : Any, VH : RecyclerView.ViewHolder>(private var mItem: T? = null) :
BaseQuickAdapter<Any, VH>() {

protected abstract fun onBindViewHolder(holder: VH, item: T?)

Expand All @@ -27,7 +27,7 @@ abstract class BaseSingleItemAdapter<T, VH : RecyclerView.ViewHolder>(private va
onBindViewHolder(holder, mItem, payloads)
}

final override fun getItemCount(items: List<Any?>): Int {
final override fun getItemCount(items: List<Any>): Int {
return 1
}

Expand All @@ -52,35 +52,35 @@ abstract class BaseSingleItemAdapter<T, VH : RecyclerView.ViewHolder>(private va
notifyItemChanged(0)
}

override fun submitList(list: List<Any?>?) {
override fun submitList(list: List<Any>?) {
throw RuntimeException("Please use setItem()")
}

override fun add(data: Any?) {
override fun add(data: Any) {
throw RuntimeException("Please use setItem()")
}

override fun add(position: Int, data: Any?) {
override fun add(position: Int, data: Any) {
throw RuntimeException("Please use setItem()")
}

override fun addAll(collection: Collection<Any?>) {
override fun addAll(collection: Collection<Any>) {
throw RuntimeException("Please use setItem()")
}

override fun addAll(position: Int, collection: Collection<Any?>) {
override fun addAll(position: Int, collection: Collection<Any>) {
throw RuntimeException("Please use setItem()")
}

override fun remove(data: Any?) {
override fun remove(data: Any) {
throw RuntimeException("Please use setItem()")
}

override fun removeAt(position: Int) {
throw RuntimeException("Please use setItem()")
}

override fun set(position: Int, data: Any?) {
override fun set(position: Int, data: Any) {
throw RuntimeException("Please use setItem()")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import com.chad.library.adapter.base.BaseQuickAdapter
* @date 2023/5/29
* @description
*/
private abstract class DebouncedClickListener<T>(private val interval: Long) :
private abstract class DebouncedClickListener<T : Any>(private val interval: Long) :
BaseQuickAdapter.OnItemClickListener<T>, BaseQuickAdapter.OnItemChildClickListener<T> {
private var mLastClickTime: Long = 0

Expand Down Expand Up @@ -46,7 +46,7 @@ private abstract class DebouncedClickListener<T>(private val interval: Long) :
* @param block
* @receiver
*/
fun <T, VH : RecyclerView.ViewHolder> BaseQuickAdapter<T, VH>.setOnDebouncedItemClick(
fun <T : Any, VH : RecyclerView.ViewHolder> BaseQuickAdapter<T, VH>.setOnDebouncedItemClick(
time: Long = 500,
block: BaseQuickAdapter.OnItemClickListener<T>
) = this.setOnItemClickListener(object : DebouncedClickListener<T>(time) {
Expand All @@ -62,7 +62,7 @@ fun <T, VH : RecyclerView.ViewHolder> BaseQuickAdapter<T, VH>.setOnDebouncedItem
* @param block
* @receiver
*/
fun <T, VH : RecyclerView.ViewHolder> BaseQuickAdapter<T, VH>.addOnDebouncedChildClick(
fun <T : Any, VH : RecyclerView.ViewHolder> BaseQuickAdapter<T, VH>.addOnDebouncedChildClick(
@IdRes id: Int,
time: Long = 500,
block: BaseQuickAdapter.OnItemChildClickListener<T>
Expand Down

0 comments on commit 8cde08d

Please sign in to comment.