Skip to content

Commit 1810a4c

Browse files
authored
Merge pull request #14 from FarshidRoohi/develop
Release new version
2 parents 1d5bfb2 + 91d2262 commit 1810a4c

25 files changed

+602
-142
lines changed

build.gradle

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
2-
32
buildscript {
4-
ext.kotlin_version = '1.5.31'
3+
ext.kotlin_version = '1.7.10'
54

65
repositories {
76
google()
87
mavenCentral()
98
}
109
dependencies {
11-
classpath 'com.android.tools.build:gradle:7.0.3'
10+
classpath 'com.android.tools.build:gradle:7.3.0'
1211
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1312
classpath "com.vanniktech:gradle-maven-publish-plugin:0.17.0"
1413
}

customAdapterRecycleView/build.gradle

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ apply plugin: 'maven-publish'
44
apply plugin: 'com.vanniktech.maven.publish'
55

66
android {
7-
compileSdkVersion 30
7+
compileSdkVersion 33
88

99
defaultConfig {
1010
minSdkVersion 14
11-
targetSdkVersion 30
11+
targetSdkVersion 33
1212
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1313

1414
}
@@ -20,6 +20,14 @@ android {
2020
}
2121
}
2222

23+
compileOptions {
24+
sourceCompatibility = JavaVersion.VERSION_1_8
25+
targetCompatibility = JavaVersion.VERSION_1_8
26+
}
27+
buildFeatures {
28+
viewBinding = true
29+
}
30+
2331
}
2432

2533
publishing {
@@ -37,8 +45,8 @@ publishing {
3745

3846
dependencies {
3947
implementation fileTree(include: ['*.jar'], dir: 'libs')
40-
implementation 'androidx.appcompat:appcompat:1.3.1'
48+
implementation 'androidx.core:core-ktx:1.9.0'
49+
implementation 'androidx.appcompat:appcompat:1.5.1'
4150
implementation 'androidx.recyclerview:recyclerview:1.2.1'
42-
implementation "androidx.core:core-ktx:1.6.0"
4351
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
4452
}

customAdapterRecycleView/src/main/java/io/github/farshidroohi/AdapterRecyclerView.kt

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.farshidroohi
22

3+
import android.annotation.SuppressLint
34
import android.content.Context
45
import android.view.LayoutInflater
56
import android.view.View
@@ -23,10 +24,11 @@ abstract class AdapterRecyclerView<T>(
2324
@IdRes val retryButtonId: Int
2425
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
2526

27+
2628
companion object {
27-
private const val ITEM_VIEW = 0
28-
private const val ITEM_LOADING = 1
29-
private const val ITEM_FAILED = 2
29+
const val ITEM_VIEW = 0
30+
const val ITEM_LOADING = 1
31+
const val ITEM_FAILED = 2
3032
}
3133

3234
val items: MutableList<T?> = arrayListOf()
@@ -39,18 +41,26 @@ abstract class AdapterRecyclerView<T>(
3941

4042

4143
abstract fun onBindView(
42-
viewHolder: ItemViewHolder,
44+
viewHolder: RecyclerView.ViewHolder,
4345
position: Int,
4446
context: Context,
4547
element: T?
4648
)
4749

50+
open fun onCreateViewHolder(
51+
layoutInflater: LayoutInflater,
52+
viewGroup: ViewGroup,
53+
viewType: Int
54+
): RecyclerView.ViewHolder? {
55+
return null
56+
}
4857

4958
override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
5059

5160
val inflater = LayoutInflater.from(viewGroup.context)
5261

5362
return when (viewType) {
63+
5464
ITEM_VIEW -> {
5565
val view = inflater.inflate(itemViewLayout, viewGroup, false)
5666
ItemViewHolder(view)
@@ -65,11 +75,7 @@ abstract class AdapterRecyclerView<T>(
6575
val view = inflater.inflate(itemFailedLayout, viewGroup, false)
6676
FailedViewHolder(view)
6777
}
68-
69-
else -> {
70-
val view = inflater.inflate(itemViewLayout, viewGroup, false)
71-
ItemViewHolder(view)
72-
}
78+
else -> onCreateViewHolder(inflater, viewGroup, viewType)!!
7379
}
7480

7581
}
@@ -78,10 +84,6 @@ abstract class AdapterRecyclerView<T>(
7884

7985
when (viewHolder) {
8086

81-
is ItemViewHolder -> {
82-
onBindView(viewHolder, position, viewHolder.itemView.context, getItem(position))
83-
}
84-
8587
is FailedViewHolder -> {
8688

8789
handleSingleRow(viewHolder, position)
@@ -90,12 +92,14 @@ abstract class AdapterRecyclerView<T>(
9092
view?.setOnClickListener {
9193
onRetryClicked()
9294
}
93-
9495
}
9596

9697
is LoadingViewHolder -> {
9798
handleSingleRow(viewHolder, position)
9899
}
100+
else -> {
101+
onBindView(viewHolder, position, viewHolder.itemView.context, getItem(position))
102+
}
99103

100104
}
101105

@@ -125,6 +129,7 @@ abstract class AdapterRecyclerView<T>(
125129
}
126130

127131

132+
@SuppressLint("NotifyDataSetChanged")
128133
fun removeAll() {
129134
if (items.isEmpty()) {
130135
return
@@ -142,11 +147,12 @@ abstract class AdapterRecyclerView<T>(
142147
notifyItemRangeChanged(position, itemCount)
143148
}
144149

150+
@SuppressLint("NotifyDataSetChanged")
145151
fun remove(vararg item: T) {
146152
if (this.items.isEmpty()) {
147153
return
148154
}
149-
this.items.removeAll(item)
155+
this.items.removeAll(item.toSet())
150156
notifyDataSetChanged()
151157
}
152158

@@ -155,6 +161,7 @@ abstract class AdapterRecyclerView<T>(
155161
}
156162

157163

164+
@SuppressLint("NotifyDataSetChanged")
158165
fun loadedState(newItems: List<T?>?) {
159166

160167
if (newItems == null) {
@@ -264,5 +271,4 @@ abstract class AdapterRecyclerView<T>(
264271
class LoadingViewHolder(view: View) : RecyclerView.ViewHolder(view)
265272

266273
class FailedViewHolder(view: View) : RecyclerView.ViewHolder(view)
267-
268274
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package io.github.farshidroohi
2+
3+
import android.view.ViewGroup
4+
import androidx.recyclerview.widget.DiffUtil
5+
import androidx.recyclerview.widget.ListAdapter
6+
import androidx.viewbinding.ViewBinding
7+
import io.github.farshidroohi.vh.BaseViewHolder
8+
9+
10+
/**
11+
* Created by Farshid Roohi.
12+
* CustomAdapterRecyclerview | Copyrights 10/8/22.
13+
*/
14+
15+
abstract class DiffUtilAdapterRecyclerView<ViewBindingType : ViewBinding, T>(
16+
diffCallback: DiffUtil.ItemCallback<T>
17+
) : ListAdapter<T, BaseViewHolder<ViewBindingType>>(diffCallback) {
18+
19+
abstract fun initViewBinding(parent: ViewGroup): ViewBindingType
20+
abstract fun bind(binding: ViewBindingType, position: Int, item: T, payloads: MutableList<Any>?)
21+
22+
override fun onCreateViewHolder(
23+
parent: ViewGroup,
24+
viewType: Int
25+
): BaseViewHolder<ViewBindingType> {
26+
val binding = initViewBinding(parent)
27+
return BaseViewHolder(binding)
28+
}
29+
30+
override fun onBindViewHolder(
31+
holder: BaseViewHolder<ViewBindingType>,
32+
position: Int,
33+
payloads: MutableList<Any>
34+
) {
35+
bind(holder.binding, position, currentList[position], payloads)
36+
}
37+
38+
override fun onBindViewHolder(holder: BaseViewHolder<ViewBindingType>, position: Int) {
39+
bind(holder.binding, position, currentList[position], null)
40+
}
41+
42+
override fun getItemCount(): Int = currentList.size
43+
44+
fun changeItem(items: ArrayList<T>, element: T, newElement: T) {
45+
val index = items.indexOf(element)
46+
items[index] = newElement
47+
submitList(items.toMutableList())
48+
}
49+
50+
fun clear() {
51+
submitList(emptyList())
52+
}
53+
54+
}

customAdapterRecycleView/src/main/java/io/github/farshidroohi/extensions/RecyclerViewExtensions.kt

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ fun RecyclerView.onLoadMoreListener(extraCount: Int = 0, onLoadMore: () -> Unit)
3434

3535
is StaggeredGridLayoutManager -> {
3636
val staggeredGridLayoutManager =
37-
layoutManager as StaggeredGridLayoutManager
37+
layoutManager as StaggeredGridLayoutManager
3838
val spanCount = staggeredGridLayoutManager.spanCount
3939
val lastPositions =
40-
staggeredGridLayoutManager.findFirstVisibleItemPositions(IntArray(spanCount))
40+
staggeredGridLayoutManager.findFirstVisibleItemPositions(IntArray(spanCount))
4141
min(lastPositions[0], lastPositions[1])
4242
}
4343

@@ -61,16 +61,29 @@ fun RecyclerView.onLoadMoreListener(extraCount: Int = 0, onLoadMore: () -> Unit)
6161

6262
}
6363

64-
fun RecyclerView.onItemClickListener(onClickItem: (position: Int) -> Unit, onLongClickItem: (position: Int) -> Unit) {
64+
fun RecyclerView.onItemClickListener(
65+
onClickItem: (position: Int) -> Unit,
66+
onLongClickItem: ((position: Int) -> Unit)? = null
67+
) {
68+
69+
this.addOnItemTouchListener(
70+
RecyclerTouchListener(
71+
context,
72+
this,
73+
object : OnItemListenerRecyclerViewListener {
74+
override fun onClick(position: Int) {
75+
if (position != (adapter?.itemCount ?: 0) - 1) {
76+
onClickItem(position)
77+
}
78+
}
6579

66-
this.addOnItemTouchListener(RecyclerTouchListener(context, this, object : OnItemListenerRecyclerViewListener {
67-
override fun onClick(position: Int) {
68-
onClickItem(position)
69-
}
80+
override fun onLongClick(position: Int) {
81+
if (position != (adapter?.itemCount ?: 0) - 1) {
82+
onLongClickItem?.invoke(position)
83+
}
7084

71-
override fun onLongClick(position: Int) {
72-
onLongClickItem(position)
73-
}
74-
}))
85+
}
86+
})
87+
)
7588

7689
}

customAdapterRecycleView/src/main/java/io/github/farshidroohi/listener/RecyclerTouchListener.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class RecyclerTouchListener(context: Context?, recyclerView: RecyclerView, priva
2525
override fun onLongPress(e: MotionEvent) {
2626
val child = recyclerView.findChildViewUnder(e.x, e.y)
2727
if (child != null && clickListener != null) {
28-
clickListener.onLongClick(recyclerView.getChildPosition(child))
28+
clickListener.onLongClick(recyclerView.getChildAdapterPosition(child))
2929
}
3030
}
3131
})
@@ -35,7 +35,7 @@ class RecyclerTouchListener(context: Context?, recyclerView: RecyclerView, priva
3535
override fun onInterceptTouchEvent(rv: RecyclerView, e: MotionEvent): Boolean {
3636
val child = rv.findChildViewUnder(e.x, e.y)
3737
if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
38-
clickListener.onClick(rv.getChildPosition(child))
38+
clickListener.onClick(rv.getChildAdapterPosition(child))
3939
}
4040
return false
4141
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package io.github.farshidroohi.vh
2+
3+
import androidx.recyclerview.widget.RecyclerView
4+
import androidx.viewbinding.ViewBinding
5+
6+
/**
7+
* Created by Farshid Roohi.
8+
* CustomAdapterRecyclerview | Copyrights 10/8/22.
9+
*/
10+
11+
class BaseViewHolder<ViewBindingType : ViewBinding>(val binding: ViewBindingType) :
12+
RecyclerView.ViewHolder(binding.root) {
13+
}

gradle.properties

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ org.gradle.jvmargs=-Xmx1536m
44

55
GROUP=io.github.farshidroohi
66
POM_ARTIFACT_ID=customAdapterRecycleView
7-
VERSION_NAME=2.0.3
7+
VERSION_NAME=2.1.2
88

99
POM_NAME=CustomAdapterRecycleView
1010
POM_DESCRIPTION=Very easy endless scrolled, onClick item and simple use recyclerView adapter
@@ -19,5 +19,4 @@ POM_LICENCE_URL=https://www.apache.org/licenses/LICENSE-2.0.txt
1919
POM_LICENCE_DIST=repo
2020

2121
POM_DEVELOPER_ID=farshidroohi
22-
POM_DEVELOPER_NAME=Farshid Roohi
23-
22+
POM_DEVELOPER_NAME=Farshid Roohi

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip

sample/build.gradle

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ apply plugin: 'kotlin-android-extensions'
33
apply plugin: 'kotlin-android'
44

55
android {
6-
compileSdkVersion 30
6+
compileSdkVersion 33
77
defaultConfig {
88
minSdkVersion 14
9-
targetSdkVersion 30
9+
targetSdkVersion 33
1010

1111
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1212
consumerProguardFiles 'consumer-rules.pro'
@@ -19,14 +19,23 @@ android {
1919
}
2020
}
2121

22+
compileOptions {
23+
sourceCompatibility = JavaVersion.VERSION_1_8
24+
targetCompatibility = JavaVersion.VERSION_1_8
25+
}
26+
27+
buildFeatures {
28+
viewBinding = true
29+
}
30+
2231
}
2332

2433
dependencies {
2534
implementation fileTree(dir: 'libs', include: ['*.jar'])
26-
implementation 'androidx.appcompat:appcompat:1.3.1'
27-
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
35+
implementation 'androidx.appcompat:appcompat:1.5.1'
36+
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
2837
implementation 'androidx.recyclerview:recyclerview:1.2.1'
29-
implementation "androidx.core:core-ktx:1.6.0"
38+
implementation 'androidx.core:core-ktx:1.9.0'
3039
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
3140

3241
implementation project(":customAdapterRecycleView")

0 commit comments

Comments
 (0)