Skip to content

Commit

Permalink
2019-05-21 13:05
Browse files Browse the repository at this point in the history
Add ItemView class
  • Loading branch information
Aryan-mor authored and Aryan-mor committed May 21, 2019
1 parent 2235f7e commit 21737dc
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 116 deletions.
25 changes: 0 additions & 25 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions advancerecyclerview/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
defaultConfig {
minSdkVersion 15
targetSdkVersion 28
versionCode 2
versionName "1.0.7"
versionCode 3
versionName "1.1"

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ open class AdvanceRecyclerView : RecyclerView {
Exception("setOrientation value must be 0(AdvanceRecycleView.VERTICAL),1(AdvanceRecycleView.HORIZONTAL)")
)
}
@LayoutRes
var itemView = -1

var itemViews = hashMapOf<Int, ItemView>()
@LayoutRes
var loadingItemView = -1
protected set
Expand Down Expand Up @@ -68,31 +68,34 @@ open class AdvanceRecyclerView : RecyclerView {
var itemSwipeCallback: ItemSwipeCallback? = null

//Animation
var itemAnimRes: Int = -1
private var lastItemPosition = -1


val count: Int
get() = itemCount + 1


//Functionality

// public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {

var myAdapter: RecyclerView.Adapter<*> = object : RecyclerView.Adapter<ViewHolder>() {
override fun getItemViewType(position: Int): Int {

val viewType =
if (itemViews.size == 1) itemViews[itemViews.entries.first().key]!!.itemViewId else onAdapterListener!!.selectItemView(
position
)
advanceViewType =
if (!isInfinite && isPaginationLoadingNextPage && itemCount == position + 1) VIEW_TYPE_LOADING else VIEW_TYPE_ITEM
if (!isInfinite && isPaginationLoadingNextPage && itemCount == position + 1) VIEW_TYPE_LOADING else viewType
return advanceViewType
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
if (advanceViewType == VIEW_TYPE_ITEM) {
if (itemView != -1) {
val view = LayoutInflater.from(parent.context).inflate(itemView, parent, false)
return ViewHolder(view)
}
return ViewHolder(onAdapterListener!!.onCreateViewHolder()!!)
if (advanceViewType != VIEW_TYPE_LOADING) {
val view = LayoutInflater.from(parent.context).inflate(itemViews[viewType]!!.itemViewId, parent, false)
val a = ViewHolder(view)
a.itemView.id = viewType
return a
}
if (loadingItemView != -1) {
val view = LayoutInflater.from(parent.context).inflate(loadingItemView, parent, false)
Expand All @@ -103,20 +106,21 @@ open class AdvanceRecyclerView : RecyclerView {

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
var position = position
if (advanceViewType != loadingItemView) {
val item = itemViews[holder.itemView.id]!!
item.animation?.let {
val animation = AnimationUtils.loadAnimation(context, it)
holder.itemView.startAnimation(animation)
lastItemPosition = position
}

if (itemAnimRes != -1) {
val animation = AnimationUtils.loadAnimation(context, itemAnimRes)
holder.itemView.startAnimation(animation)
lastItemPosition = position
}

if (advanceViewType == VIEW_TYPE_ITEM) {
if (onAdapterListener == null)
return
if (isInfinite) {
position = getItemPositionOnInfinity(position)
}
onAdapterListener!!.onBindViewHolder(holder, position)

onAdapterListener!!.onBindViewHolder(holder, position, item)
return
}
if (onPaginationLoadingAdapterListener == null)
Expand Down Expand Up @@ -148,18 +152,14 @@ open class AdvanceRecyclerView : RecyclerView {
}

fun init(
@LayoutRes itemView: Int, itemCount: Int = this.itemCount,
linearLayoutManager: LinearLayoutManager? = null
itemCount: Int = this.itemCount,
itemView: ItemView,
vararg itemViews: ItemView
) {
var linearLayoutManager = linearLayoutManager
this.itemView = itemView
linearLayoutManager ?: let {
linearLayoutManager = LinearLayoutManager(context)
this.itemViews[itemView.itemViewId] = itemView
itemViews.forEach {
this.itemViews[it.itemViewId] = it
}
init(itemCount)
}

fun init(itemCount: Int) {

setItemCount(itemCount)
adapter = myAdapter
Expand Down Expand Up @@ -239,10 +239,8 @@ open class AdvanceRecyclerView : RecyclerView {
}

fun notifyItemRemove(itemPosition: Int) {
Log.e("Ari", "notifyItemInsert before remove -> $itemCount")
myAdapter.notifyItemRemoved(itemPosition)
setItemCount(count - 1, false)
Log.e("Ari", "notifyItemInsert before after -> $itemCount")
}

fun addNewItemCount(newItemCount: Int) {
Expand All @@ -267,23 +265,15 @@ open class AdvanceRecyclerView : RecyclerView {
// lm.smoothScrollToPosition(this, null, pos);
}

fun setItemAnimation(@AnimRes animRes: Int = android.R.anim.slide_in_left) {
itemAnimRes = animRes
}

fun enableItemAnim() {
setItemAnimation(android.R.anim.slide_in_left)
}

inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

//Listener
interface OnAdapterListener {
fun onCreateViewHolder(): View? {
return null
abstract class OnAdapterListener {
open fun selectItemView(nextPosition: Int): Int {
return -1
}

fun onBindViewHolder(holder: ViewHolder, position: Int) {
open fun onBindViewHolder(holder: ViewHolder, position: Int, itemView: ItemView) {

}
}
Expand Down Expand Up @@ -336,7 +326,7 @@ open class AdvanceRecyclerView : RecyclerView {
}

companion object {
val VERTICAL = LinearLayoutManager.VERTICAL
val HORIZONTAL = LinearLayoutManager.HORIZONTAL
const val VERTICAL = LinearLayoutManager.VERTICAL
const val HORIZONTAL = LinearLayoutManager.HORIZONTAL
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,32 +109,32 @@ class ItemSwipeCallback(private val mSwipeItemListener: OnSwipeItemListener, swi

private fun a() {
//
// val iconMargin = (itemView.height - leftIcon!!.intrinsicHeight) / 2
// val iconTop = itemView.top + (itemView.height - leftIcon!!.intrinsicHeight) / 2
// val iconMargin = (itemViewId.height - leftIcon!!.intrinsicHeight) / 2
// val iconTop = itemViewId.top + (itemViewId.height - leftIcon!!.intrinsicHeight) / 2
// val iconBottom = iconTop + leftIcon!!.intrinsicHeight
//
// when {
//
//
// dX > 0 -> { // Swiping to the right
// val iconLeft = itemView.left + iconMargin + leftIcon!!.intrinsicWidth
// val iconRight = itemView.left + iconMargin
// val iconLeft = itemViewId.left + iconMargin + leftIcon!!.intrinsicWidth
// val iconRight = itemViewId.left + iconMargin
// leftIcon!!.setBounds(iconLeft, iconTop, iconRight, iconBottom)
//
// leftBackground!!.setBounds(
// itemView.left, itemView.top,
// itemView.left + dX.toInt() + backgroundCornerOffset,
// itemView.bottom
// itemViewId.left, itemViewId.top,
// itemViewId.left + dX.toInt() + backgroundCornerOffset,
// itemViewId.bottom
// )
// }
// dX < 0 -> { // Swiping to the left
// val iconLeft = itemView.right - iconMargin - leftIcon!!.intrinsicWidth
// val iconRight = itemView.right - iconMargin
// val iconLeft = itemViewId.right - iconMargin - leftIcon!!.intrinsicWidth
// val iconRight = itemViewId.right - iconMargin
// leftIcon!!.setBounds(iconLeft, iconTop, iconRight, iconBottom)
//
// leftBackground!!.setBounds(
// itemView.right + dX.toInt() - backgroundCornerOffset,
// itemView.top, itemView.right, itemView.bottom
// itemViewId.right + dX.toInt() - backgroundCornerOffset,
// itemViewId.top, itemViewId.right, itemViewId.bottom
// )
// }
// else -> {// view is unSwiped
Expand All @@ -150,19 +150,19 @@ class ItemSwipeCallback(private val mSwipeItemListener: OnSwipeItemListener, swi
private fun b() {
// if (dX > 0) { // Swiping to the right
// rightIcon?.let {
// val iconMargin = (itemView.height - it.intrinsicHeight) / 2
// val iconTop = itemView.top + (itemView.height - it.intrinsicHeight) / 2
// val iconMargin = (itemViewId.height - it.intrinsicHeight) / 2
// val iconTop = itemViewId.top + (itemViewId.height - it.intrinsicHeight) / 2
// val iconBottom = iconTop + it.intrinsicHeight
//
// val iconLeft = itemView.left + iconMargin + it.intrinsicWidth
// val iconRight = itemView.left + iconMargin
// val iconLeft = itemViewId.left + iconMargin + it.intrinsicWidth
// val iconRight = itemViewId.left + iconMargin
// it.setBounds(iconLeft, iconTop, iconRight, iconBottom)
//
// rightBackground?.let { bg ->
// bg.setBounds(
// itemView.left, itemView.top,
// itemView.left + dX.toInt() + backgroundCornerOffset,
// itemView.bottom
// itemViewId.left, itemViewId.top,
// itemViewId.left + dX.toInt() + backgroundCornerOffset,
// itemViewId.bottom
// )
// bg.draw(c)
// }
Expand All @@ -173,19 +173,19 @@ class ItemSwipeCallback(private val mSwipeItemListener: OnSwipeItemListener, swi
//
// if (dX < 0) { // Swiping to the left
// leftIcon?.let {
// val iconMargin = (itemView.height - it.intrinsicHeight) / 2
// val iconTop = itemView.top + (itemView.height - it.intrinsicHeight) / 2
// val iconMargin = (itemViewId.height - it.intrinsicHeight) / 2
// val iconTop = itemViewId.top + (itemViewId.height - it.intrinsicHeight) / 2
// val iconBottom = iconTop + it.intrinsicHeight
//
// val iconLeft = itemView.right - iconMargin - it.intrinsicWidth
// val iconRight = itemView.right - iconMargin
// val iconLeft = itemViewId.right - iconMargin - it.intrinsicWidth
// val iconRight = itemViewId.right - iconMargin
// leftIcon!!.setBounds(iconLeft, iconTop, iconRight, iconBottom)
//
//
// leftBackground?.let { bg ->
// bg.setBounds(
// itemView.right + dX.toInt() - backgroundCornerOffset,
// itemView.top, itemView.right, itemView.bottom
// itemViewId.right + dX.toInt() - backgroundCornerOffset,
// itemViewId.top, itemViewId.right, itemViewId.bottom
// )
// bg.draw(c)
// }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ir.aryanmo.advancerecyclerview

import android.support.annotation.AnimRes
import android.support.annotation.LayoutRes

class ItemView(
@field:LayoutRes
val itemViewId: Int,
@field:AnimRes
var animation: Int? = null
) {

companion object {
const val DEFAULT_ANIMATION = android.R.anim.slide_in_left
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.View
import android.widget.TextView
import ir.aryanmo.advancerecyclerview.AdvanceRecyclerView
import ir.aryanmo.advancerecyclerview.ItemSwipeCallback
import ir.aryanmo.advancerecyclerview.ItemView
import kotlinx.android.synthetic.main.activity_main.*


Expand All @@ -22,24 +22,26 @@ class MainActivity : AppCompatActivity() {

val myList = getList()

re.onAdapterListener = object : AdvanceRecyclerView.OnAdapterListener {
re.onAdapterListener = object : AdvanceRecyclerView.OnAdapterListener() {
override fun selectItemView(nextPosition: Int): Int {
return if (nextPosition % 10 != 0) R.layout.test_item else R.layout.test_item2
}

override fun onBindViewHolder(holder: AdvanceRecyclerView.ViewHolder, position: Int) {
super.onBindViewHolder(holder, position)
override fun onBindViewHolder(holder: AdvanceRecyclerView.ViewHolder, position: Int, itemView: ItemView) {
super.onBindViewHolder(holder, position, itemView)
holder.itemView.findViewById<TextView>(R.id.text).text = myList[position]
}
}

val itemSwipeCallback = ItemSwipeCallback(
object : ItemSwipeCallback.OnSwipeItemListener {
override fun onSwipeToLeft(position: Int) {
Log.e("Ari","remove -> $position -> ${myList[position]}")
Log.e("Ari", "remove -> $position -> ${myList[position]}")
lastItemPosition = position
lastItemName = myList[position]
Log.e("Ari","myList before remove -> ${myList.size-1}")
Log.e("Ari", "myList before remove -> ${myList.size - 1}")
myList.removeAt(position)
re.notifyItemRemove(position)
Log.e("Ari","myList after remove -> ${myList.size-1}")
Log.e("Ari", "myList after remove -> ${myList.size - 1}")
}

override fun onSwipeToRight(position: Int) {
Expand All @@ -59,21 +61,12 @@ class MainActivity : AppCompatActivity() {
)

re.setSwipeListener(itemSwipeCallback)
re.init(R.layout.test_item, myList.size)

re.setItemAnimation()

re.init(myList.size, ItemView(R.layout.test_item), ItemView(R.layout.test_item2))

goTo.setOnClickListener {
// if (re.isInitialize) {
// myList.add("saom")
// myList.add("hlfg")
// myList.add("sifkpasj")
// myList.add("dop")
// myList.add("rez")
// re.notifyDataSetChanged(myList.size)
// }

myList.add(lastItemPosition,lastItemName)
myList.add(lastItemPosition, lastItemName)
re.notifyItemInsert(lastItemPosition)
}
smoothGoTo.setOnClickListener { re.smoothScrollToPos(10) }
Expand Down
Loading

0 comments on commit 21737dc

Please sign in to comment.