Instance of a
Reflection + polymorphism: Generate container instantiation and install view subclasses
Simple implementation steps:
Two methods, one providing the full package name. References to classes and resources (id of XML in Android);
Another approach is to use reflection plus polymorphic loading;
private List<View> getViewList(List<View> list){
if(list == null)
list = new ArrayList<View>();
String vName[] = {"com.sansui.Memo.MemoView",
"com.sansui.Calendar.CalendarView","com.sansui.ClassRoom.ClassRoom"};
int vNo[] = {R.layout.memoview, R.layout.calendarview , R.layout.classroomview};
for(int i = 0 ; i < vNo.length ; i++)
list.add(getInstanceView(vName[i],vNo[i]));
return list;
}
Copy the code
The three classes loaded with vNames are classes for custom controls. The topmost parent class is View(similar to J2SE object).
private View getInstanceView(String className,int resouces){
XmlPullParser parser = this.getResources().getXml(resouces);
AttributeSet attributes = Xml.asAttributeSet(parser);
Object view = null;
try {
Class clazz = Class.forName(className);
Constructor con = clazz.getDeclaredConstructor(Context.class,AttributeSet.class);
view = con.newInstance(MainView.this,attributes);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return (View)view;
}
Copy the code
The class is loaded with a simple reflection (that is, the matching and instantiation of the concrete class and XML), and then loaded into the List container
It’s setting the viewPager, that’s what this was written for; Do you find it convenient? If you have multiple views, it’s easy and flexible to load, and this method is quite reusable;
Example 2
Gets the control object by reflection
@ViewInject(R.id.pull_to_refresh_listview)
private PullToRefreshListView pullToRefreshListView;
Copy the code
No more code initializationCopy the code
pullToRefreshListView = (PullToRefreshListView) findViewById(R.id.pullToRefreshListView);
Implementation (there is a lot of code on the web) : one interface and one class
interface
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ViewInject {
int value();
}
Copy the code
class
import android.app.Activity; import java.lang.reflect.Field; @SuppressWarnings("unused") public class ViewInjectClass { public static void autoInjectAllField(Activity activity) { Clazz = activity.getClass(); Field[] fields = clazz.getDeclaredFields(); Field[] fields = clazz.getDeclaredFields(); try { for (Field field : Fields) {/ / whether fields marked InjectView if (field. IsAnnotationPresent (ViewInject. Class)) {/ / if the labeling, Get its ID ViewInject Inject = field.getannotation (viewinject.class); int id = inject.value(); If (id > 0) {// Reflect access to private member, must add this line field.setaccessible (true); Try {// Then copy field.set(activity, activity.findViewById(id)) for this property; } catch (Exception ex) { } } } } } catch (Exception ex) { ex.printStackTrace(); }}}Copy the code
Usage and suggestions: Unified write a BaseActivityCopy the code
import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import com.anykaalibrary.ViewInjectClass; public class BaseActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public void setContentView(int layoutResID) { super.setContentView(layoutResID); ViewInjectClass.autoInjectAllField(this); } } public class MainActivity extends BaseActivity { @ViewInject(R.id.pull_to_refresh_listview) private PullToRefreshListView pullToRefreshListView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }}Copy the code
I don’t actually recommend this, but I would like to see development tools that can use code like this, Automatically change the code to before compilation pullToRefreshListView = (pullToRefreshListView) the findViewById (R.i d.p ullToRefreshListView); In this form.