In our development work, Findviewbyid may be one of the most used functions, but it is particularly annoying that we often need to type-convert the returned view, enter the trouble, the code is ugly, for example, we used to find some child controls in the activity is generally the case:
@Overrideprotected void onCreate(Bundle savedinstancestate) { Super.onCreate(savedinstancestate); Setcontentview(R.Layout.Activity_main); //Find child controls TextView TextView = (TextView)Findviewbyid(R.ID.My_textview); ImageView ImageView = (ImageView)Findviewbyid(R.ID.My_imageview); ListView ListView = (ListView)Findviewbyid(R.ID.My_listview);}
If there are more controls on the page, there will be a lot of type conversions, so is it possible to happily develop the project on Android? The use of viewfinder eliminates the type conversion, andviewfinder is a tool class that is looking for a child control in a layout. The user needs to invoke the Viewfinder.initcontentview function when using to initialize the Contentview, the parameters are context and the layout ID. Then use Viewfinder.findviewbyid to get the desired view, and the returned view is directly the type you receive, without forcing the type conversion.
Examples are as follows:
@Overrideprotected void onCreate(Bundle savedinstancestate) { Super.onCreate(savedinstancestate); Setcontentview(R.Layout.Activity_main); //Initialize viewfinder.Initcontentview( This, R.Layout.Activity_main) ; //Find child controls TextView TextView = viewfinder.Findviewbyid(R.ID.My_textview); ImageView ImageView = viewfinder.Findviewbyid(R.ID.My_imageview); ListView ListView = viewfinder.Findviewbyid(R.ID.My_listview);}
the realization of viewfinder
/** * View finder for easy search of view. The user needs to call Initcontentview when using, * pass in the context and layout ID, then use Findviewbyid to get the desired view *, Findviewbyid a generic method, and the returned view is directly the type you receive. You do not need to force type conversions. For example, we used to find a TextView in the activity generally: * TextView TextView = (TextView) Findviewbyid (viewId); * If there are more controls on the page, there will be a lot of type conversions, while using viewfinder eliminates the type conversion, * Examples are as follows: * TextView TextView = Viewfinder.findviewbyid (viewId); * * @author mrsimple */public final class Viewfinder {/** * layoutinflater * * static Layoutinflater Minfla ter /** * The Sub view Map */private static sparsearray<view> Mviewmap = new sparsearray<view> () for each item of view; /** * Content View */static view Mcontentview; /** * Initializes the viewfinder, actually gets the Contentview to the page. * * @param context * @param layoutid */public static void Initcontentview (context context, int layoutid) {Minflater = Layoutinflater.from (context); Mcontentview = minflater.inflate (LayoutID, NULL, FALSE); if (Minflater = = NULL | |Mcontentview = = null) {throw new RuntimeException ("Viewfinder init failed, minflater = = Nu ll | | Mcontentview = = null. ");}} /** * @return * */public static View Getcontentview () {return mcontentview; }/** * @param viewId * @return * */@SuppressWarnings ("unchecked") public static <t extends View> ; T Findviewbyid (int viewId) {//First look up from view map, if any cache is used directly, otherwise look for view Tagetview = Mviewmap.get from Mcontentview ( VIEWID); if (Tagetview = = null) {Tagetview = Mcontentview.findviewbyid (viewId); Mviewmap.put (ViewId, Tagetview); } return Tagetview = = null? Null: (T) Mcontentview.findviewbyid (viewId); }}
Android solves an auxiliary class that requires a type conversion problem with the return value when using Findviewbyid