Skip to content

Commit

Permalink
2019-04-1 14:40
Browse files Browse the repository at this point in the history
Add itemCount from xml for dataBinding
  • Loading branch information
Aryan-mor committed Apr 1, 2019
1 parent 77fa3c1 commit e6b4969
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 13 deletions.
4 changes: 2 additions & 2 deletions advancerecyclerview/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
defaultConfig {
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0.2"
versionCode 2
versionName "1.0.3"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,21 +130,24 @@ open class AdvanceRecyclerView : RecyclerView {
setAttributes(attrs)
}

fun init(@LayoutRes itemView: Int, itemCount: Int, linearLayoutManager: LinearLayoutManager?) {
fun init(
@LayoutRes itemView: Int, itemCount: Int = this.itemCount,
linearLayoutManager: LinearLayoutManager? = null
) {
var linearLayoutManager = linearLayoutManager
this.itemView = itemView
if (linearLayoutManager == null) {
linearLayoutManager ?: let {
linearLayoutManager = LinearLayoutManager(context)
}
this.linearLayoutManager = linearLayoutManager
init(itemCount)
}

fun init(itemCount: Int) {

Log.e("Ari","init itemCount -> $itemCount")
this.itemCount = itemCount
Log.e("Ari","init this.itemCount -> ${this.itemCount}")
init()
}

private fun init() {
adapter = myAdapter
setCurrentPosition(0)

Expand All @@ -162,9 +165,6 @@ open class AdvanceRecyclerView : RecyclerView {
if (paginationListener == null)
return

Log.e("Aryan", " item pos -> " + linearLayoutManager.findFirstCompletelyVisibleItemPosition())


if (linearLayoutManager.childCount + linearLayoutManager.findFirstVisibleItemPosition() >= layoutManager!!.itemCount) {
paginationListener!!.onEndItemListener(this, dx, dy)
return
Expand All @@ -187,6 +187,7 @@ open class AdvanceRecyclerView : RecyclerView {

protected fun setAttributes(attrs: AttributeSet?) {
setOrientationFromAttr(attrs)
setItemCountFromAttr(attrs)
}

protected fun setOrientationFromAttr(attr: AttributeSet?) {
Expand All @@ -198,6 +199,13 @@ open class AdvanceRecyclerView : RecyclerView {
a.recycle()
}

protected fun setItemCountFromAttr(attr: AttributeSet?) {
val a = getStyledAttributes(attr)
val count = a.getInt(R.styleable.AdvanceRecyclerView_ar_adre_itemCount, 0)
itemCount = count + 1
a.recycle()
}

protected fun getStyledAttributes(attr: AttributeSet?): TypedArray {
return context.theme.obtainStyledAttributes(
attr, R.styleable.AdvanceRecyclerView, 0, 0
Expand All @@ -206,7 +214,7 @@ open class AdvanceRecyclerView : RecyclerView {

// PROPERTIES

fun notifyDataSetChanged(newItemCount:Int){
fun notifyDataSetChanged(newItemCount: Int) {
addItemCount(newItemCount)
}

Expand Down
1 change: 1 addition & 0 deletions advancerecyclerview/src/main/res/values/attr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
<flag name="VERTICAL" value="0" />
<flag name="HORIZONTAL" value="1" />
</attr>
<attr name="ar_adre_itemCount" format="integer"/>
</declare-styleable>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package ir.aryanmo.advancerecyclerviewexample

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.TextView
import ir.aryanmo.advancerecyclerview.AdvanceRecyclerView
import kotlinx.android.synthetic.main.activity_main.*
import kotlin.math.log

class MainActivity : AppCompatActivity() {

Expand All @@ -25,7 +27,9 @@ class MainActivity : AppCompatActivity() {
}
}

re.init(R.layout.test_item, myList.size, null)
Log.e("ary","1")
re.init(itemView = R.layout.test_item)
Log.e("ary","2")

goTo.setOnClickListener {
if (re.isInitialize) {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
android:id="@+id/re"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:ar_adre_itemCount="5"
android:text="Hello World!"/>

</LinearLayout>

0 comments on commit e6b4969

Please sign in to comment.