Skip to content

Commit 93b6fdf

Browse files
committed
Merge branch 'release/1.4.0'
# Conflicts: # README.md
2 parents 366c208 + 082e431 commit 93b6fdf

File tree

10 files changed

+101
-36
lines changed

10 files changed

+101
-36
lines changed

README.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# ProperBaseAdapter
2-
Straightforward, fast, easy to use and adaptable generic RecyclerView adapter.
2+
Straightforward, fast, easy to use and adaptable generic RecyclerView adapter. Never create another RecyclerView adapter again.
33

44
## How does it work?
55
1. Define an AdapterItem representing an adapter item view type.
@@ -20,7 +20,7 @@ or inflate your own view.
2020
``` kotlin
2121
class TextRowRecyclerItem(private val text: String): AdapterItem<View>() {
2222
override fun getNewView(parent: ViewGroup): View {
23-
return LayoutInflater.from(parent.context).inflate(android.R.layout.activity_list_item, parent, false)
23+
return getViewFromLayout(parent, android.R.layout.activity_list_item)
2424
}
2525
override fun onViewBound(view: View) {
2626
view.findViewById<TextView>(R.id.text1).text = text
@@ -47,7 +47,7 @@ fun setAnimation(@AnimRes animation: Int): Unit
4747

4848
fun setClickListener(clickListener: View.OnClickListener?): Unit
4949

50-
fun setMargins(startMargin: Int = 0, topMargin: Int = 0, endMargin: Int = 0, bottomMargin: Int = 0): Unit
50+
fun setMargins(marginStart: Int = 0, marginTop: Int = 0, marginEnd: Int = 0, marginBottom: Int = 0): Unit
5151

5252
fun setIsStickyHeader(isStickyHeader: Boolean)
5353

@@ -119,19 +119,18 @@ fun setItems(newData: MutableList<AdapterItem<*>>, notifyDataSetChanged: Boolean
119119
```
120120

121121
## Nice! How do I get started?
122-
Add it in your root build.gradle at the end of repositories:
122+
Make sure root build.gradle repositories include JitPack
123123
``` gradle
124124
allprojects {
125-
repositories {
126-
...
127-
maven { url 'https://jitpack.io' }
128-
}
125+
repositories {
126+
maven { url 'https://jitpack.io' }
127+
}
129128
}
130129
```
131130

132-
And make sure this is in your app build.gradle
131+
And ProperBaseAdapter dependency is added to app build.gradle
133132
``` gradle
134133
dependencies {
135-
implementation "com.github.mvojtkovszky:ProperBaseAdapter:$latest_version"
134+
implementation "com.github.mvojtkovszky:ProperBaseAdapter:$latest_version"
136135
}
137136
```

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ apply plugin: 'kotlin-android-extensions'
44

55
android {
66
compileSdkVersion 30
7-
buildToolsVersion "30.0.1"
7+
buildToolsVersion "30.0.2"
88

99
compileOptions {
1010
sourceCompatibility = JavaVersion.VERSION_1_8

app/src/main/kotlin/com/vojtkovszky/properbaseadapter/example/MainActivity.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.os.Bundle
55
import android.view.View
66
import android.widget.Toast
77
import androidx.core.content.ContextCompat
8+
import androidx.recyclerview.widget.LinearLayoutManager
89
import androidx.recyclerview.widget.RecyclerView
910

1011
import com.vojtkovszky.properbaseadapter.*
@@ -21,7 +22,7 @@ class MainActivity : AppCompatActivity(), ProperBaseAdapterImplementation {
2122
// let's start
2223
refreshRecyclerView(
2324
refreshType = DataDispatchMethod.SET_DATA_AND_REFRESH,
24-
delayMillis = 1000)
25+
delayMillis = 500)
2526
}
2627

2728
override fun getAdapterData(data: MutableList<AdapterItem<*>>): MutableList<AdapterItem<*>> {
@@ -47,6 +48,7 @@ class MainActivity : AppCompatActivity(), ProperBaseAdapterImplementation {
4748

4849
// and another image for the last row
4950
data.add(ImageViewItem(ContextCompat.getDrawable(this, android.R.drawable.ic_btn_speak_now))
51+
.withMargins(marginBottom = resources.getDimensionPixelSize(R.dimen.dp16))
5052
.withViewTag("BOTTOM_IMAGE"))
5153

5254
return data
@@ -56,6 +58,12 @@ class MainActivity : AppCompatActivity(), ProperBaseAdapterImplementation {
5658
return findViewById(R.id.recyclerView)
5759
}
5860

61+
override fun getNewLayoutManager(): RecyclerView.LayoutManager? {
62+
return getRecyclerView()?.let {
63+
LinearLayoutManager(it.context, RecyclerView.VERTICAL, false)
64+
}
65+
}
66+
5967
override fun fadeOutStickyHeaders(): Boolean {
6068
return true
6169
}

app/src/main/kotlin/com/vojtkovszky/properbaseadapter/example/items/SectionHeaderItem.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.vojtkovszky.properbaseadapter.example.items
22

3-
import android.view.LayoutInflater
43
import android.view.View
54
import android.view.ViewGroup
65
import com.vojtkovszky.properbaseadapter.AdapterItem
@@ -10,7 +9,7 @@ import kotlinx.android.synthetic.main.section_header_view.view.*
109
class SectionHeaderItem(private val text: String): AdapterItem<View>() {
1110

1211
override fun getNewView(parent: ViewGroup): View {
13-
return LayoutInflater.from(parent.context).inflate(R.layout.section_header_view, parent, false)
12+
return getViewFromLayout(parent, R.layout.section_header_view)
1413
}
1514

1615
override fun onViewBound(view: View) {

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
buildscript {
2-
ext.kotlin_version = '1.3.72'
2+
ext.kotlin_version = '1.4.0'
33
repositories {
44
google()
55
jcenter()
@@ -12,6 +12,7 @@ buildscript {
1212

1313
allprojects {
1414
repositories {
15+
mavenLocal()
1516
google()
1617
jcenter()
1718
}

properbaseadapter/build.gradle

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
apply plugin: 'com.android.library'
22
apply plugin: 'kotlin-android'
33
apply plugin: 'kotlin-android-extensions'
4+
apply plugin: 'maven-publish'
45

56
android {
67
compileSdkVersion 30
7-
buildToolsVersion "30.0.1"
8+
buildToolsVersion "30.0.2"
89

910
compileOptions {
1011
sourceCompatibility = JavaVersion.VERSION_1_8
@@ -18,8 +19,8 @@ android {
1819
defaultConfig {
1920
minSdkVersion 16
2021
targetSdkVersion 30
21-
versionCode 4
22-
versionName "1.3.0"
22+
versionCode 5
23+
versionName "1.4.0"
2324

2425
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
2526
}
@@ -30,11 +31,48 @@ android {
3031
}
3132

3233
dependencies {
33-
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
34+
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
3435
implementation 'androidx.core:core-ktx:1.3.1'
3536
implementation 'androidx.appcompat:appcompat:1.2.0'
3637
implementation 'androidx.recyclerview:recyclerview:1.1.0'
38+
3739
testImplementation 'junit:junit:4.13'
3840
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
3941
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
4042
}
43+
44+
// handle publishing
45+
project.afterEvaluate {
46+
publishing {
47+
publications {
48+
library(MavenPublication) {
49+
groupId = 'com.github.mvojtkovszky'
50+
artifactId = 'BillingHelper'
51+
version = android.defaultConfig.versionName
52+
artifact bundleDebugAar
53+
// make sure pom has all dependencies
54+
pom.withXml {
55+
def dependenciesNode = asNode().appendNode('dependencies')
56+
configurations.api.allDependencies
57+
.findAll { dependency -> dependency.name != "unspecified" }
58+
.each { dependency ->
59+
addDependency(dependenciesNode.appendNode('dependency'), dependency, "compile")
60+
}
61+
configurations.implementation.allDependencies
62+
.findAll { dependency -> !configurations.api.allDependencies.contains(dependency) }
63+
.findAll { dependency -> dependency.name != "unspecified" }
64+
.each { dependency ->
65+
addDependency(dependenciesNode.appendNode('dependency'), dependency, "runtime")
66+
}
67+
}
68+
}
69+
}
70+
}
71+
}
72+
73+
static def addDependency(dependencyNode, dependency, scope) {
74+
dependencyNode.appendNode('groupId', dependency.group)
75+
dependencyNode.appendNode('artifactId', dependency.name)
76+
dependencyNode.appendNode('version', dependency.version)
77+
dependencyNode.appendNode('scope', scope)
78+
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2-
package="com.vojtkovszky.properbaseadapter" />
1+
<manifest package="com.vojtkovszky.properbaseadapter" />

properbaseadapter/src/main/kotlin/com/vojtkovszky/properbaseadapter/AdapterItem.kt

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.vojtkovszky.properbaseadapter
22

3+
import android.view.LayoutInflater
34
import android.view.View
45
import android.view.ViewGroup
56
import androidx.annotation.AnimRes
7+
import androidx.annotation.LayoutRes
68
import androidx.recyclerview.widget.RecyclerView
79
import kotlin.reflect.KClass
810

@@ -19,7 +21,6 @@ abstract class AdapterItem<AIV: View> : AdapterViewHolder.OnCallbackListener<AIV
1921
private var boundPosition = RecyclerView.NO_POSITION // position when bind is called
2022

2123
@AnimRes internal var animation: Int = 0 // animation when view will get displayed
22-
internal var layoutParamsInitialized = false // flag telling us if layout parameters have been applied yet
2324
internal var isStickyHeader: Boolean = false // sticky header option.
2425
internal var clickListener: View.OnClickListener? = null // generic click listener
2526
internal var viewTag: Any? = null // allows to attach a tag to item
@@ -49,7 +50,15 @@ abstract class AdapterItem<AIV: View> : AdapterViewHolder.OnCallbackListener<AIV
4950
}
5051
return h
5152
}
52-
}
53+
54+
/**
55+
* Convenience function to return inflated view from layout.
56+
* Commonly used for [getNewView] when using own custom layout
57+
*/
58+
fun getViewFromLayout(parent: ViewGroup, @LayoutRes layoutRes: Int): View {
59+
return LayoutInflater.from(parent.context).inflate(layoutRes, parent, false)
60+
}
61+
}
5362

5463
/**
5564
* Called when underlying adapter calls
@@ -158,15 +167,15 @@ abstract class AdapterItem<AIV: View> : AdapterViewHolder.OnCallbackListener<AIV
158167
/**
159168
* Define custom margins for this item to be applied when view gets bound.
160169
*/
161-
open fun withMargins(marginStart: Int = 0,
162-
marginTop: Int = 0,
163-
marginEnd: Int = 0,
164-
marginBottom: Int = 0
170+
open fun withMargins(marginStart: Int? = null,
171+
marginTop: Int? = null,
172+
marginEnd: Int? = null,
173+
marginBottom: Int? = null
165174
): AdapterItem<AIV> {
166-
this.marginStart = marginStart
167-
this.marginTop = marginTop
168-
this.marginEnd = marginEnd
169-
this.marginBottom = marginBottom
175+
this.marginStart = marginStart ?: this.marginStart
176+
this.marginTop = marginTop ?: this.marginTop
177+
this.marginEnd = marginEnd ?: this.marginEnd
178+
this.marginBottom = marginBottom ?: this.marginBottom
170179
return this
171180
}
172181

properbaseadapter/src/main/kotlin/com/vojtkovszky/properbaseadapter/ProperBaseAdapter.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ class ProperBaseAdapter constructor(data: MutableList<AdapterItem<*>> = mutableL
1717
// cache view type ids
1818
var viewTypeCachingEnabled = true
1919

20+
// allows us to properly set layout parameters in case LinearLayout is used
21+
var linearLayoutManagerOrientation = RecyclerView.VERTICAL
22+
2023
// Represents data in the adapter
2124
private lateinit var data: MutableList<AdapterItem<*>>
2225
// Represents cache of view type ids from our data.
@@ -247,7 +250,12 @@ class ProperBaseAdapter constructor(data: MutableList<AdapterItem<*>> = mutableL
247250
// retrieve params or create new. By default parameters take full available width
248251
val itemViewLayoutParams =
249252
if (viewHolder.itemView.layoutParams != null) viewHolder.itemView.layoutParams as RecyclerView.LayoutParams
250-
else RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
253+
else {
254+
val width =
255+
if (linearLayoutManagerOrientation == RecyclerView.VERTICAL) ViewGroup.LayoutParams.MATCH_PARENT
256+
else ViewGroup.LayoutParams.WRAP_CONTENT
257+
RecyclerView.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT)
258+
}
251259
// apply margins as defined by adapter item
252260
itemViewLayoutParams.setMargins(adapterItem.marginStart, adapterItem.marginTop,
253261
adapterItem.marginEnd, adapterItem.marginBottom)
@@ -256,7 +264,6 @@ class ProperBaseAdapter constructor(data: MutableList<AdapterItem<*>> = mutableL
256264
if (viewHolder.itemView.layoutParams == null) {
257265
viewHolder.itemView.layoutParams = itemViewLayoutParams
258266
}
259-
adapterItem.layoutParamsInitialized = true
260267

261268
// add generic click listener, if present
262269
if (adapterItem.clickListener != null) {

properbaseadapter/src/main/kotlin/com/vojtkovszky/properbaseadapter/ProperBaseAdapterImplementation.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ interface ProperBaseAdapterImplementation {
3636
* Define a layout manager.
3737
* Default implementation will use LinearLayoutManager
3838
*/
39-
fun getLayoutManager(): RecyclerView.LayoutManager? {
39+
fun getNewLayoutManager(): RecyclerView.LayoutManager? {
4040
return getRecyclerView()?.let { LinearLayoutManager(it.context) }
4141
}
4242

@@ -98,11 +98,16 @@ interface ProperBaseAdapterImplementation {
9898
try {
9999
// set layout manager if not set
100100
if (recyclerView.layoutManager == null) {
101-
recyclerView.layoutManager = getLayoutManager()
101+
recyclerView.layoutManager = getNewLayoutManager()
102102
}
103103

104104
// setup adapters
105-
val adapter = getAdapter() ?: ProperBaseAdapter()
105+
val adapter = getAdapter() ?: ProperBaseAdapter().also {
106+
// allows us to properly set default layout parameters in case LinearLayout is used
107+
if (recyclerView.layoutManager is LinearLayoutManager) {
108+
it.linearLayoutManagerOrientation = (recyclerView.layoutManager as LinearLayoutManager).orientation
109+
}
110+
}
106111

107112
// different behaviour based on refresh type
108113
when (refreshType) {

0 commit comments

Comments
 (0)