The simplest and most efficient way to handle ScrollView nested ListView in Android

Source: Internet
Author: User

The first time to write a blog, the wrong place you can point out;

We all know that in ScrollView nested Listview,listview will be displayed incomplete, unable to calculate the height of the ListView, to resolve the display of the ListView and other layouts in a sliding interface, there are two methods;

The first kind: to let the height of the ListView Show All, then this will be the ListView to encapsulate, reset the height; code is as follows:

public class ListviewforscrollviewExtends ListView {
Public Listviewforscrollview (Context context) {
Super (context);
}

Public Listviewforscrollview (context context, AttributeSet Attrs) {
Super (context, attrs);
}

Public Listviewforscrollview (context context, AttributeSet attrs,
int Defstyle) {
Super (context, attrs, Defstyle);
}

/**
*Rewriteonmeasure(), reaching theListviewAdaptScrollViewThe effect
*/
@Override
protected void Onmeasure (int Widthmeasurespec, int heightmeasurespec) {
int Expandspec = Measurespec.makemeasurespec (Integer.2,
Measurespec.at_most);
super.onmeasure (Widthmeasurespec, Expandspec);
}
}

two of these input parameters:
WidthmeasurespeclevelWidth
HeightmeasurespecVerticalHeight
These two requirements are coded according to the View.measurespec class.
See View.measurespec for a description of this class: This class wraps the layout request passed down from the parent and passes it to the child.
Each of the MEASURESPEC represents a requirement for width or height.
Each of the MEASURESPEC has a size (size) and a pattern (mode).
Measurespec.at_most indicates that a sub-layout can choose a pattern of any size according to its size
Integer.max_value >> 2 represents a value of 2 for the 31-square-1 constant, which represents the maximum value that the int type can represent.

The Second approach: to implement a sliding interface that includes a ListView, can be implemented by adding headerview or Footerview to the ListView, with the following code:
MView = View. Inflate (mcontext, r.layout.  this);  
Mistview = (ListView)Mview.findviewbyid (r.id.  Lv_card); 
headerview = View. inflate (mcontext, r.layout.< Span style= "color: #660e7a; Font-weight:bold; Font-style:italic; " >top, null);      
footerview = View. inflate (mcontext, r.layout.< Span style= "color: #660e7a; Font-weight:bold; Font-style:italic; " >foot, null);
mtextnotexc = (TextView) headerview.findviewbyid (R.id. friendtips = (ImageView) headerview.findviewbyid (R.id.      
allnumber = (TextView) footerview.findviewbyid (R.id. llnumber = (linearlayout) footerview.findviewbyid (R.id.
mlistview.addheaderview ( Headerview, null, false);     < br>  mlistview.addfooterview ( footerview,  null,  false);  

< Span style= "color: #000080;" >< Span style= "color: #660e7a;" > add the top and tail layouts in the ListView like this, and it's better than the first, and it doesn't have to load all the data at once, Can be used almost the same as the usual ListView, support paging loading; < Span style= "color: #660e7a; Font-weight:bold; " > < Span>
< Span style= "color: #808080; Font-style:italic; Font-family: ' The song Body '; > < Span style= "color: #000080; Font-weight:bold; " >                  

The simplest and most efficient way to handle ScrollView nested ListView in Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.