2626import com .noober .background .view .Const ;
2727
2828import java .lang .reflect .Constructor ;
29+ import java .lang .reflect .InvocationTargetException ;
30+ import java .lang .reflect .Method ;
31+ import java .util .HashMap ;
2932import java .util .Map ;
3033
3134public class BackgroundFactory implements LayoutInflater .Factory2 {
@@ -36,6 +39,7 @@ public class BackgroundFactory implements LayoutInflater.Factory2 {
3639 private static final Class <?>[] sConstructorSignature = new Class []{Context .class , AttributeSet .class };
3740 private static final Object [] mConstructorArgs = new Object [2 ];
3841 private static final Map <String , Constructor <? extends View >> sConstructorMap = new ArrayMap <>();
42+ private static final HashMap <String , HashMap <String , Method >> methodMap = new HashMap <>();
3943
4044 @ Override
4145 public View onCreateView (String name , Context context , AttributeSet attrs ) {
@@ -195,6 +199,27 @@ private static View setViewBackground(String name, Context context, AttributeSet
195199 }
196200 }
197201
202+ if (otherTa .hasValue (R .styleable .bl_other_bl_function )) {
203+ String methodName = otherTa .getString (R .styleable .bl_other_bl_function );
204+ if (!TextUtils .isEmpty (methodName )) {
205+ final Context currentContext = view .getContext ();
206+ final Class parentClass = currentContext .getClass ();
207+ final Method method = getMethod (parentClass , methodName );
208+ view .setOnClickListener (new View .OnClickListener () {
209+ @ Override
210+ public void onClick (View view ) {
211+ try {
212+ method .invoke (currentContext );
213+ } catch (IllegalAccessException e ) {
214+ e .printStackTrace ();
215+ } catch (InvocationTargetException e ) {
216+ e .printStackTrace ();
217+ }
218+ }
219+ });
220+ }
221+ }
222+
198223 return view ;
199224 } catch (Exception e ) {
200225 e .printStackTrace ();
@@ -212,6 +237,50 @@ private static View setViewBackground(String name, Context context, AttributeSet
212237 }
213238 }
214239
240+
241+ private static Method getMethod (Class clazz , String methodName ) {
242+ Method method = null ;
243+ HashMap <String , Method > methodHashMap = methodMap .get (clazz .getCanonicalName ());
244+ if (methodHashMap != null ) {
245+ method = methodMap .get (clazz .getCanonicalName ()).get (methodName );
246+ } else {
247+ methodHashMap = new HashMap <>();
248+ methodMap .put (clazz .getCanonicalName (), methodHashMap );
249+ }
250+ if (method == null ) {
251+ method = findMethod (clazz , methodName );
252+ if (method != null ) {
253+ methodHashMap .put (methodName , method );
254+ }
255+ }
256+ return method ;
257+ }
258+
259+
260+ private static Method findMethod (Class clazz , String methodName ) {
261+ Method method ;
262+ try {
263+ method = clazz .getMethod (methodName );
264+ } catch (NoSuchMethodException e ) {
265+ method = findDeclaredMethod (clazz , methodName );
266+ }
267+ return method ;
268+ }
269+
270+ private static Method findDeclaredMethod (Class clazz , String methodName ) {
271+ Method method = null ;
272+ try {
273+ method = clazz .getDeclaredMethod (methodName );
274+ method .setAccessible (true );
275+ } catch (NoSuchMethodException e ) {
276+ if (clazz .getSuperclass () != null ) {
277+ method = findDeclaredMethod (clazz .getSuperclass (), methodName );
278+ }
279+ }
280+ return method ;
281+ }
282+
283+
215284 private static void setDrawable (Drawable drawable , View view , TypedArray otherTa , TypedArray typedArray ){
216285
217286 if (view instanceof TextView ){
0 commit comments