diff --git a/.idea/compiler.xml b/.idea/compiler.xml
index fb7f4a8..61a9130 100644
--- a/.idea/compiler.xml
+++ b/.idea/compiler.xml
@@ -1,6 +1,6 @@
-
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
index ccccd12..e17a734 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -7,7 +7,7 @@
-
+
diff --git a/app/src/main/java/com/jackandphantom/carousellayout/MainActivity.kt b/app/src/main/java/com/jackandphantom/carousellayout/MainActivity.kt
index 1e265f7..218d4ac 100644
--- a/app/src/main/java/com/jackandphantom/carousellayout/MainActivity.kt
+++ b/app/src/main/java/com/jackandphantom/carousellayout/MainActivity.kt
@@ -22,7 +22,7 @@ class MainActivity : AppCompatActivity() {
list.add(DataModel(R.drawable.hobes, "Thi is cool"))
list.add(DataModel(R.drawable.guypro, "Thi is cool"))
list.add(DataModel(R.drawable.joker, "Thi is cool"))
- list.add(DataModel(R.drawable.londonlove, "Thi is cool"))
+ // list.add(DataModel(R.drawable.londonlove, "Thi is cool"))
val adapter = DataAdapter(list)
@@ -30,9 +30,11 @@ class MainActivity : AppCompatActivity() {
this.adapter = adapter
set3DItem(true)
setAlpha(true)
- setInfinite(true)
}
- val carouselLayoutManager = binding.recycler.getCarouselLayoutManager()
- // carouselLayoutManager.scrollToPosition(4)
+
+ //Trigger the button and put your useCase to test different cases of adapter
+ binding.button.setOnClickListener {
+ adapter.removeData()
+ }
}
}
\ No newline at end of file
diff --git a/app/src/main/java/com/jackandphantom/carousellayout/adapter/DataAdapter.kt b/app/src/main/java/com/jackandphantom/carousellayout/adapter/DataAdapter.kt
index 11a2ad5..32b0bd2 100644
--- a/app/src/main/java/com/jackandphantom/carousellayout/adapter/DataAdapter.kt
+++ b/app/src/main/java/com/jackandphantom/carousellayout/adapter/DataAdapter.kt
@@ -9,7 +9,7 @@ import com.bumptech.glide.Glide
import com.example.customviewimple.model.DataModel
import com.jackandphantom.carousellayout.R
-class DataAdapter (private var list : List): RecyclerView.Adapter() {
+class DataAdapter (private var list : ArrayList): RecyclerView.Adapter() {
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val image : ImageView = itemView.findViewById(R.id.image)
@@ -28,8 +28,24 @@ class DataAdapter (private var list : List): RecyclerView.Adapter) {
+ fun updateData(list: ArrayList) {
this.list = list
notifyDataSetChanged()
}
+
+ //Use the method for item changed
+ fun itemChanged() {
+ // remove last item for test purposes
+ this.list[0] = (DataModel(R.drawable.londonlove, "Thi is cool"))
+ notifyItemChanged(0)
+
+ }
+
+ //Use the method for checking the itemRemoved
+ fun removeData() {
+ // remove last item for test purposes
+ val orgListSize = list.size
+ this.list = this.list.subList(0, orgListSize - 1).toList() as ArrayList
+ notifyItemRemoved(orgListSize - 1)
+ }
}
\ No newline at end of file
diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml
index d90afc3..250f78e 100644
--- a/app/src/main/res/layout/activity_main.xml
+++ b/app/src/main/res/layout/activity_main.xml
@@ -1,14 +1,23 @@
-
-
\ No newline at end of file
+
+
+
\ No newline at end of file
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 108510b..f5a0fa0 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -1,3 +1,4 @@
CarouselLayout
+ removeItem
\ No newline at end of file
diff --git a/carouselrecyclerview/src/main/java/com/jackandphantom/carouselrecyclerview/CarouselLayoutManager.kt b/carouselrecyclerview/src/main/java/com/jackandphantom/carouselrecyclerview/CarouselLayoutManager.kt
index 512b0ef..f8b6ec3 100644
--- a/carouselrecyclerview/src/main/java/com/jackandphantom/carouselrecyclerview/CarouselLayoutManager.kt
+++ b/carouselrecyclerview/src/main/java/com/jackandphantom/carouselrecyclerview/CarouselLayoutManager.kt
@@ -464,8 +464,9 @@ class CarouselLayoutManager constructor(
* It's also used in the [CarouselRecyclerview.getChildDrawingOrder] for measure the order of the child
*/
fun getChildActualPos(index: Int): Int {
- val child = getChildAt(index)
- if (child!!.tag != null) {
+ val child = getChildAt(index) ?: return Int.MIN_VALUE
+
+ if (child.tag != null) {
val tag = checkTAG(child.tag)
if (tag != null)
return tag.pos
diff --git a/carouselrecyclerview/src/main/java/com/jackandphantom/carouselrecyclerview/CarouselRecyclerview.kt b/carouselrecyclerview/src/main/java/com/jackandphantom/carouselrecyclerview/CarouselRecyclerview.kt
index f8bef66..87c23e2 100644
--- a/carouselrecyclerview/src/main/java/com/jackandphantom/carouselrecyclerview/CarouselRecyclerview.kt
+++ b/carouselrecyclerview/src/main/java/com/jackandphantom/carouselrecyclerview/CarouselRecyclerview.kt
@@ -84,7 +84,7 @@ class CarouselRecyclerview(context: Context, attributeSet: AttributeSet) : Recyc
/**
* Get the layout manager instance
- * @return CoverLayout
+ * @return carouselLayoutManager
*/
fun getCarouselLayoutManager(): CarouselLayoutManager {
return layoutManager as CarouselLayoutManager
@@ -100,25 +100,23 @@ class CarouselRecyclerview(context: Context, attributeSet: AttributeSet) : Recyc
override fun getChildDrawingOrder(childCount: Int, i: Int): Int {
val center: Int = getCarouselLayoutManager().centerPosition()
- // Get the actual position of the i-th child view in RecyclerView
-
// Get the actual position of the i-th child view in RecyclerView
val actualPos: Int = getCarouselLayoutManager().getChildActualPos(i)
-
- // The number of intervals from the middle item
-
- // The number of intervals from the middle item
- val dist = actualPos - center
-
- var order: Int
- // [< 0] indicates that the item is located to the left of the middle item and can be drawn in order
- // [< 0] indicates that the item is located to the left of the middle item and can be drawn in order
- order = if (dist < 0) {
- i
- } else {
- //[>= 0] It means that the item is located to the right
- // of the middle item, and the order needs to be reversed.
- childCount - 1 - dist
+ var order: Int = i
+
+ if (actualPos != Int.MIN_VALUE) {
+
+ // The number of intervals from the middle item
+ val dist = actualPos - center
+ // [< 0] indicates that the item is located to the left of the middle item and can be drawn in order
+ // [< 0] indicates that the item is located to the left of the middle item and can be drawn in order
+ order = if (dist < 0) {
+ i
+ } else {
+ //[>= 0] It means that the item is located to the right
+ // of the middle item, and the order needs to be reversed.
+ childCount - 1 - dist
+ }
}
if (order < 0) order = 0 else if (order > childCount - 1) order = childCount - 1
@@ -126,6 +124,7 @@ class CarouselRecyclerview(context: Context, attributeSet: AttributeSet) : Recyc
return order
}
+
fun setItemSelectListener(listener: CarouselLayoutManager.OnSelected) {
getCarouselLayoutManager().setOnSelectedListener(listener)
}
@@ -163,7 +162,5 @@ class CarouselRecyclerview(context: Context, attributeSet: AttributeSet) : Recyc
override fun setAdapter(adapter: Adapter<*>?) {
super.setAdapter(adapter)
restorePosition()
-
}
-
}