Skip to content

Commit

Permalink
Merge pull request #9 from Fueled/item-position-fix
Browse files Browse the repository at this point in the history
Fix logic for setting and checking item position
  • Loading branch information
julien-fueled authored Jul 21, 2017
2 parents 1ae8945 + a8a3d77 commit 1517111
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 22 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.1'
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
8 changes: 3 additions & 5 deletions reclaim/src/main/java/com/fueled/reclaim/BaseItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ public void setPositionInAdapter(int positionInAdapter) {
*
* @return the layout id to be used by the item
*/
abstract public
@LayoutRes
int getLayoutId();
abstract public int getLayoutId();

/**
* Let the caller know the item is the last one in the adapter
Expand Down Expand Up @@ -96,8 +95,7 @@ public void setIsLastItem(boolean isLastItem) {
*
* @param viewHolder the view holder to bind to this item
*/
public void onBindViewHolder(T3 viewHolder, int positionInAdapter) {
this.positionInAdapter = positionInAdapter;
public void onBindViewHolder(T3 viewHolder) {
this.viewHolder = viewHolder;
updateItemViews();
}
Expand Down Expand Up @@ -140,7 +138,7 @@ public T2 getItemHandler() {
* @return the view holder currently attached to this item
*/
public T3 getViewHolder() {
if (viewHolder != null && viewHolder.getItemBoundTo() == getPositionInAdapter()) {
if (viewHolder != null && viewHolder.getAdapterPosition() == getPositionInAdapter()) {
// Make sure the view holder is still bound to this item before returning it.
return viewHolder;
}
Expand Down
9 changes: 0 additions & 9 deletions reclaim/src/main/java/com/fueled/reclaim/BaseViewHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,8 @@
*/
public abstract class BaseViewHolder extends RecyclerView.ViewHolder {

private int itemBoundTo;

public BaseViewHolder(View view) {
super(view);
}

public int getItemBoundTo() {
return itemBoundTo;
}

public void setItemBoundTo(int itemBoundTo) {
this.itemBoundTo = itemBoundTo;
}
}
35 changes: 28 additions & 7 deletions reclaim/src/main/java/com/fueled/reclaim/ItemsViewAdapter.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fueled.reclaim;

import android.content.Context;
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
Expand Down Expand Up @@ -36,7 +37,8 @@ public void addItemsList(List<? extends BaseItem> items) {
int startSize = getItemCount();

this.items.addAll(items);
notifyItemRangeChanged(startSize, items.size());
updateItemPositions(startSize);
notifyItemRangeInserted(startSize, items.size());
}

/**
Expand All @@ -48,6 +50,7 @@ public void addItemsList(List<? extends BaseItem> items) {
*/
public void addItemsList(int position, List<? extends BaseItem> items) {
this.items.addAll(position, items);
updateItemPositions(position);
notifyItemRangeInserted(position, items.size());
}

Expand All @@ -59,6 +62,7 @@ public void addItemsList(int position, List<? extends BaseItem> items) {
public void replaceItems(List<? extends BaseItem> items) {
this.items.clear();
this.items.addAll(items);
updateItemPositions(0);
notifyDataSetChanged();
}

Expand All @@ -73,13 +77,20 @@ public void replaceItems(List<? extends BaseItem> items, boolean useDiffCheck) {
if (!useDiffCheck) {
replaceItems(items);
} else {
DiffChecker.calculateDiff(this.items, items)
.dispatchUpdatesTo(this);
DiffUtil.DiffResult result = DiffChecker.calculateDiff(this.items, items);
this.items.clear();
this.items.addAll(items);
updateItemPositions(0);
result.dispatchUpdatesTo(this);
}
}

public void replaceItemAt(int position, BaseItem item) {
items.set(position, item);
item.setPositionInAdapter(position);
notifyItemChanged(position);
}

/**
* Add item to the adapter.
*
Expand All @@ -97,6 +108,7 @@ public void addItem(BaseItem item) {
*/
public void addItem(int position, BaseItem item) {
items.add(position, item);
updateItemPositions(position);
notifyItemInserted(position);
}

Expand All @@ -107,15 +119,25 @@ public void addItem(int position, BaseItem item) {
*/
public void removeItemAt(int position) {
items.remove(position);
updateItemPositions(position);
notifyItemRemoved(position);
}

/**
* Removes all items inside the recycler adapter view.
*/
public void clearAllRecyclerItems() {
int total = items.size();

items.clear();
notifyDataSetChanged();

notifyItemRangeRemoved(0, total);
}

private void updateItemPositions(int startPosition) {
for (int i = startPosition; i < items.size(); i++) {
items.get(i).setPositionInAdapter(i);
}
}

/**
Expand Down Expand Up @@ -145,8 +167,7 @@ public BaseViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
@Override
@SuppressWarnings("unchecked")
public void onBindViewHolder(BaseViewHolder holder, int position) {
holder.setItemBoundTo(position);
items.get(position).onBindViewHolder(holder, position);
items.get(position).onBindViewHolder(holder);
}

@Override
Expand All @@ -165,4 +186,4 @@ public EmptyViewHolder(View view) {
}
}

}
}

0 comments on commit 1517111

Please sign in to comment.