Adding another slide control in a sliding control or layout usually causes some inexplicable problems. Today, we will mainly introduce the problem of abnormal display of nested listview in scrollview layout and sliding conflict between nested listview in listview.
1. Solution to abnormal display of nested listview in scrollview Layout
Currently, there are several solutions to this problem. Here we will only introduce two of them, which are relatively simple and easy to use.
(1) customize a listview and inherit from the listview. The Code is as follows:
Public class listviewforscrollview extends listview {public listviewforscrollview (context) {super (context);} public listviewforscrollview (context, attributeset attrs) {super (context, attrs );} public listviewforscrollview (context, attributeset attrs, int defstyle) {super (context, attrs, defstyle );} /*** you only need to override this method. */@ overrideprotected void onmeasure (INT widthmeasurespec, int heightmeasurespec) {int expandspec = measurespec. makemeasurespec (integer. max_value> 2, measurespec. at_most); super. onmeasure (widthmeasurespec, expandspec );}}
In use, use this control instead of the listview control.
(2) recalculate the height of the listview
Add a sliding control to a sliding layout. The height of the sliding control cannot be calculated, so only one item can be displayed. To solve this problem, we can recalculate the height of the listview, call the following static method.
/*** Solve the problem of incomplete display of scrollview nested listview ** @ Param listview */public static void setlistviewheightbasedonchildren (listview) {listadapter = listview. getadapter (); If (listadapter = NULL) {return;} int totalheight = 0; For (INT I = 0; I <listadapter. getcount (); I ++) {view listitem = listadapter. getview (I, null, listview); listitem. measure (0, 0); totalheight + = listitem. getmeasuredheight ();} viewgroup. layoutparams Params = listview. getlayoutparams (); Params. height = totalheight + (listview. getdividerheight () * (listadapter. getcount ()-1); listview. setlayoutparams (Params );}
2. Sliding conflicts of nested listview in listview
Previously, this requirement was introduced in the project, that is, to add a small listview to the footer of A listview. However, due to the touch conflict problem, the small listview cannot slide, to solve this problem, we can customize a control to inherit from listview, and then rewrite the onintercepttouchevent method to distribute touch events.
Onintercepttouchevent () is a method of viewgroup. It is used to intercept related events before the system triggers ontouchevent () to the viewgroup and its childview. In this method, the child control or parent control obtains the touch processing permission for distribution to complete their respective slide tasks.
As follows:
The Code is as follows:
/***** @ Classname: COM. example. listdemo. innerlistview * @ Description: listview * @ author zhaokaiqiang * @ date 2:43:34 **/public class innerlistview extends listview {public innerlistview (context) {super (context);} public innerlistview (context, attributeset attrs) {super (context, attrs);} public innerlistview (context, attributeset attrs, int defstyle ) {Super (context, attrs, defstyle) ;}@ overridepublic Boolean onintercepttouchevent (motionevent eV) {Switch (ev. getaction () {// when a finger touches listview, let the parent control hand over the ontouch permission and cannot scroll case motionevent. action_down: setparentscrollable (false); Case motionevent. action_move: break; Case motionevent. action_up: Case motionevent. action_cancel: // when the finger is released, let the parent control re-obtain the ontouch permission setparentscrollable (true); break;} return Super. onintercepttouch Event (EV);} // sets whether the parent control can obtain the touch processing permission private void setparentscrollable (Boolean flag) {getparent (). requestdisallowintercepttouchevent (! Flag );}}
In this way, the listview outside and the small listview inside can both achieve their own sliding.
This article is from http://blog.csdn.net/zhaokaiqiang1992!