From 3276ed57b84cfc21c4198e58acbf101206972eb2 Mon Sep 17 00:00:00 2001 From: vivchar Date: Fri, 19 Jan 2018 00:17:15 +0700 Subject: [PATCH] added most popular methods to ViewFinder, added Context to Adapter --- .../example/pages/github/GithubFragment.java | 18 +- .../items/category/CategoryViewRenderer.java | 4 +- .../github/items/fork/ForkViewRenderer.java | 4 +- .../items/list/RecyclerViewRenderer.java | 6 +- .../items/selected/UserViewRenderer.java | 4 +- .../stargazer/StargazerViewRenderer.java | 4 +- .../simple/CompositeViewRendererFragment.java | 6 +- .../pages/simple/DiffUtilFragment.java | 6 +- .../pages/simple/LoadMoreFragment.java | 9 +- .../example/pages/simple/PayloadFragment.java | 8 +- .../pages/simple/ViewBinderFragment.java | 6 +- .../pages/simple/ViewRendererFragment.java | 8 +- .../pages/simple/ViewStateFragment.java | 7 +- .../example/widgets/NestedAdapter.java | 6 + gradle.properties | 4 +- .../CompositeViewRenderer.java | 21 +- .../DiffCallback.java | 6 +- .../LoadMoreViewRenderer.java | 4 +- .../RendererRecyclerViewAdapter.java | 13 + .../ViewFinderImpl.java | 85 ---- .../ViewHolder.java | 10 +- .../ViewRenderer.java | 15 +- .../binder/BinderAdapter.java | 19 + .../binder/CompositeViewBinder.java | 107 +++++ .../binder/LoadMoreViewBinder.java | 36 ++ .../binder/ViewBinder.java | 29 ++ .../binder/ViewFinder.java | 90 +++++ .../binder/ViewFinderFactory.java | 16 + .../binder/ViewFinderImpl.java | 371 ++++++++++++++++++ 29 files changed, 780 insertions(+), 142 deletions(-) delete mode 100644 rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/ViewFinderImpl.java create mode 100644 rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/binder/BinderAdapter.java create mode 100644 rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/binder/ViewFinderFactory.java create mode 100644 rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/binder/ViewFinderImpl.java diff --git a/example/src/main/java/com/github/vivchar/example/pages/github/GithubFragment.java b/example/src/main/java/com/github/vivchar/example/pages/github/GithubFragment.java index 4d5c01e..8c5c591 100644 --- a/example/src/main/java/com/github/vivchar/example/pages/github/GithubFragment.java +++ b/example/src/main/java/com/github/vivchar/example/pages/github/GithubFragment.java @@ -36,6 +36,7 @@ import com.github.vivchar.rendererrecyclerviewadapter.RendererRecyclerViewAdapter; import com.github.vivchar.rendererrecyclerviewadapter.ViewModel; import com.github.vivchar.rendererrecyclerviewadapter.ViewRenderer; +import com.github.vivchar.rendererrecyclerviewadapter.binder.LoadMoreViewBinder; import com.github.vivchar.rendererrecyclerviewadapter.binder.ViewBinder; import com.github.vivchar.rendererrecyclerviewadapter.binder.ViewProvider; @@ -75,9 +76,9 @@ public View onCreateView(final LayoutInflater inflater, mSwipeToRefresh = (SwipeRefreshLayout) inflate.findViewById(R.id.refresh); mSwipeToRefresh.setOnRefreshListener(() -> mGithubPresenter.onRefresh()); - mRecyclerViewAdapter = new RendererRecyclerViewAdapter(); + mRecyclerViewAdapter = new RendererRecyclerViewAdapter(getContext()); mRecyclerViewAdapter.setDiffCallback(new ItemsDiffCallback()); - mRecyclerViewAdapter.registerRenderer(new LoadMoreViewRenderer(R.layout.item_load_more, getContext())); + mRecyclerViewAdapter.registerRenderer(new LoadMoreViewBinder(R.layout.item_load_more)); mRecyclerViewAdapter.registerRenderer(createStargazerRenderer(R.layout.item_user_full_width)); mRecyclerViewAdapter.registerRenderer(createListRenderer() .registerRenderer(createForkRenderer()) @@ -87,7 +88,6 @@ public View onCreateView(final LayoutInflater inflater, mRecyclerViewAdapter.registerRenderer(new ViewBinder<>( R.layout.item_category, CategoryModel.class, - getContext(), (model, finder, payloads) -> finder .find(R.id.title, (ViewProvider) view -> view.setText(model.getName())) .setOnClickListener(R.id.viewAll, (v -> mGithubPresenter.onCategoryClicked(model))) @@ -133,28 +133,28 @@ public void onStop() { @NonNull private ViewRenderer createForkRenderer() { - return new ForkViewRenderer(getContext(), new Listener()); + return new ForkViewRenderer(new Listener()); } @NonNull private ViewRenderer createStargazerRenderer(final int layout) { - return new StargazerViewRenderer(layout, getContext(), new Listener()); + return new StargazerViewRenderer(layout, new Listener()); } @NonNull private ViewRenderer createCategoryRenderer() { - return new CategoryViewRenderer(getContext(), new Listener()); + return new CategoryViewRenderer(new Listener()); } @NonNull private CompositeViewRenderer createListRenderer() { - return new RecyclerViewRenderer(getContext()); + return new RecyclerViewRenderer(); } @NonNull private ViewRenderer createUserRenderer() { /* vivchar: ideally we should use other model */ - return new UserViewRenderer(getContext()); + return new UserViewRenderer(); } @NonNull @@ -191,7 +191,7 @@ public void showMessageView(@NonNull final String message) { @Override public void showSelectedUsers(@NonNull final ArrayList list) { - final RendererRecyclerViewAdapter adapter = new RendererRecyclerViewAdapter(); + final RendererRecyclerViewAdapter adapter = new RendererRecyclerViewAdapter(getContext()); adapter.registerRenderer(createUserRenderer()); final LayoutInflater inflater = LayoutInflater.from(getContext()); diff --git a/example/src/main/java/com/github/vivchar/example/pages/github/items/category/CategoryViewRenderer.java b/example/src/main/java/com/github/vivchar/example/pages/github/items/category/CategoryViewRenderer.java index 4f9b287..894c4f0 100644 --- a/example/src/main/java/com/github/vivchar/example/pages/github/items/category/CategoryViewRenderer.java +++ b/example/src/main/java/com/github/vivchar/example/pages/github/items/category/CategoryViewRenderer.java @@ -17,8 +17,8 @@ public class CategoryViewRenderer extends ViewRenderer { @NonNull private final Listener mListener; - public ForkViewRenderer(final Context context, @NonNull final Listener listener) { - super(ForkModel.class, context); + public ForkViewRenderer(@NonNull final Listener listener) { + super(ForkModel.class); mListener = listener; } diff --git a/example/src/main/java/com/github/vivchar/example/pages/github/items/list/RecyclerViewRenderer.java b/example/src/main/java/com/github/vivchar/example/pages/github/items/list/RecyclerViewRenderer.java index 5c5b8ea..d866caf 100644 --- a/example/src/main/java/com/github/vivchar/example/pages/github/items/list/RecyclerViewRenderer.java +++ b/example/src/main/java/com/github/vivchar/example/pages/github/items/list/RecyclerViewRenderer.java @@ -26,8 +26,8 @@ public class RecyclerViewRenderer extends CompositeViewRenderer { - public UserViewRenderer(@NonNull final Context context) { - super(StargazerModel.class, context); + public UserViewRenderer() { + super(StargazerModel.class); } @Override diff --git a/example/src/main/java/com/github/vivchar/example/pages/github/items/stargazer/StargazerViewRenderer.java b/example/src/main/java/com/github/vivchar/example/pages/github/items/stargazer/StargazerViewRenderer.java index 0ed1a9b..747690c 100644 --- a/example/src/main/java/com/github/vivchar/example/pages/github/items/stargazer/StargazerViewRenderer.java +++ b/example/src/main/java/com/github/vivchar/example/pages/github/items/stargazer/StargazerViewRenderer.java @@ -28,8 +28,8 @@ public class StargazerViewRenderer extends ViewRenderer( R.layout.item_simple_composite, R.id.recycler_view, DefaultCompositeViewModel.class, - getContext(), Collections.singletonList(new BetweenSpacesItemDecoration(10, 10)) ).registerRenderer(getAnyViewRenderer()) ); @@ -62,8 +61,7 @@ private ViewRenderer getAnyViewRenderer() { return new ViewBinder<>( R.layout.item_simple_square, DiffUtilFragment.DiffViewModel.class, - getContext(), - (model, finder, payloads) -> finder.find(R.id.text, (ViewProvider) textView -> textView.setText(model.getText())) + (model, finder, payloads) -> finder.setText(R.id.text, model.getText()) ); } } \ No newline at end of file diff --git a/example/src/main/java/com/github/vivchar/example/pages/simple/DiffUtilFragment.java b/example/src/main/java/com/github/vivchar/example/pages/simple/DiffUtilFragment.java index ac12b5b..5353324 100644 --- a/example/src/main/java/com/github/vivchar/example/pages/simple/DiffUtilFragment.java +++ b/example/src/main/java/com/github/vivchar/example/pages/simple/DiffUtilFragment.java @@ -37,14 +37,14 @@ public View onCreateView(final LayoutInflater inflater, final View view = inflater.inflate(R.layout.fragment_list, container, false); - mAdapter = new RendererRecyclerViewAdapter(); + mAdapter = new RendererRecyclerViewAdapter(getContext()); mAdapter.setDiffCallback(new DiffCallback()); // adapter.enableDiffUtil(); /* Or just call it to enable DiffUtil with DefaultDiffCallback */ - mAdapter.registerRenderer(new ViewBinder<>(R.layout.item_simple_square, DiffViewModel.class, getContext(), + mAdapter.registerRenderer(new ViewBinder<>(R.layout.item_simple_square, DiffViewModel.class, (model, finder, payloads) -> finder - .find(R.id.text, (ViewProvider) textView -> textView.setText(model.getText())) + .setText(R.id.text, model.getText()) .setOnClickListener(R.id.text, v -> { reloadItems(model); }) diff --git a/example/src/main/java/com/github/vivchar/example/pages/simple/LoadMoreFragment.java b/example/src/main/java/com/github/vivchar/example/pages/simple/LoadMoreFragment.java index dff4afc..d5d17a0 100644 --- a/example/src/main/java/com/github/vivchar/example/pages/simple/LoadMoreFragment.java +++ b/example/src/main/java/com/github/vivchar/example/pages/simple/LoadMoreFragment.java @@ -1,6 +1,7 @@ package com.github.vivchar.example.pages.simple; import android.os.Bundle; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.RecyclerView; @@ -14,10 +15,12 @@ import com.github.vivchar.example.R; import com.github.vivchar.example.widgets.EndlessScrollListener; import com.github.vivchar.example.widgets.ItemOffsetDecoration; +import com.github.vivchar.rendererrecyclerviewadapter.binder.BinderAdapter; import com.github.vivchar.rendererrecyclerviewadapter.binder.LoadMoreViewBinder; import com.github.vivchar.rendererrecyclerviewadapter.RendererRecyclerViewAdapter; import com.github.vivchar.rendererrecyclerviewadapter.ViewModel; import com.github.vivchar.rendererrecyclerviewadapter.binder.ViewBinder; +import com.github.vivchar.rendererrecyclerviewadapter.binder.ViewFinder; import com.github.vivchar.rendererrecyclerviewadapter.binder.ViewProvider; import java.lang.reflect.Type; @@ -42,14 +45,14 @@ public View onCreateView(final LayoutInflater inflater, final View view = inflater.inflate(R.layout.fragment_list, container, false); - mAdapter = new RendererRecyclerViewAdapter(); + mAdapter = new RendererRecyclerViewAdapter(getContext()); mAdapter.enableDiffUtil(); // mAdapter.setLoadMoreModel(new YourLoadMoreModel()); /* you can change the LoadMoreModel if needed */ mAdapter.registerRenderer(new LoadMoreViewBinder(R.layout.item_load_more, getContext())); - mAdapter.registerRenderer(new ViewBinder<>(R.layout.item_simple_square, SimpleViewModel.class, getContext(), - (model, finder, payloads) -> finder.find(R.id.text, (ViewProvider) textView -> textView.setText(model.getText())) + mAdapter.registerRenderer(new ViewBinder<>(R.layout.item_simple_square, SimpleViewModel.class, + (model, finder, payloads) -> finder.setText(R.id.text, model.getText()) )); // adapter.registerRenderer(...); // adapter.registerRenderer(...); diff --git a/example/src/main/java/com/github/vivchar/example/pages/simple/PayloadFragment.java b/example/src/main/java/com/github/vivchar/example/pages/simple/PayloadFragment.java index 2caa16e..eb02dd2 100644 --- a/example/src/main/java/com/github/vivchar/example/pages/simple/PayloadFragment.java +++ b/example/src/main/java/com/github/vivchar/example/pages/simple/PayloadFragment.java @@ -40,12 +40,12 @@ public View onCreateView(final LayoutInflater inflater, final View view = inflater.inflate(R.layout.fragment_list, container, false); - mAdapter = new RendererRecyclerViewAdapter(); + mAdapter = new RendererRecyclerViewAdapter(getContext()); mAdapter.setDiffCallback(new PayloadDiffCallback()); mAdapter.registerRenderer(new ViewBinder<>( - R.layout.item_payload_square, PayloadViewModel.class, getContext(), + R.layout.item_payload_square, PayloadViewModel.class, (model, finder, payloads) -> { finder.setOnClickListener(R.id.text, v -> changeItem(model)); final TextView textView = finder.find(R.id.text); @@ -53,7 +53,7 @@ R.layout.item_payload_square, PayloadViewModel.class, getContext(), if (payloads.isEmpty()) { /* full bind */ textView.setText(model.getText()); - finder.find(R.id.desciption, (ViewProvider) desciption -> desciption.setText(model.getDescription())); + finder.setText(R.id.desciption, model.getDescription()); } else { /* partially bind */ final Object payload = payloads.get(0); @@ -62,7 +62,7 @@ R.layout.item_payload_square, PayloadViewModel.class, getContext(), textView.animate().rotation(360).start(); textView.setText(model.getText()); } else if (payload.equals(DESCRIPTION_CHANGED)) { - finder.find(R.id.desciption, (ViewProvider) desciption -> desciption.setText(model.getDescription())); + finder.setText(R.id.desciption, model.getDescription()); } } } diff --git a/example/src/main/java/com/github/vivchar/example/pages/simple/ViewBinderFragment.java b/example/src/main/java/com/github/vivchar/example/pages/simple/ViewBinderFragment.java index 1bed186..8b24907 100644 --- a/example/src/main/java/com/github/vivchar/example/pages/simple/ViewBinderFragment.java +++ b/example/src/main/java/com/github/vivchar/example/pages/simple/ViewBinderFragment.java @@ -30,11 +30,11 @@ public View onCreateView(final LayoutInflater inflater, final View view = inflater.inflate(R.layout.fragment_list, container, false); - final RendererRecyclerViewAdapter adapter = new RendererRecyclerViewAdapter(); + final RendererRecyclerViewAdapter adapter = new RendererRecyclerViewAdapter(getContext()); - adapter.registerRenderer(new ViewBinder<>(R.layout.item_simple, RectViewModel.class, getContext(), + adapter.registerRenderer(new ViewBinder<>(R.layout.item_simple, RectViewModel.class, (model, finder, payloads) -> finder - .find(R.id.text, (ViewProvider) textView -> textView.setText(model.getText())) + .setText(R.id.text, model.getText()) .setOnClickListener(R.id.text, v -> { Toast.makeText(getContext(), "Text Clicked " + model.getText(), Toast.LENGTH_SHORT).show(); }) diff --git a/example/src/main/java/com/github/vivchar/example/pages/simple/ViewRendererFragment.java b/example/src/main/java/com/github/vivchar/example/pages/simple/ViewRendererFragment.java index c0a2b97..dc50356 100644 --- a/example/src/main/java/com/github/vivchar/example/pages/simple/ViewRendererFragment.java +++ b/example/src/main/java/com/github/vivchar/example/pages/simple/ViewRendererFragment.java @@ -34,9 +34,9 @@ public View onCreateView(final LayoutInflater inflater, final View view = inflater.inflate(R.layout.fragment_list, container, false); - final RendererRecyclerViewAdapter adapter = new RendererRecyclerViewAdapter(); + final RendererRecyclerViewAdapter adapter = new RendererRecyclerViewAdapter(getContext()); - adapter.registerRenderer(new RectViewRenderer(RectViewModel.class, getContext())); + adapter.registerRenderer(new RectViewRenderer(RectViewModel.class)); // adapter.registerRenderer(...); // adapter.registerRenderer(...); @@ -51,8 +51,8 @@ public View onCreateView(final LayoutInflater inflater, public static class RectViewRenderer extends ViewRenderer { - public RectViewRenderer(@NonNull final Class type, @NonNull final Context context) { - super(type, context); + public RectViewRenderer(@NonNull final Class type) { + super(type); } @Override diff --git a/example/src/main/java/com/github/vivchar/example/pages/simple/ViewStateFragment.java b/example/src/main/java/com/github/vivchar/example/pages/simple/ViewStateFragment.java index 973f8b8..1c94ad9 100644 --- a/example/src/main/java/com/github/vivchar/example/pages/simple/ViewStateFragment.java +++ b/example/src/main/java/com/github/vivchar/example/pages/simple/ViewStateFragment.java @@ -43,14 +43,13 @@ public View onCreateView(final LayoutInflater inflater, final View view = inflater.inflate(R.layout.fragment_list, container, false); - final RendererRecyclerViewAdapter adapter = new RendererRecyclerViewAdapter(); + final RendererRecyclerViewAdapter adapter = new RendererRecyclerViewAdapter(getContext()); adapter.registerRenderer( new CompositeViewBinder<>( R.layout.item_simple_composite, R.id.recycler_view, StateViewModel.class, - getContext(), Collections.singletonList(new BetweenSpacesItemDecoration(10, 10)), new CompositeViewStateProvider() { @Override @@ -78,8 +77,8 @@ public int createViewStateID(@NonNull final StateViewModel model) { } private ViewRenderer getAnyViewRenderer() { - return new ViewBinder<>(R.layout.item_simple_square, DiffUtilFragment.DiffViewModel.class, getContext(), - (model, finder, payloads) -> finder.find(R.id.text, (ViewProvider) textView -> textView.setText(model.getText())) + return new ViewBinder<>(R.layout.item_simple_square, DiffUtilFragment.DiffViewModel.class, + (model, finder, payloads) -> finder.setText(R.id.text, model.getText()) ); } diff --git a/example/src/main/java/com/github/vivchar/example/widgets/NestedAdapter.java b/example/src/main/java/com/github/vivchar/example/widgets/NestedAdapter.java index e9919d8..2a83d3e 100644 --- a/example/src/main/java/com/github/vivchar/example/widgets/NestedAdapter.java +++ b/example/src/main/java/com/github/vivchar/example/widgets/NestedAdapter.java @@ -1,5 +1,7 @@ package com.github.vivchar.example.widgets; +import android.content.Context; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.util.Log; import android.view.ViewGroup; @@ -17,6 +19,10 @@ public class NestedAdapter extends RendererRecyclerViewAdapter { private static final String TAG = NestedAdapter.class.getSimpleName(); + public NestedAdapter(@NonNull final Context context) { + super(context); + } + @Override public ViewHolder onCreateViewHolder(final ViewGroup parent, final int typeIndex) { final ViewHolder viewHolder = super.onCreateViewHolder(parent, typeIndex); diff --git a/gradle.properties b/gradle.properties index ddb0716..8368377 100644 --- a/gradle.properties +++ b/gradle.properties @@ -15,8 +15,8 @@ org.gradle.jvmargs=-Xmx1536m # This option should only be used with decoupled projects. More details, visit # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects # org.gradle.parallel=true -VERSION_NAME=2.3.1 -VERSION_CODE=24 +VERSION_NAME=2.4.0 +VERSION_CODE=25 GROUP=com.github.vivchar POM_DESCRIPTION=A single adapter with multiple view types for the whole project diff --git a/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/CompositeViewRenderer.java b/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/CompositeViewRenderer.java index 49bd2a2..c74a6a9 100644 --- a/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/CompositeViewRenderer.java +++ b/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/CompositeViewRenderer.java @@ -22,15 +22,34 @@ public abstract class CompositeViewRenderer mRenderers = new ArrayList<>(); + /** + * Please use a constructor without Context + * and set Context via {@link RendererRecyclerViewAdapter#RendererRecyclerViewAdapter(Context)} + */ + @Deprecated public CompositeViewRenderer(@NonNull final Class type, @NonNull final Context context) { super(type, context); } + /** + * Please use a constructor without Context + * and set Context via {@link RendererRecyclerViewAdapter#RendererRecyclerViewAdapter(Context)} + */ + @Deprecated public CompositeViewRenderer(@NonNull final Class type, @NonNull final Context context, @NonNull final ViewRenderer... renderers) { super(type, context); Collections.addAll(mRenderers, renderers); } + public CompositeViewRenderer(@NonNull final Class type) { + super(type); + } + + public CompositeViewRenderer(@NonNull final Class type, @NonNull final ViewRenderer... renderers) { + super(type); + Collections.addAll(mRenderers, renderers); + } + @Override public void bindView(@NonNull final M model, @NonNull final VH holder) { holder.getAdapter().setItems(model.getItems()); @@ -85,7 +104,7 @@ protected RecyclerView.LayoutManager createLayoutManager() { @NonNull protected RendererRecyclerViewAdapter createAdapter() { - return new RendererRecyclerViewAdapter(); + return new RendererRecyclerViewAdapter(getContext()); } @NonNull diff --git a/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/DiffCallback.java b/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/DiffCallback.java index c42ddc4..9a708d7 100644 --- a/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/DiffCallback.java +++ b/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/DiffCallback.java @@ -12,10 +12,10 @@ */ public abstract class DiffCallback extends DiffUtil.Callback { - private final List mOldItems = new ArrayList<>(); - private final List mNewItems = new ArrayList<>(); + protected final List mOldItems = new ArrayList<>(); + protected final List mNewItems = new ArrayList<>(); - void setItems(@NonNull final List oldItems, @NonNull final List newItems) { + public void setItems(@NonNull final List oldItems, @NonNull final List newItems) { mOldItems.clear(); mOldItems.addAll(oldItems); diff --git a/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/LoadMoreViewRenderer.java b/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/LoadMoreViewRenderer.java index e5c44ac..feb07c1 100644 --- a/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/LoadMoreViewRenderer.java +++ b/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/LoadMoreViewRenderer.java @@ -17,8 +17,8 @@ public class LoadMoreViewRenderer extends ViewRenderer { + private Context mContext; @NonNull protected final ArrayList mItems = new ArrayList<>(); @NonNull @@ -43,6 +45,16 @@ public class RendererRecyclerViewAdapter extends RecyclerView.Adapter mCachedViews = new SparseArray<>(); - @NonNull - private final View mItemView; - - ViewFinderImpl(@NonNull final View itemView) { - mItemView = itemView; - } - - @NonNull - @Override - public ViewFinder find(final int ID, @NonNull final ViewProvider viewProvider) { - viewProvider.provide((V) findViewById(ID)); - return this; - } - - @NonNull - @Override - public V find(final int ID) { - return (V) findViewById(ID); - } - - @NonNull - @Override - public ViewFinder getRootView(@NonNull final ViewProvider viewProvider) { - viewProvider.provide((V) mItemView); - return this; - } - - @NonNull - @Override - public V getRootView() { - return (V) mItemView; - } - - @NonNull - @Override - public ViewFinder setOnClickListener(final int ID, @NonNull final View.OnClickListener onClickListener) { - findViewById(ID).setOnClickListener(onClickListener); - return this; - } - - @NonNull - @Override - public ViewFinder setText(final int ID, final CharSequence text) { - ((TextView)find(ID)).setText(text); - return this; - } - - @NonNull - @Override - public ViewFinder setText(final int ID, @StringRes final int textID) { - ((TextView)find(ID)).setText(textID); - return this; - } - - @NonNull - private View findViewById(@IdRes final int ID) { - final View cachedView = mCachedViews.get(ID); - if (cachedView != null) { - return cachedView; - } - final View view = mItemView.findViewById(ID); - mCachedViews.put(ID, view); - return view; - } -} diff --git a/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/ViewHolder.java b/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/ViewHolder.java index a2ae8ad..717de9e 100644 --- a/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/ViewHolder.java +++ b/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/ViewHolder.java @@ -1,10 +1,12 @@ package com.github.vivchar.rendererrecyclerviewadapter; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.v7.widget.RecyclerView; import android.view.View; import com.github.vivchar.rendererrecyclerviewadapter.binder.ViewFinder; +import com.github.vivchar.rendererrecyclerviewadapter.binder.ViewFinderFactory; /** * Created by Vivchar Vitaly on 12/26/17. @@ -15,16 +17,18 @@ public class ViewHolder extends RecyclerView.ViewHolder { private int mViewStateID = UNDEFINED; private Class mType; - @NonNull - private final ViewFinder mViewFinder; + @Nullable + private ViewFinder mViewFinder; public ViewHolder(final View itemView) { super(itemView); - mViewFinder = new ViewFinderImpl(itemView); } @NonNull public ViewFinder getViewFinder() { + if (mViewFinder == null) { + mViewFinder = ViewFinderFactory.create(itemView); + } return mViewFinder; } diff --git a/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/ViewRenderer.java b/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/ViewRenderer.java index 6dd02e3..40b1ce7 100644 --- a/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/ViewRenderer.java +++ b/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/ViewRenderer.java @@ -22,13 +22,26 @@ public abstract class ViewRenderer @NonNull private final Type mType; @NonNull - private final Context mContext; + private Context mContext; + /** + * Please use a constructor without Context + * and set Context via {@link RendererRecyclerViewAdapter#RendererRecyclerViewAdapter(Context)} + */ + @Deprecated public ViewRenderer(@NonNull final Class type, @NonNull final Context context) { mType = type; mContext = context; } + public ViewRenderer(@NonNull final Class type) { + mType = type; + } + + public void setContext(@NonNull final Context context) { + mContext = context; + } + @NonNull protected Context getContext() { return mContext; diff --git a/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/binder/BinderAdapter.java b/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/binder/BinderAdapter.java new file mode 100644 index 0000000..86b8468 --- /dev/null +++ b/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/binder/BinderAdapter.java @@ -0,0 +1,19 @@ +package com.github.vivchar.rendererrecyclerviewadapter.binder; + +import android.support.annotation.NonNull; + +import java.util.List; + +/** + * Created by Vivchar Vitaly on 18.01.18. + */ + +public abstract class BinderAdapter implements ViewBinder.Binder { + + @Override + public void bindView(@NonNull final M model, @NonNull final ViewFinder finder, @NonNull final List payloads) { + bindView(model, finder); + } + + public void bindView(@NonNull final M model, @NonNull final ViewFinder finder) {} +} diff --git a/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/binder/CompositeViewBinder.java b/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/binder/CompositeViewBinder.java index 62d7656..8303412 100644 --- a/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/binder/CompositeViewBinder.java +++ b/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/binder/CompositeViewBinder.java @@ -11,6 +11,7 @@ import com.github.vivchar.rendererrecyclerviewadapter.CompositeViewHolder; import com.github.vivchar.rendererrecyclerviewadapter.CompositeViewModel; import com.github.vivchar.rendererrecyclerviewadapter.CompositeViewRenderer; +import com.github.vivchar.rendererrecyclerviewadapter.RendererRecyclerViewAdapter; import com.github.vivchar.rendererrecyclerviewadapter.ViewRenderer; import com.github.vivchar.rendererrecyclerviewadapter.ViewState; @@ -34,6 +35,11 @@ public class CompositeViewBinder extends Composit @Nullable private CompositeViewStateProvider mViewStateProvider = null; + /** + * Please use a constructor without Context + * and set Context via {@link RendererRecyclerViewAdapter#RendererRecyclerViewAdapter(Context)} + */ + @Deprecated public CompositeViewBinder(final int layoutID, @IdRes final int recyclerViewID, @NonNull final Class type, @@ -49,6 +55,11 @@ public CompositeViewBinder(final int layoutID, mDecorations.addAll(decorations); } + /** + * Please use a constructor without Context + * and set Context via {@link RendererRecyclerViewAdapter#RendererRecyclerViewAdapter(Context)} + */ + @Deprecated public CompositeViewBinder(final int layoutID, @IdRes final int recyclerViewID, @NonNull final Class type, @@ -60,6 +71,11 @@ public CompositeViewBinder(final int layoutID, mBinder = binder; } + /** + * Please use a constructor without Context + * and set Context via {@link RendererRecyclerViewAdapter#RendererRecyclerViewAdapter(Context)} + */ + @Deprecated public CompositeViewBinder(final int layoutID, @IdRes final int recyclerViewID, @NonNull final Class type, @@ -72,6 +88,11 @@ public CompositeViewBinder(final int layoutID, mBinder = binder; } + /** + * Please use a constructor without Context + * and set Context via {@link RendererRecyclerViewAdapter#RendererRecyclerViewAdapter(Context)} + */ + @Deprecated public CompositeViewBinder(final int layoutID, @IdRes final int recyclerViewID, @NonNull final Class type, @@ -82,6 +103,11 @@ public void bindView(@NonNull final M model, @NonNull final ViewFinder finder, @ }); } + /** + * Please use a constructor without Context + * and set Context via {@link RendererRecyclerViewAdapter#RendererRecyclerViewAdapter(Context)} + */ + @Deprecated public CompositeViewBinder(final int layoutID, @IdRes final int recyclerViewID, @NonNull final Class type, @@ -91,6 +117,11 @@ public CompositeViewBinder(final int layoutID, mDecorations.addAll(decorations); } + /** + * Please use a constructor without Context + * and set Context via {@link RendererRecyclerViewAdapter#RendererRecyclerViewAdapter(Context)} + */ + @Deprecated public CompositeViewBinder(final int layoutID, @IdRes final int recyclerViewID, @NonNull final Class type, @@ -100,6 +131,11 @@ public CompositeViewBinder(final int layoutID, mViewStateProvider = viewStateProvider; } + /** + * Please use a constructor without Context + * and set Context via {@link RendererRecyclerViewAdapter#RendererRecyclerViewAdapter(Context)} + */ + @Deprecated public CompositeViewBinder(final int layoutID, @IdRes final int recyclerViewID, @NonNull final Class type, @@ -111,6 +147,77 @@ public CompositeViewBinder(final int layoutID, mDecorations.addAll(decorations); } + + public CompositeViewBinder(final int layoutID, + @IdRes final int recyclerViewID, + @NonNull final Class type, + @NonNull final ViewBinder.Binder binder, + @NonNull final List decorations, + @Nullable final CompositeViewStateProvider viewStateProvider) { + super(type); + mLayoutID = layoutID; + mRecyclerViewID = recyclerViewID; + mBinder = binder; + mViewStateProvider = viewStateProvider; + mDecorations.addAll(decorations); + } + + public CompositeViewBinder(final int layoutID, + @IdRes final int recyclerViewID, + @NonNull final Class type, + @NonNull final ViewBinder.Binder binder) { + super(type); + mLayoutID = layoutID; + mRecyclerViewID = recyclerViewID; + mBinder = binder; + } + + public CompositeViewBinder(final int layoutID, + @IdRes final int recyclerViewID, + @NonNull final Class type, + @NonNull final ViewBinder.Binder binder, + @NonNull final ViewRenderer... renderers) { + super(type, renderers); + mLayoutID = layoutID; + mRecyclerViewID = recyclerViewID; + mBinder = binder; + } + + public CompositeViewBinder(final int layoutID, + @IdRes final int recyclerViewID, + @NonNull final Class type) { + this(layoutID, recyclerViewID, type, new ViewBinder.Binder() { + @Override + public void bindView(@NonNull final M model, @NonNull final ViewFinder finder, @NonNull final List payloads) {} + }); + } + + public CompositeViewBinder(final int layoutID, + @IdRes final int recyclerViewID, + @NonNull final Class type, + @NonNull final List decorations) { + this(layoutID, recyclerViewID, type); + mDecorations.addAll(decorations); + } + + public CompositeViewBinder(final int layoutID, + @IdRes final int recyclerViewID, + @NonNull final Class type, + @Nullable final CompositeViewStateProvider viewStateProvider) { + this(layoutID, recyclerViewID, type); + mViewStateProvider = viewStateProvider; + } + + public CompositeViewBinder(final int layoutID, + @IdRes final int recyclerViewID, + @NonNull final Class type, + @NonNull final List decorations, + @Nullable final CompositeViewStateProvider viewStateProvider) { + this(layoutID, recyclerViewID, type); + mViewStateProvider = viewStateProvider; + mDecorations.addAll(decorations); + } + @Override public void bindView(@NonNull final M model, @NonNull final CompositeViewHolder holder) { super.bindView(model, holder); diff --git a/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/binder/LoadMoreViewBinder.java b/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/binder/LoadMoreViewBinder.java index 5a2f2e0..c638fb9 100644 --- a/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/binder/LoadMoreViewBinder.java +++ b/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/binder/LoadMoreViewBinder.java @@ -4,6 +4,7 @@ import android.support.annotation.NonNull; import com.github.vivchar.rendererrecyclerviewadapter.LoadMoreViewModel; +import com.github.vivchar.rendererrecyclerviewadapter.RendererRecyclerViewAdapter; import java.util.List; @@ -13,6 +14,11 @@ public class LoadMoreViewBinder extends ViewBinder { + /** + * Please use a constructor without Context + * and set Context via {@link RendererRecyclerViewAdapter#RendererRecyclerViewAdapter(Context)} + */ + @Deprecated public LoadMoreViewBinder(final int layoutID, @NonNull final Class type, @NonNull final Context context, @@ -20,6 +26,11 @@ public LoadMoreViewBinder(final int layoutID, super(layoutID, type, context, binder); } + /** + * Please use a constructor without Context + * and set Context via {@link RendererRecyclerViewAdapter#RendererRecyclerViewAdapter(Context)} + */ + @Deprecated public LoadMoreViewBinder(final int layoutID, @NonNull final Class type, @NonNull final Context context) { @@ -31,7 +42,32 @@ public void bindView(@NonNull final LoadMoreViewModel model, }); } + /** + * Please use a constructor without Context + * and set Context via {@link RendererRecyclerViewAdapter#RendererRecyclerViewAdapter(Context)} + */ + @Deprecated public LoadMoreViewBinder(final int layoutID, @NonNull final Context context) { this(layoutID, LoadMoreViewModel.class, context); } + + public LoadMoreViewBinder(final int layoutID, + @NonNull final Class type, + @NonNull final Binder binder) { + super(layoutID, type, binder); + } + + public LoadMoreViewBinder(final int layoutID, + @NonNull final Class type) { + this(layoutID, type, new Binder() { + @Override + public void bindView(@NonNull final LoadMoreViewModel model, + @NonNull final ViewFinder finder, + @NonNull final List payloads) {} + }); + } + + public LoadMoreViewBinder(final int layoutID) { + this(layoutID, LoadMoreViewModel.class); + } } \ No newline at end of file diff --git a/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/binder/ViewBinder.java b/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/binder/ViewBinder.java index 720188a..c6470ef 100644 --- a/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/binder/ViewBinder.java +++ b/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/binder/ViewBinder.java @@ -6,6 +6,7 @@ import android.support.annotation.Nullable; import android.view.ViewGroup; +import com.github.vivchar.rendererrecyclerviewadapter.RendererRecyclerViewAdapter; import com.github.vivchar.rendererrecyclerviewadapter.ViewHolder; import com.github.vivchar.rendererrecyclerviewadapter.ViewModel; import com.github.vivchar.rendererrecyclerviewadapter.ViewRenderer; @@ -27,6 +28,11 @@ public class ViewBinder extends ViewRenderer mViewStateProvider = null; + /** + * Please use a constructor without Context + * and set Context via {@link RendererRecyclerViewAdapter#RendererRecyclerViewAdapter(Context)} + */ + @Deprecated public ViewBinder(@LayoutRes final int layoutID, @NonNull final Class type, @NonNull final Context context, @@ -36,6 +42,11 @@ public ViewBinder(@LayoutRes final int layoutID, mBinder = binder; } + /** + * Please use a constructor without Context + * and set Context via {@link RendererRecyclerViewAdapter#RendererRecyclerViewAdapter(Context)} + */ + @Deprecated public ViewBinder(@LayoutRes final int layoutID, @NonNull final Class type, @NonNull final Context context, @@ -47,6 +58,24 @@ public ViewBinder(@LayoutRes final int layoutID, mViewStateProvider = viewStateProvider; } + public ViewBinder(@LayoutRes final int layoutID, + @NonNull final Class type, + @NonNull final Binder binder) { + super(type); + mLayoutID = layoutID; + mBinder = binder; + } + + public ViewBinder(@LayoutRes final int layoutID, + @NonNull final Class type, + @NonNull final Binder binder, + @Nullable final ViewStateProvider viewStateProvider) { + super(type); + mLayoutID = layoutID; + mBinder = binder; + mViewStateProvider = viewStateProvider; + } + @Override public void bindView(@NonNull final M model, @NonNull final ViewHolder finder) { mBinder.bindView(model, finder.getViewFinder(), new ArrayList<>()); diff --git a/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/binder/ViewFinder.java b/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/binder/ViewFinder.java index 0269fcd..f2a710d 100644 --- a/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/binder/ViewFinder.java +++ b/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/binder/ViewFinder.java @@ -1,9 +1,20 @@ package com.github.vivchar.rendererrecyclerviewadapter.binder; +import android.content.res.ColorStateList; +import android.graphics.Bitmap; +import android.graphics.ColorFilter; +import android.graphics.PorterDuff; +import android.graphics.Typeface; +import android.graphics.drawable.Drawable; +import android.net.Uri; +import android.support.annotation.ColorInt; +import android.support.annotation.DrawableRes; import android.support.annotation.IdRes; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.annotation.StringRes; import android.view.View; +import android.widget.TextView; /** * Created by Vivchar Vitaly on 29.12.17. @@ -11,6 +22,7 @@ * More detail you can get there: https://github.com/vivchar/ViewFinder */ +@SuppressWarnings({"unused", "UnusedReturnValue"}) public interface ViewFinder { @NonNull @@ -24,7 +36,85 @@ public interface ViewFinder { @NonNull ViewFinder setOnClickListener(@IdRes int ID, @NonNull View.OnClickListener onClickListener); @NonNull + ViewFinder setOnLongClickListener(@IdRes int ID, @NonNull View.OnLongClickListener onClickListener); + @NonNull + ViewFinder setOnClickListener(@NonNull View.OnClickListener onClickListener); + @NonNull ViewFinder setText(@IdRes int ID, CharSequence text); @NonNull ViewFinder setText(@IdRes int ID, @StringRes int textID); + @NonNull + ViewFinder setText(@IdRes int ID, CharSequence text, TextView.BufferType type); + @NonNull + ViewFinder setTextSize(@IdRes int ID, float size); + @NonNull + ViewFinder setTypeface(int ID, Typeface typeface, int style); + @NonNull + ViewFinder setTypeface(int ID, Typeface typeface); + @NonNull + ViewFinder setError(@IdRes int ID, CharSequence error); + @NonNull + ViewFinder setError(@IdRes int ID, CharSequence error, Drawable icon); + @NonNull + ViewFinder setTextColor(@IdRes int ID, @ColorInt int color); + @NonNull + ViewFinder setTextColor(@IdRes int ID, ColorStateList colors); + @NonNull + ViewFinder setBackgroundColor(@IdRes int ID, @ColorInt int color); + @NonNull + ViewFinder setForeground(@IdRes int ID, Drawable background); + @NonNull + ViewFinder setBackground(@IdRes int ID, Drawable background); + @NonNull + ViewFinder setBackgroundDrawable(@IdRes int ID, Drawable background); + @NonNull + ViewFinder setBackgroundResource(@IdRes int ID, @DrawableRes int background); + @NonNull + ViewFinder setImageDrawable(@IdRes int ID, @Nullable Drawable drawable); + @NonNull + ViewFinder setImageResource(@IdRes int ID, @DrawableRes int resId); + @NonNull + ViewFinder setImageURI(@IdRes int ID, @Nullable Uri uri); + @NonNull + ViewFinder setImageBitmap(@IdRes int ID, Bitmap bm); + @NonNull + ViewFinder setColorFilter(@IdRes int ID, ColorFilter cf); + @NonNull + ViewFinder setColorFilter(@IdRes int ID, int color, PorterDuff.Mode mode); + @NonNull + ViewFinder clearColorFilter(@IdRes int ID); + @NonNull + ViewFinder setVisible(@IdRes int ID, boolean visible); + @NonNull + ViewFinder setInvisible(@IdRes int ID, boolean invisible); + @NonNull + ViewFinder setGone(@IdRes int ID, boolean gone); + @NonNull + ViewFinder setVisibility(@IdRes int ID, int visibility); + @NonNull + ViewFinder setAlpha(@IdRes int ID, int alpha); + @NonNull + ViewFinder addView(@IdRes int ID, View child); + @NonNull + ViewFinder addView(@IdRes int ID, View child, int index); + @NonNull + ViewFinder removeView(@IdRes int ID, View view); + @NonNull + ViewFinder removeViewAt(@IdRes int ID, int index); + @NonNull + ViewFinder removeAllViews(@IdRes int ID); + @NonNull + ViewFinder setEnabled(int ID, boolean enable); + @NonNull + ViewFinder setEnabled(int ID); + @NonNull + ViewFinder setDisabled(int ID); + @NonNull + ViewFinder setTag(int ID, Object tag); + @NonNull + ViewFinder setChecked(int ID, boolean checked); + @NonNull + ViewFinder setSelected(int ID, boolean selected); + @NonNull + ViewFinder setPressed(int ID, boolean pressed); } \ No newline at end of file diff --git a/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/binder/ViewFinderFactory.java b/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/binder/ViewFinderFactory.java new file mode 100644 index 0000000..3fe8a0b --- /dev/null +++ b/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/binder/ViewFinderFactory.java @@ -0,0 +1,16 @@ +package com.github.vivchar.rendererrecyclerviewadapter.binder; + +import android.support.annotation.NonNull; +import android.view.View; + +/** + * Created by Vivchar Vitaly on 18.01.18. + */ + +public class ViewFinderFactory { + + public static ViewFinder create(@NonNull final View itemView) { + //TODO + return new ViewFinderImpl(itemView); + } +} diff --git a/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/binder/ViewFinderImpl.java b/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/binder/ViewFinderImpl.java new file mode 100644 index 0000000..7371a47 --- /dev/null +++ b/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/binder/ViewFinderImpl.java @@ -0,0 +1,371 @@ +package com.github.vivchar.rendererrecyclerviewadapter.binder; + +import android.content.res.ColorStateList; +import android.graphics.Bitmap; +import android.graphics.ColorFilter; +import android.graphics.PorterDuff; +import android.graphics.Typeface; +import android.graphics.drawable.Drawable; +import android.net.Uri; +import android.os.Build; +import android.support.annotation.ColorInt; +import android.support.annotation.DrawableRes; +import android.support.annotation.IdRes; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.annotation.RequiresApi; +import android.support.annotation.StringRes; +import android.util.SparseArray; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Checkable; +import android.widget.ImageView; +import android.widget.TextView; + + +/** + * Created by Vivchar Vitaly on 29.12.17. + */ +@SuppressWarnings("unchecked") +class ViewFinderImpl implements ViewFinder { + + @NonNull + private final SparseArray mCachedViews = new SparseArray<>(); + @NonNull + private final View mItemView; + + ViewFinderImpl(@NonNull final View itemView) { + mItemView = itemView; + } + + @NonNull + @Override + public ViewFinder find(final int ID, @NonNull final ViewProvider viewProvider) { + viewProvider.provide((V) findViewById(ID)); + return this; + } + + @NonNull + @Override + public V find(final int ID) { + return (V) findViewById(ID); + } + + @NonNull + @Override + public ViewFinder getRootView(@NonNull final ViewProvider viewProvider) { + viewProvider.provide((V) mItemView); + return this; + } + + @NonNull + @Override + public V getRootView() { + return (V) mItemView; + } + + @NonNull + @Override + public ViewFinder setOnClickListener(final int ID, @NonNull final View.OnClickListener onClickListener) { + findViewById(ID).setOnClickListener(onClickListener); + return this; + } + + @NonNull + @Override + public ViewFinder setOnLongClickListener(final int ID, @NonNull final View.OnLongClickListener onClickListener) { + find(ID).setOnLongClickListener(onClickListener); + return this; + } + + @NonNull + @Override + public ViewFinder setOnClickListener(@NonNull final View.OnClickListener onClickListener) { + getRootView().setOnClickListener(onClickListener); + return this; + } + + @NonNull + @Override + public ViewFinder setText(final int ID, final CharSequence text) { + ((TextView) find(ID)).setText(text); + return this; + } + + @NonNull + @Override + public ViewFinder setText(final int ID, @StringRes final int textID) { + ((TextView) find(ID)).setText(textID); + return this; + } + + @NonNull + @Override + public ViewFinder setText(final int ID, final CharSequence text, final TextView.BufferType type) { + ((TextView) find(ID)).setText(text, type); + return this; + } + + @NonNull + @Override + public ViewFinder setTextSize(final int ID, final float size) { + ((TextView) find(ID)).setTextSize(size); + return this; + } + + @NonNull + @Override + public ViewFinder setTypeface(final int ID, final Typeface typeface, final int style) { + ((TextView) find(ID)).setTypeface(typeface, style); + return this; + } + + @NonNull + @Override + public ViewFinder setTypeface(final int ID, final Typeface typeface) { + ((TextView) find(ID)).setTypeface(typeface); + return this; + } + + @NonNull + @Override + public ViewFinder setError(final int ID, final CharSequence error) { + ((TextView) find(ID)).setError(error); + return this; + } + + @NonNull + @Override + public ViewFinder setError(final int ID, final CharSequence error, final Drawable icon) { + ((TextView) find(ID)).setError(error, icon); + return this; + } + + @NonNull + @Override + public ViewFinder setTextColor(final int ID, final int color) { + ((TextView) find(ID)).setTextColor(color); + return this; + } + + @NonNull + @Override + public ViewFinder setTextColor(final int ID, final ColorStateList colors) { + ((TextView) find(ID)).setTextColor(colors); + return this; + } + + @NonNull + @Override + public ViewFinder setBackgroundColor(int ID, @ColorInt final int color) { + find(ID).setBackgroundColor(color); + return this; + } + + @RequiresApi(api = Build.VERSION_CODES.M) + @NonNull + @Override + public ViewFinder setForeground(final int ID, final Drawable background) { + find(ID).setForeground(background); + return this; + } + + @NonNull + @Override + public ViewFinder setBackground(final int ID, final Drawable background) { + find(ID).setBackgroundDrawable(background); + return this; + } + + @NonNull + @Override + public ViewFinder setBackgroundDrawable(final int ID, final Drawable background) { + return setBackground(ID, background); + } + + @NonNull + @Override + public ViewFinder setBackgroundResource(final int ID, @DrawableRes final int background) { + find(ID).setBackgroundResource(background); + return this; + } + + @NonNull + @Override + public ViewFinder setImageDrawable(final int ID, @Nullable final Drawable drawable) { + ((ImageView) find(ID)).setImageDrawable(drawable); + return this; + } + + @NonNull + @Override + public ViewFinder setImageResource(final int ID, final int resId) { + ((ImageView) find(ID)).setImageResource(resId); + return this; + } + + @NonNull + @Override + public ViewFinder setImageURI(final int ID, @Nullable final Uri uri) { + ((ImageView) find(ID)).setImageURI(uri); + return this; + } + + @NonNull + @Override + public ViewFinder setImageBitmap(final int ID, final Bitmap bm) { + ((ImageView) find(ID)).setImageBitmap(bm); + return this; + } + + @NonNull + @Override + public ViewFinder setColorFilter(final int ID, final ColorFilter cf) { + ((ImageView) find(ID)).setColorFilter(cf); + return this; + } + + @NonNull + @Override + public ViewFinder setColorFilter(final int ID, final int color, final PorterDuff.Mode mode) { + ((ImageView) find(ID)).setColorFilter(color, mode); + return this; + } + + @NonNull + @Override + public ViewFinder clearColorFilter(final int ID) { + ((ImageView) find(ID)).clearColorFilter(); + return this; + } + + @NonNull + @Override + public ViewFinder setVisible(final int ID, final boolean visible) { + find(ID).setVisibility(visible ? View.VISIBLE : View.INVISIBLE); + return this; + } + + @NonNull + @Override + public ViewFinder setInvisible(final int ID, final boolean invisible) { + find(ID).setVisibility(invisible ? View.INVISIBLE : View.VISIBLE); + return this; + } + + @NonNull + @Override + public ViewFinder setGone(final int ID, final boolean gone) { + find(ID).setVisibility(gone ? View.GONE : View.VISIBLE); + return this; + } + + @NonNull + @Override + public ViewFinder setVisibility(final int ID, final int visibility) { + find(ID).setVisibility(visibility); + return this; + } + + @NonNull + @Override + public ViewFinder setAlpha(final int ID, final int alpha) { + find(ID).setAlpha(alpha); + return this; + } + + @NonNull + @Override + public ViewFinder addView(final int ID, final View child) { + ((ViewGroup) find(ID)).addView(child); + return this; + } + + @NonNull + @Override + public ViewFinder addView(final int ID, final View child, final int index) { + ((ViewGroup) find(ID)).addView(child, index); + return this; + } + + @NonNull + @Override + public ViewFinder removeView(final int ID, final View view) { + ((ViewGroup) find(ID)).removeView(view); + return this; + } + + @NonNull + @Override + public ViewFinder removeViewAt(final int ID, final int index) { + ((ViewGroup) find(ID)).removeViewAt(index); + return this; + } + + @NonNull + @Override + public ViewFinder removeAllViews(final int ID) { + ((ViewGroup) find(ID)).removeAllViews(); + return this; + } + + @NonNull + @Override + public ViewFinder setEnabled(final int ID, final boolean enable) { + find(ID).setEnabled(enable); + return this; + } + + @NonNull + @Override + public ViewFinder setEnabled(final int ID) { + find(ID).setEnabled(true); + return this; + } + + @NonNull + @Override + public ViewFinder setDisabled(final int ID) { + find(ID).setEnabled(false); + return this; + } + + @NonNull + @Override + public ViewFinder setTag(final int ID, final Object tag) { + find(ID).setTag(tag); + return this; + } + + @NonNull + @Override + public ViewFinder setChecked(final int ID, final boolean checked) { + ((Checkable) find(ID)).setChecked(checked); + return this; + } + + @NonNull + @Override + public ViewFinder setSelected(final int ID, final boolean selected) { + find(ID).setSelected(selected); + return this; + } + + @NonNull + @Override + public ViewFinder setPressed(final int ID, final boolean pressed) { + find(ID).setPressed(pressed); + return this; + } + + @NonNull + private View findViewById(@IdRes final int ID) { + final View cachedView = mCachedViews.get(ID); + if (cachedView != null) { + return cachedView; + } + final View view = mItemView.findViewById(ID); + mCachedViews.put(ID, view); + return view; + } +} \ No newline at end of file