Skip to content

Commit

Permalink
Merge pull request #171 from yuyakaido/issue/169
Browse files Browse the repository at this point in the history
RecyclerView.Adapter.notifyItemChanged does't work
  • Loading branch information
yuyakaido authored Dec 21, 2018
2 parents e800a80 + 9cfef30 commit f1e6416
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,31 +190,17 @@ public void run() {
}
}

for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
int position = getPosition(child);
state.cache.put(position, child);
}

for (int i = 0; i < state.cache.size(); i++) {
detachView(state.cache.valueAt(i));
}
removeAndRecycleAllViews(recycler);

final int parentTop = getPaddingTop();
final int parentLeft = getPaddingLeft();
final int parentRight = getWidth() - getPaddingLeft();
final int parentBottom = getHeight() - getPaddingBottom();
for (int i = state.topPosition; i < state.topPosition + setting.visibleCount && i < getItemCount(); i++) {
View child = state.cache.get(i);
if (child == null) {
child = recycler.getViewForPosition(i);
addView(child, 0);
measureChildWithMargins(child, 0, 0);
layoutDecoratedWithMargins(child, parentLeft, parentTop, parentRight, parentBottom);
} else {
attachView(child, 0);
state.cache.remove(i);
}
View child = recycler.getViewForPosition(i);
addView(child, 0);
measureChildWithMargins(child, 0, 0);
layoutDecoratedWithMargins(child, parentLeft, parentTop, parentRight, parentBottom);

resetTranslation(child);
resetScale(child);
Expand All @@ -235,11 +221,6 @@ public void run() {
}
}

for (int i = 0; i < state.cache.size(); i++) {
removeAndRecycleView(state.cache.valueAt(i), recycler);
}
state.cache.clear();

if (state.status == CardStackState.Status.Dragging) {
listener.onCardDragging(state.getDirection(), state.getRatio());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package com.yuyakaido.android.cardstackview.internal;

import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;

import com.yuyakaido.android.cardstackview.Direction;

public class CardStackState {
public SparseArray<View> cache = new SparseArray<>();
public Status status = Status.Idle;
public int width = 0;
public int height = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CardStackAdapter(

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val spot = spots[position]
holder.name.text = spot.name
holder.name.text = "${spot.id}. ${spot.name}"
holder.city.text = spot.city
Glide.with(holder.image)
.load(spot.url)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.support.v4.widget.DrawerLayout
import android.support.v7.app.ActionBarDrawerToggle
import android.support.v7.app.AppCompatActivity
import android.support.v7.util.DiffUtil
import android.support.v7.widget.DefaultItemAnimator
import android.support.v7.widget.Toolbar
import android.util.Log
import android.view.Gravity
Expand Down Expand Up @@ -83,14 +84,12 @@ class MainActivity : AppCompatActivity(), CardStackListener {
navigationView.setNavigationItemSelectedListener { menuItem ->
when (menuItem.itemId) {
R.id.reload -> reload()
R.id.add_one_spot_at_first -> addFirst(1)
R.id.add_two_spots_at_first -> addFirst(2)
R.id.add_one_spot_at_last -> addLast(1)
R.id.add_two_spots_at_last -> addLast(2)
R.id.remove_one_spot_at_first -> removeFirst(1)
R.id.remove_two_spots_at_first -> removeFirst(2)
R.id.remove_one_spot_at_last -> removeLast(1)
R.id.remove_two_spots_at_last -> removeLast(2)
R.id.add_spot_to_first -> addFirst(1)
R.id.add_spot_to_last -> addLast(1)
R.id.remove_spot_from_first -> removeFirst(1)
R.id.remove_spot_from_last -> removeLast(1)
R.id.replace_first_spot -> replace()
R.id.swap_first_for_last -> swap()
}
drawerLayout.closeDrawers()
true
Expand Down Expand Up @@ -148,6 +147,11 @@ class MainActivity : AppCompatActivity(), CardStackListener {
manager.setCanScrollVertical(true)
cardStackView.layoutManager = manager
cardStackView.adapter = adapter
cardStackView.itemAnimator.apply {
if (this is DefaultItemAnimator) {
supportsChangeAnimations = false
}
}
}

private fun paginate() {
Expand Down Expand Up @@ -230,6 +234,32 @@ class MainActivity : AppCompatActivity(), CardStackListener {
result.dispatchUpdatesTo(adapter)
}

private fun replace() {
val old = adapter.getSpots()
val new = mutableListOf<Spot>().apply {
addAll(old)
removeAt(manager.topPosition)
add(manager.topPosition, createSpot())
}
adapter.setSpots(new)
adapter.notifyItemChanged(manager.topPosition)
}

private fun swap() {
val old = adapter.getSpots()
val new = mutableListOf<Spot>().apply {
addAll(old)
val first = removeAt(manager.topPosition)
val last = removeAt(this.size - 1)
add(manager.topPosition, last)
add(first)
}
val callback = SpotDiffCallback(old, new)
val result = DiffUtil.calculateDiff(callback)
adapter.setSpots(new)
result.dispatchUpdatesTo(adapter)
}

private fun createSpot(): Spot {
return Spot(
name = "Yasaka Shrine",
Expand Down
41 changes: 20 additions & 21 deletions sample/src/main/res/menu/navigation_main_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,37 @@

<item
android:id="@+id/reload"
android:title="@string/reload"/>
android:title="@string/reload"
/>

<item
android:id="@+id/add_one_spot_at_first"
android:title="@string/add_one_spot_at_first"/>
android:id="@+id/add_spot_to_first"
android:title="@string/add_spot_to_first"
/>

<item
android:id="@+id/add_two_spots_at_first"
android:title="@string/add_two_spots_at_first"/>
android:id="@+id/add_spot_to_last"
android:title="@string/add_spot_to_last"
/>

<item
android:id="@+id/add_one_spot_at_last"
android:title="@string/add_one_spot_at_last"/>
android:id="@+id/remove_spot_from_first"
android:title="@string/remove_spot_from_first"
/>

<item
android:id="@+id/add_two_spots_at_last"
android:title="@string/add_two_spots_at_last"/>
android:id="@+id/remove_spot_from_last"
android:title="@string/remove_spot_from_last"
/>

<item
android:id="@+id/remove_one_spot_at_first"
android:title="@string/remove_one_spot_at_first"/>
android:id="@+id/replace_first_spot"
android:title="@string/replace_first_spot"
/>

<item
android:id="@+id/remove_two_spots_at_first"
android:title="@string/remove_two_spots_at_first"/>

<item
android:id="@+id/remove_one_spot_at_last"
android:title="@string/remove_one_spot_at_last"/>

<item
android:id="@+id/remove_two_spots_at_last"
android:title="@string/remove_two_spots_at_last"/>
android:id="@+id/swap_first_for_last"
android:title="@string/swap_first_for_last"
/>

</menu>
14 changes: 6 additions & 8 deletions sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
<string name="open_drawer">Open drawer</string>
<string name="close_drawer">Close drawer</string>
<string name="reload">Reload</string>
<string name="add_one_spot_at_first">Add one spot at first</string>
<string name="add_two_spots_at_first">Add two spots at first</string>
<string name="add_one_spot_at_last">Add one spot at last</string>
<string name="add_two_spots_at_last">Add two spots at last</string>
<string name="remove_one_spot_at_first">Remove one spot at first</string>
<string name="remove_two_spots_at_first">Remove two spots at first</string>
<string name="remove_one_spot_at_last">Remove one spot at last</string>
<string name="remove_two_spots_at_last">Remove two spots at last</string>
<string name="add_spot_to_first">Add spot to first</string>
<string name="add_spot_to_last">Add spot to last</string>
<string name="remove_spot_from_first">Remove spot from first</string>
<string name="remove_spot_from_last">Remove spot from last</string>
<string name="replace_first_spot">Replace first spot</string>
<string name="swap_first_for_last">Swap first for last</string>
</resources>

0 comments on commit f1e6416

Please sign in to comment.