Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Notify Adapter Changed #29

Merged
merged 5 commits into from
May 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/compiler.xml

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

2 changes: 1 addition & 1 deletion .idea/misc.xml

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

Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@ 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)

binding.recycler.apply {
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()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<DataModel>): RecyclerView.Adapter<DataAdapter.ViewHolder>() {
class DataAdapter (private var list : ArrayList<DataModel>): RecyclerView.Adapter<DataAdapter.ViewHolder>() {

class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val image : ImageView = itemView.findViewById(R.id.image)
Expand All @@ -28,8 +28,24 @@ class DataAdapter (private var list : List<DataModel>): RecyclerView.Adapter<Dat
Glide.with(holder.image).load(list.get(position).img).into(holder.image)
}

fun updateData(list: List<DataModel>) {
fun updateData(list: ArrayList<DataModel>) {
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<DataModel>
notifyItemRemoved(orgListSize - 1)
}
}
13 changes: 11 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<com.jackandphantom.carouselrecyclerview.CarouselRecyclerview
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/recycler"/>

</FrameLayout>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="@string/removeitem" />

</LinearLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<resources>
<string name="app_name">CarouselLayout</string>
<string name="removeitem">removeItem</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -100,32 +100,31 @@ 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

return order
}


fun setItemSelectListener(listener: CarouselLayoutManager.OnSelected) {
getCarouselLayoutManager().setOnSelectedListener(listener)
}
Expand Down Expand Up @@ -163,7 +162,5 @@ class CarouselRecyclerview(context: Context, attributeSet: AttributeSet) : Recyc
override fun setAdapter(adapter: Adapter<*>?) {
super.setAdapter(adapter)
restorePosition()

}

}