Leanback框架是基于RecyclerView,所以首先得了解RecyclerView的适配器。在这里,Leanback专门写了一个类ItemBridgeAdapter来适配数据
//########### 核心代码 ##########
public class ItemBridgeAdapter extends RecyclerView.Adapter implements FacetProviderAdapter {
//ItemBridgeAdapter的ViewHolder是继承RecyclerView.ViewHolder的,将持有mPresenter、mHolder、mItem。可以在view的点击、选中监听事件中被传递出来。
public static class ViewHolder extends RecyclerView.ViewHolder implements FacetProvider {
final Presenter mPresenter;
final Presenter.ViewHolder mHolder;
Object mItem;
Object mExtraObject;
public final Presenter getPresenter() {
return mPresenter;
}
public final Presenter.ViewHolder getViewHolder() {
return mHolder;
}
public final Object getItem() {
return mItem;
}
public final Object getExtraObject() {
return mExtraObject;
}
public void setExtraObject(Object object) {
mExtraObject = object;
}
@Override
public Object getFacet(Class<?> facetClass) {
return mHolder.getFacet(facetClass);
}
ViewHolder(Presenter presenter, View view, Presenter.ViewHolder holder) {
super(view);
mPresenter = presenter;
mHolder = holder;
}
}
//设置数据
public void setAdapter(ObjectAdapter adapter) {
if (adapter == mAdapter) {
return;
}
if (mAdapter != null) {
mAdapter.unregisterObserver(mDataObserver);
}
mAdapter = adapter;
if (mAdapter == null) {
notifyDataSetChanged();
return;
}
//观察数据,ObjectAdapter的改变会更新到UI上
mAdapter.registerObserver(mDataObserver);
if (hasStableIds() != mAdapter.hasStableIds()) {
setHasStableIds(mAdapter.hasStableIds());
}
notifyDataSetChanged();
}
@Override
public int getItemViewType(int position) {
//在获取getItemViewType时初始化每个item的presenter
//PresenterSelector是一个presenter选择器,可以设置在自身ItemBridgeAdapter中,也可以设置ObjectAdapter中,优先使用自身的。
PresenterSelector presenterSelector = mPresenterSelector != null
? mPresenterSelector : mAdapter.getPresenterSelector();
//从ObjectAdapter获取到item
Object item = mAdapter.get(position);
//PresenterSelector通过item来获取具体的 presenter,
Presenter presenter = presenterSelector.getPresenter(item);
int type = mPresenters.indexOf(presenter);
if (type < 0) {
//存储每个type的presenter
mPresenters.add(presenter);
type = mPresenters.indexOf(presenter);
onAddPresenter(presenter, type);
if (mAdapterListener != null) {
mAdapterListener.onAddPresenter(presenter, type);
}
}
return type;
}
@Override
public final RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//通过type从存储的列表中拿到每个item的presenter
Presenter presenter = mPresenters.get(viewType);
Presenter.ViewHolder presenterVh;
View view;
//Wrapper 后面分析,此时不需要理会
if (mWrapper != null) {
view = mWrapper.createWrapper(parent);
presenterVh = presenter.onCreateViewHolder(parent);
mWrapper.wrap(view, presenterVh.view);
} else {
presenterVh = presenter.onCreateViewHolder(parent);
view = presenterVh.view;
}
//ViewHolder将持有Presenter、view、Presenter. ViewHolder。
ViewHolder viewHolder = new ViewHolder(presenter, view, presenterVh);
//
onCreate(viewHolder);
return viewHolder;
}
@Override
public final void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (DEBUG) Log.v(TAG, "onBindViewHolder position " + position);
ViewHolder viewHolder = (ViewHolder) holder;
//ViewHolder将持有Item。
viewHolder.mItem = mAdapter.get(position);
//执行Presenter的onBindViewHolder
viewHolder.mPresenter.onBindViewHolder(viewHolder.mHolder, viewHolder.mItem);
onBind(viewHolder);
if (mAdapterListener != null) {
mAdapterListener.onBind(viewHolder);
}
}
@Override
public final void onViewRecycled(RecyclerView.ViewHolder holder) {
ViewHolder viewHolder = (ViewHolder) holder;
//执行Presenter的onUnbindViewHolder
viewHolder.mPresenter.onUnbindViewHolder(viewHolder.mHolder);
onUnbind(viewHolder);
if (mAdapterListener != null) {
mAdapterListener.onUnbind(viewHolder);
}
viewHolder.mItem = null;
}
在ItemBridgeAdapter提到了ObjectAdapter、Presenter、PresenterSelector 。ObjectAdapter提供数据,PresenterSelector根据每个item获取Presenter,Presenter来绑定item数据到ViewHolder中。
网友评论