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

Create an AdapterProxy to directly call through to underlying Adapter #481

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.facebook.litho.widget;

import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

/**
* A proxy class for RecyclerBinder's internal adapter to directly call through to another
* {@link RecyclerView.Adapter} implementation. Useful for low overhead integration with an existing
* {@link RecyclerView} with mixed Litho and Android views.
*/
public interface AdapterProxy<H extends RecyclerView.ViewHolder> {

// Placeholder RenderInfo to signal RecyclerBinder to call directly to AdapterProxy.
RenderInfo PROXY_RENDER_INFO = new BaseRenderInfo(new BaseRenderInfo.Builder() {}) {
@Override
public boolean rendersComponent() {
return false;
}

@Override
public boolean rendersView() {
return true;
}

@Override
public String getName() {
return "AdapterProxyRenderInfo";
}
};

H onCreateViewHolder(ViewGroup parent, int viewType);

void onBindViewHolder(H holder, int position);

int getItemViewType(int position);

void onViewRecycled(H holder);
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public class RecyclerBinder
@GuardedBy("this")
private final List<ComponentTreeHolder> mAsyncComponentTreeHolders = new ArrayList<>();

@Nullable private final AdapterProxy mAdapterProxy;
private final LayoutInfo mLayoutInfo;
private final RecyclerView.Adapter mInternalAdapter;
private final ComponentContext mComponentContext;
Expand Down Expand Up @@ -392,6 +393,15 @@ public static class Builder {
private boolean canMeasure;
private boolean hscrollAsyncMode = false;
private boolean singleThreadPool = ComponentsConfiguration.useSingleThreadPool;
private AdapterProxy<?> adapterProxy;

/**
* Set an {@link AdapterProxy} to use with this RecyclerBinder.
*/
public Builder adapterProxy(AdapterProxy<?> adapterProxy) {
this.adapterProxy = adapterProxy;
return this;
}

/**
* @param rangeRatio specifies how big a range this binder should try to compute. The range is
Expand Down Expand Up @@ -679,6 +689,7 @@ private static void dispatchRenderCompleteEvent(
private RecyclerBinder(Builder builder) {
mComponentContext = builder.componentContext;
mComponentTreeHolderFactory = builder.componentTreeHolderFactory;
mAdapterProxy = builder.adapterProxy;
mInternalAdapter =
builder.overrideInternalAdapter != null
? builder.overrideInternalAdapter
Expand Down Expand Up @@ -2866,7 +2877,7 @@ public BaseViewHolder(View view, boolean isLithoViewType) {
}
}

private class InternalAdapter extends RecyclerView.Adapter<BaseViewHolder>
private class InternalAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
implements RecyclerBinderAdapter {

InternalAdapter() {
Expand All @@ -2875,7 +2886,7 @@ private class InternalAdapter extends RecyclerView.Adapter<BaseViewHolder>
}

@Override
public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final ViewCreator viewCreator = mRenderInfoViewCreatorController.getViewCreator(viewType);

if (viewCreator != null) {
Expand All @@ -2893,7 +2904,7 @@ public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

@Override
@GuardedBy("RecyclerBinder.this")
public void onBindViewHolder(BaseViewHolder holder, int position) {
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
final int normalizedPosition = getNormalizedPosition(position);

// We can ignore the synchronization here. We'll only add to this from the UiThread.
Expand Down Expand Up @@ -2953,10 +2964,13 @@ public void onPostDraw() {
}
});
}
} else if(mAdapterProxy != null) {
mAdapterProxy.onBindViewHolder(holder, position);
} else {
BaseViewHolder baseViewHolder = (BaseViewHolder) holder;
final ViewBinder viewBinder = renderInfo.getViewBinder();
holder.viewBinder = viewBinder;
viewBinder.bind(holder.itemView);
baseViewHolder.viewBinder = viewBinder;
viewBinder.bind(baseViewHolder.itemView);
}
}

Expand All @@ -2967,6 +2981,8 @@ public int getItemViewType(int position) {
if (renderInfo.rendersComponent()) {
// Special value for LithoViews
return mRenderInfoViewCreatorController.getComponentViewType();
} else if (mAdapterProxy != null) {
return mAdapterProxy.getItemViewType(position);
} else {
return renderInfo.getViewType();
}
Expand All @@ -2985,18 +3001,23 @@ public int getItemCount() {
}

@Override
public void onViewRecycled(BaseViewHolder holder) {
if (holder.isLithoViewType) {
final LithoView lithoView = (LithoView) holder.itemView;
lithoView.unmountAllItems();
lithoView.setComponentTree(null);
lithoView.setInvalidStateLogParamsList(null);
} else {
final ViewBinder viewBinder = holder.viewBinder;
if (viewBinder != null) {
viewBinder.unbind(holder.itemView);
holder.viewBinder = null;
public void onViewRecycled(RecyclerView.ViewHolder holder) {
if (holder instanceof BaseViewHolder) {
BaseViewHolder baseViewHolder = (BaseViewHolder) holder;
if (baseViewHolder.isLithoViewType) {
final LithoView lithoView = (LithoView) baseViewHolder.itemView;
lithoView.unmountAllItems();
lithoView.setComponentTree(null);
lithoView.setInvalidStateLogParamsList(null);
} else {
final ViewBinder viewBinder = baseViewHolder.viewBinder;
if (viewBinder != null) {
viewBinder.unbind(holder.itemView);
baseViewHolder.viewBinder = null;
}
}
} else {
mAdapterProxy.onViewRecycled(holder);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public RenderInfoViewCreatorController(boolean customViewTypeEnabled, int compon

@UiThread
public void maybeTrackViewCreator(RenderInfo renderInfo) {
if (!renderInfo.rendersView()) {
if (!renderInfo.rendersView() || renderInfo == AdapterProxy.PROXY_RENDER_INFO) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems weird! If AdapterProxy is going to be used, maybe then RenderInfo should have a method that will indicate whether it should be tracked, otherwise it risks being unmaintainable in case more RenderInfo children will be added in the future!

return;
}

Expand Down