Create a linearlayout in the activity, create a scrollhorizon object (scrollhorizon inherited from viewgroup), and call the createlayout function in scrollhorizon to load the myview (inherited from view) object, call the deleteallview function to delete all views.
ScrollhorizonCode:
Public class scrollhorizon extends viewgroup {private context; Public scrollhorizon (context) {super (context); this. context = context; createlayout (); deleteallview () ;}@ overrideprotected void onlayout (Boolean changed, int L, int T, int R, int B) {int childcount = This. getchildcount (); int childleft = 0; int childtop = 0; For (INT I = 0; I <childcount; I ++) {view child = getchildat (I); child. setvisibility (view. visible); child. measure (R-L, B-T); child. layout (childleft, childtop, childleft + child. getmeasuredwidth (), childtop + child. getmeasuredheight (); If (childleft <= 320) {childleft = childleft + child. getmeasuredwidth ();} else {childleft = 0; childtop = childtop + child. getmeasuredheight () ;}} public void createlayout () {resources resource = This. getresources (); packagemanager pmanager = context. getpackagemanager (); Final intent mainintent = new intent (intent. action_main, null); mainintent. addcategory (intent. category_launcher); List <resolveinfo> List = pmanager. queryintentactivities (mainintent, 0); For (INT I = 0; I <list. size (); I ++) {int icon = R. drawable. contacts_button_normal; linearlayout Linear = new linearlayout (context); linear. setlayoutparams (New layoutparams (45, 45); linear. setbackgrounddrawable (this. getresources (). getdrawable (R. drawable. icon); imageview image2 = new imageview (context); image2.setbackgrounddrawable (this. getresources (). getdrawable (R. drawable. icon); drawable = resource. getdrawable (icon); imageview image = new imageview (context); image. setbackgrounddrawable (drawable); linear. addview (image2); linear. addview (image); this. addview (linear) ;}} public void deleteallview () {int size = This. getchildcount (); For (INT I = 0; I <size; I ++) {This. removeviewat (I );}}}
Myview code:
Public class myview extends view {public myview (context) {super (context); setlayoutparams (New layoutparams (40, 40 ));}}
Main activity code:
Public class scrollhorizontest extends activity {public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); linearlayout Linear = new linearlayout (this); scrollhorizon horizon = new scrollhorizon (this); linear. addview (Horizon); this. setcontentview (linear );}}
Display result:
If you do not call the deleteallview functionProgramNo problem. It can be displayed normally, but a null pointer exception occurs when the deleteallview function is called.
cause: After you delete the first view (assuming there are two views in total), there is only one subview in the current viewgroup, and the position of this subview changes to 0, therefore, when removeviewat (1) is called, a null pointer exception occurs. The correct deletion method should be to change removeviewat (I) in the deleteallview function to removeviewat (0);