-
-
Save abombss/802475 to your computer and use it in GitHub Desktop.
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
package roboguice.astroboy; | |
import android.content.Context; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.BaseAdapter; | |
import com.google.inject.internal.Nullable; | |
import roboguice.inject.InjectView; | |
import java.lang.reflect.Field; | |
import java.util.ArrayList; | |
/** | |
* @author Ildar Karimov | |
*/ | |
public abstract class RoboBaseAdapter<THolder> extends BaseAdapter { | |
protected ArrayList<ViewMembersInjector> viewsForInjection = new ArrayList<ViewMembersInjector>(); | |
private Context context; | |
private int layoutId; | |
private LayoutInflater inflater; | |
private void init() { | |
inflater = LayoutInflater.from(context); | |
prepareFields(holderType); | |
} | |
public RoboBaseAdapter(Context context, int layoutId, Class<? extends Object> holderType) { | |
this.context = context; | |
this.layoutId = layoutId; | |
init(holderType); | |
} | |
protected abstract THolder createNewViewHolder(int position, View convertView, ViewGroup parent); | |
protected abstract void bindView(int position, View convertView, ViewGroup parent, THolder holder); | |
protected View createNewView(int position, View convertView, ViewGroup parent) { | |
return inflater.inflate(layoutId, parent, false); | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
THolder holder; | |
if (convertView == null) { | |
convertView = createNewView(position, convertView, parent); | |
holder = createNewViewHolder(position, convertView, parent); | |
convertView.setTag(holder); | |
injectViews(convertView, holder); | |
} else { | |
holder = convertView.getTag(); | |
} | |
bindView(position, convertView, parent, holder); | |
return convertView; | |
} | |
public void injectViews(View view) { | |
for (ViewMembersInjector viewMembersInjector : viewsForInjection) { | |
viewMembersInjector.reallyInjectMembers(this, view); | |
} | |
} | |
private void prepareFields(Class clazz) { | |
for (Field field : clazz.getDeclaredFields()) { | |
if (field.isAnnotationPresent(InjectView.class)) { | |
field.setAccessible(true); | |
viewsForInjection.add(new ViewMembersInjector(field, field.getAnnotation(InjectView.class))); | |
} | |
} | |
} | |
class ViewMembersInjector { | |
protected Field field; | |
protected InjectView annotation; | |
public ViewMembersInjector(Field field, InjectView annotation) { | |
this.field = field; | |
this.annotation = annotation; | |
} | |
public void reallyInjectMembers(Object holder, View view) { | |
Object value = null; | |
try { | |
value = view.findViewById(annotation.value()); | |
if (value == null && field.getAnnotation(Nullable.class) == null) { | |
throw new NullPointerException(String.format("Can't inject null value into %s.%s when field is not @Nullable", field.getDeclaringClass(), field | |
.getName())); | |
} | |
field.set(holder, value); | |
} catch (IllegalAccessException e) { | |
throw new RuntimeException(e); | |
} catch (IllegalArgumentException f) { | |
throw new IllegalArgumentException(String.format("Can't assign %s value %s to %s field %s", value != null ? value.getClass() : "(null)", value, | |
field.getType(), field.getName())); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment