Discussion: How to nest listview_android in ScrollView

Source: Internet
Author: User
First, analysis
1, the simplest layout: only one listview
If the entire page has only one ListView, the ListView itself has a scrolling effect, so when the loaded data exceeds the scope of the page display, you can view all the item by sliding up and down.
Therefore, you do not need to add ScrollView in this case.
2, other layout A+listview
In this case, if layout A is defined in front of the ListView, the ListView display is incomplete when the ratio of layout A is large, or when more data is ListView loaded. Also, because the ListView itself can scroll, you can still scroll up and down to see all of the ListView's item.
As shown in the figure:
3, other layout B+listview
In this case, assuming that layout B is defined behind the ListView, there are two scenarios:
(1), ListView Loading data is not much, you can fully display ListView each item, then if there is enough space left behind, layout B can be normal display;
(2), ListView load of data added, then will lead to the space for layout B is insufficient, or not at all, Layout B will show incomplete or completely do not display.
However, the content of ListView itself can be viewed by sliding.
In the third case, if you don't reconsider the layout, you'll need to add scrollview to view the remaining page content.
Ii. the conflict between ScrollView and ListView
1. Assign a height to ListView
For example, set the android:layout_height= "240dip", then can be resolved, may affect the beauty.
As shown in the figure:
2. Add a scrollview outside

In this case, there is a problem. As shown in the figure:
3, to ScrollView set properties: Android:fillviewport= "true"
The test found that if the ListView load of data, it can be solved, but when the ListView load more data, still can not show the full, and this time ListView itself can not scroll.

Three, there are two ways to solve the problem
1, in the calculation of ListView total height and set
ListView ListView = (ListView) Findviewbyid (ID);
Youradapter adapter = new Myadapter ("Initialize your adapter");
Listview.setadapter (adapter);
Setlistviewheightbasedonchildren (ListView);(call a custom method after Setadapter)
Copy Code code as follows:

/**
* @param ListView
*/
private void Setlistviewheightbasedonchildren (ListView ListView) {

ListAdapter 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);
}

Use this method to note that each item of the child ListView must be linearlayout and cannot be otherwise, because other layout (such as relativelayout) do not rewrite onmeasure (), so the Onmeasure () Throws an exception when.
2, Custom ListView, Overload Onmeasure () method, set all display
Copy Code code as follows:

Package com.meiya.ui;

Import Android.widget.ListView;

/**
*
* @Description: Simple implementation of embedded ListView in ScrollView
*
* @File: Scrollviewwithlistview.java
*
* @Paceage Com.meiya.ui
*
*
* @Date 03:02:38
*
* @Version
*/
public class Scrollviewwithlistview extends ListView {

Public Scrollviewwithlistview (Android.content.Context context,
Android.util.AttributeSet attrs) {
Super (context, attrs);
}

/**
* Integer.max_value >> 2, if not set, the system default setting is to display two
*/
public void onmeasure (int widthmeasurespec, int heightmeasurespec) {
int expandspec = Measurespec.makemeasurespec (Integer.max_value >> 2,
Measurespec.at_most);
Super.onmeasure (Widthmeasurespec, Expandspec);

}

}

The above can solve ScrollView embedded listview, but there is a problem is the first time to enter the interface dynamically loaded ListView items after the page will jump to the ListView first child, this very egg pain,
Helpless and do not know how to solve, first use
Copy Code code as follows:

Scrollview.post (New Runnable () {
Let ScrollView jump to the top and must be placed in the runnable () method
@Override
public void Run () {
Scrollview.scrollto (0, 0);
}
});

This method over, want to have to know friends also give a point solution
3. Use ScrollView +linearlayout to add a list using the AddView () method.

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.