Created
May 9, 2018 00:44
-
-
Save aucd29/0483cbd44d1a605890ead1d71a359e14 to your computer and use it in GitHub Desktop.
view binding 형태 adapter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<Data> list = new ArrayList<>(); | |
HspAdapter<Data> adapter = new HspAdapter<>("your_row"); | |
adapter.setItems(list); | |
adapter.setViewModel(model); | |
binding.recycler.setAdapter(adapter); | |
// 끝 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HspAdapter<ITEM> extends RecyclerView.Adapter<HspViewHolder> { | |
private static final Logger mLog = LoggerFactory.getLogger(HspAdapter.class); | |
private List<ITEM> mItems; | |
private ViewModel mViewModel; | |
private String[] mLayouts; | |
public HspAdapter(@NonNull String layoutId) { | |
mLayouts = new String[] { layoutId }; | |
} | |
public HspAdapter(@NonNull String[] layoutIds) { | |
mLayouts = layoutIds; | |
} | |
@NonNull | |
@Override | |
public HspViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { | |
Context context = parent.getContext(); | |
int layoutId = context.getResources().getIdentifier(mLayouts[viewType], "layout", context.getPackageName()); | |
View view = LayoutInflater.from(context).inflate(layoutId, parent, false); | |
if (mLog.isDebugEnabled()) { | |
mLog.debug(String.format(Locale.getDefault(), "LAYOUT ID : %s (%d)", mLayouts[viewType], layoutId)); | |
} | |
try { | |
ViewDataBinding binding; | |
String bindingClassPath = bindClassName(context, mLayouts[viewType]); | |
if (mLog.isDebugEnabled()) { | |
mLog.debug("BINDING CLASS : " + bindingClassPath); | |
} | |
Class<?> bindingClass = Class.forName(bindingClassPath); | |
Method method = bindingClass.getDeclaredMethod("bind", new Class<?>[]{ View.class }); | |
binding = (ViewDataBinding) method.invoke(null, new Object[] { view }); | |
HspViewHolder vh = new HspViewHolder(view); | |
vh.mBinding = binding; | |
return vh; | |
} catch (Exception e) { | |
e.printStackTrace(); | |
mLog.error("ERROR: " + e.getMessage()); | |
return null; | |
} | |
} | |
@Override | |
public void onBindViewHolder(@NonNull HspViewHolder holder, int position) { | |
if (mViewModel != null) { | |
invokeMethod(holder.mBinding, "setModel", mViewModel.getClass(), mViewModel); | |
} | |
if (mItems != null) { | |
ITEM item = mItems.get(position); | |
invokeMethod(holder.mBinding, "setItem", item.getClass(), item); | |
} | |
holder.mBinding.executePendingBindings(); | |
} | |
@Override | |
public int getItemCount() { | |
if (mItems == null) { | |
return 0; | |
} | |
return mItems.size(); | |
} | |
@Override | |
public int getItemViewType(int position) { | |
if (mItems == null) { | |
return 0; | |
} | |
ITEM item = mItems.get(position); | |
if (item instanceof IHspItem) { | |
return ((IHspItem) item).type(); | |
} | |
return 0; | |
} | |
public void setItems(List<ITEM> items) { | |
mItems = items; | |
} | |
public void setViewModel(ViewModel model) { | |
mViewModel = model; | |
} | |
private static String bindClassName(@NonNull Context context, @NonNull String layoutId) { | |
StringBuilder sb = new StringBuilder(); | |
sb.append(context.getPackageName()); | |
sb.append(".databinding."); | |
sb.append(Character.toUpperCase(layoutId.charAt(0))); | |
for (int i = 1; i < layoutId.length(); ++i) { | |
char c = layoutId.charAt(i); | |
if (c == '_') { | |
c = layoutId.charAt(++i); | |
sb.append(Character.toUpperCase(c)); | |
} else { | |
sb.append(c); | |
} | |
} | |
sb.append("Binding"); | |
return sb.toString(); | |
} | |
private static void invokeMethod(ViewDataBinding binding, String methodName, Class<?> argType, Object argValue) { | |
try { | |
Method method = binding.getClass().getDeclaredMethod(methodName, new Class<?>[] { argType }); | |
method.invoke(binding, new Object[] { argValue }); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
mLog.error("ERROR: " + e.getMessage()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HspViewHolder extends RecyclerView.ViewHolder { | |
public ViewDataBinding mBinding; | |
public HspViewHolder(View itemView) { | |
super(itemView); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment