Skip to content

Commit

Permalink
Make wrapper adapters support onBindViewHolder() which has payloads p…
Browse files Browse the repository at this point in the history
…arameter
  • Loading branch information
h6ah4i committed Nov 7, 2015
1 parent 9c3e81a commit 54332e9
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
import com.h6ah4i.android.widget.advrecyclerview.utils.BaseWrapperAdapter;
import com.h6ah4i.android.widget.advrecyclerview.utils.WrapperAdapterUtils;

import java.util.Collections;
import java.util.List;

class DraggableItemWrapperAdapter<VH extends RecyclerView.ViewHolder> extends BaseWrapperAdapter<VH> implements SwipeableItemAdapter<VH> {
private static final String TAG = "ARVDraggableWrapper";

Expand Down Expand Up @@ -85,7 +88,7 @@ public VH onCreateViewHolder(ViewGroup parent, int viewType) {
}

@Override
public void onBindViewHolder(VH holder, int position) {
public void onBindViewHolder(VH holder, int position, List<Object> payloads) {
if (isDragging()) {
final long draggingItemId = mDraggingItemInfo.id;
final long itemId = holder.getItemId();
Expand Down Expand Up @@ -115,10 +118,10 @@ public void onBindViewHolder(VH holder, int position) {
}

safeUpdateFlags(holder, flags);
super.onBindViewHolder(holder, origPosition);
super.onBindViewHolder(holder, origPosition, payloads);
} else {
safeUpdateFlags(holder, 0);
super.onBindViewHolder(holder, position);
super.onBindViewHolder(holder, position, payloads);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

import java.util.List;

public interface ExpandableItemAdapter<GVH extends RecyclerView.ViewHolder, CVH extends RecyclerView.ViewHolder> {

/**
Expand Down Expand Up @@ -104,6 +106,31 @@ public interface ExpandableItemAdapter<GVH extends RecyclerView.ViewHolder, CVH
*/
void onBindGroupViewHolder(GVH holder, int groupPosition, int viewType);

/**
* Called by RecyclerView to display the group data at the specified position.
* This method should update the contents of the {@link android.support.v7.widget.RecyclerView.ViewHolder#itemView}
* to reflect the item at the given position.
*
* @param holder The ViewHolder which should be updated to represent the contents of the item at the given position in the data set
* @param groupPosition The position of the group item within the adapter's data set
* @param viewType The view type code
* @param payloads A non-null list of merged payloads. Can be empty list if requires full update.
*/
void onBindGroupViewHolder(GVH holder, int groupPosition, int viewType, List<Object> payloads);

/**
* Called by RecyclerView to display the child data at the specified position.
* This method should update the contents of the {@link android.support.v7.widget.RecyclerView.ViewHolder#itemView}
* to reflect the item at the given position.
*
* @param holder The ViewHolder which should be updated to represent the contents of the item at the given position in the data set
* @param groupPosition The position of the group item within the adapter's data set
* @param childPosition The position of the child item within the group
* @param viewType The view type code
* @param payloads A non-null list of merged payloads. Can be empty list if requires full update.
*/
void onBindChildViewHolder(CVH holder, int groupPosition, int childPosition, int viewType, List<Object> payloads);

/**
* Called by RecyclerView to display the child data at the specified position.
* This method should update the contents of the {@link android.support.v7.widget.RecyclerView.ViewHolder#itemView}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@
import com.h6ah4i.android.widget.advrecyclerview.swipeable.RecyclerViewSwipeManager;
import com.h6ah4i.android.widget.advrecyclerview.swipeable.SwipeableItemAdapter;
import com.h6ah4i.android.widget.advrecyclerview.swipeable.action.SwipeResultAction;
import com.h6ah4i.android.widget.advrecyclerview.swipeable.action.SwipeResultActionDefault;
import com.h6ah4i.android.widget.advrecyclerview.utils.BaseWrapperAdapter;
import com.h6ah4i.android.widget.advrecyclerview.utils.WrapperAdapterUtils;

import java.util.List;

class ExpandableRecyclerViewWrapperAdapter
extends BaseWrapperAdapter<RecyclerView.ViewHolder>
implements DraggableItemAdapter<RecyclerView.ViewHolder>,
Expand Down Expand Up @@ -165,7 +166,7 @@ public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType

@SuppressWarnings("unchecked")
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position, List<Object> payloads) {
if (mExpandableItemAdapter == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import com.h6ah4i.android.widget.advrecyclerview.utils.BaseWrapperAdapter;
import com.h6ah4i.android.widget.advrecyclerview.utils.WrapperAdapterUtils;

import java.util.List;

class SwipeableItemWrapperAdapter<VH extends RecyclerView.ViewHolder> extends BaseWrapperAdapter<VH> {
private static final String TAG = "ARVSwipeableWrapper";

Expand Down Expand Up @@ -101,7 +103,7 @@ public VH onCreateViewHolder(ViewGroup parent, int viewType) {
}

@Override
public void onBindViewHolder(VH holder, int position) {
public void onBindViewHolder(VH holder, int position, List<Object> payloads) {
float prevSwipeItemSlideAmount = 0;

if (holder instanceof SwipeableItemViewHolder) {
Expand All @@ -116,10 +118,10 @@ public void onBindViewHolder(VH holder, int position) {
}

safeUpdateFlags(holder, flags);
super.onBindViewHolder(holder, position);
super.onBindViewHolder(holder, position, payloads);
} else {
safeUpdateFlags(holder, 0);
super.onBindViewHolder(holder, position);
super.onBindViewHolder(holder, position, payloads);
}

if (holder instanceof SwipeableItemViewHolder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import com.h6ah4i.android.widget.advrecyclerview.expandable.ExpandableItemAdapter;

import java.util.List;

public abstract class AbstractExpandableItemAdapter<GVH extends RecyclerView.ViewHolder, CVH extends RecyclerView.ViewHolder>
extends RecyclerView.Adapter<RecyclerView.ViewHolder>
implements ExpandableItemAdapter<GVH, CVH> {
Expand Down Expand Up @@ -86,6 +88,22 @@ public final int getItemCount() {
public final void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
}

/**
* {@inheritDoc}
*/
@Override
public void onBindGroupViewHolder(GVH holder, int groupPosition, int viewType, List<Object> payloads) {
onBindGroupViewHolder(holder, groupPosition, viewType);
}

/**
* {@inheritDoc}
*/
@Override
public void onBindChildViewHolder(CVH holder, int groupPosition, int childPosition, int viewType, List<Object> payloads) {
onBindChildViewHolder(holder, groupPosition, childPosition, viewType);
}

/**
* Override this method if need to customize the behavior.
* {@inheritDoc}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import android.view.ViewGroup;

import java.lang.ref.WeakReference;
import java.util.Collections;
import java.util.List;

public class BaseWrapperAdapter<VH extends RecyclerView.ViewHolder>
extends RecyclerView.Adapter<VH> {
Expand All @@ -30,6 +32,8 @@ public class BaseWrapperAdapter<VH extends RecyclerView.ViewHolder>
private RecyclerView.Adapter<VH> mWrappedAdapter;
private BridgeObserver mBridgeObserver;

protected static final List<Object> FULLUPDATE_PAYLOADS = Collections.emptyList();

public BaseWrapperAdapter(RecyclerView.Adapter<VH> adapter) {
mWrappedAdapter = adapter;
mBridgeObserver = new BridgeObserver<>(this);
Expand Down Expand Up @@ -105,8 +109,13 @@ public VH onCreateViewHolder(ViewGroup parent, int viewType) {

@Override
public void onBindViewHolder(VH holder, int position) {
onBindViewHolder(holder, position, FULLUPDATE_PAYLOADS);
}

@Override
public void onBindViewHolder(VH holder, int position, List<Object> payloads) {
if (isWrappedAdapterAlive())
mWrappedAdapter.onBindViewHolder(holder, position);
mWrappedAdapter.onBindViewHolder(holder, position, payloads);
}

@Override
Expand Down

0 comments on commit 54332e9

Please sign in to comment.