Android ScrollView Nesting listview

Source: Internet
Author: User

Hi, everyone, after studying scrollview nested scrollview, I suddenly want to study scrollview nested in the ListView.
If you do not know how ScrollView nested ScrollView is implemented, you can refer to http://www.eoeandroid.com/thread-240709-1-1.html
In the last article in fact, I did not how to explain the principle clearly, just on the original code, and in fact ScrollView set ScrollView and ScrollView set the ListView is the same truth.


By common sense, there are two problems with the ScrollView set of ListView:

1. The listview height in the inside cannot be counted, usually only one row of the ListView is displayed
2.listview cannot scroll

In solving the problem, I found a lot of information on the Internet, how to let the ListView display complete, finally I found a post, can solve the problem
http://blog.csdn.net/hitlion2008/article/details/6737459
(At the same time i accidentally searched my "ScrollView nested ScrollView" posts flying ... Here I beg everyone, reprint notes source Ah pro ... )。
Following this post I tried, although the height of the ListView came out, but there are two questions:
A. or only scrollview scrolling is supported, the ListView does not scroll because the ListView has reached its maximum height and it does not need to scroll.
B.listview The advantage is to be able to reuse ListItem, if the ListView to the maximum, equal to no re-use ListItem, which is useless as a ListView, can not save the UI resources, then I might as well direct use linearlayout it

How to support the outside ScrollView and the inside of the ListView can scroll?

Think of a lot of ways, eventually I still put ScrollView set ScrollView the realization of the principle moved to try, the result is successful ....

In fact the implementation of the principle is very simple scrollview there is a method Requestdisallowintercepttouchevent (Boolean);
This method is set whether to surrender the Ontouch permission, if the outer scrollview.requestdisallowintercepttouchevent (FALSE), then the outer ontouch permission will be lost, So the ListView inside will be able to
Get Ontouch permission, the ListView will be able to roll.
The problem is that there is only one permission, and you can scroll to support two view. This is a bit difficult to achieve it.

In fact, this is not difficult, when the finger touch the ListView, let the outside ScrollView surrender authority, when the finger released, the outside of the ScrollView regain access. That's OK.

And look at the code implementation:
Rewrite a Innerlistview
Extends ListView

Innerlistview.java

@Override
Public
Boolean onintercepttouchevent (Motionevent ev) {
Switch
(Ev.getaction ()) {
Case
Motionevent.action_down:

Setparentscrollable (false);//When the finger touches the ListView, let the parent ScrollView surrender Ontouch permission, that is, let the parent ScrollView
Stop can't scroll
LOGMANAGER.D ("Onintercepttouchevent
Down ");
Case Motionevent.action_move:

LOGMANAGER.D ("onintercepttouchevent move");
Break

Case MOTIONEVENT.ACTION_UP:

LOGMANAGER.D ("onintercepttouchevent up");
Case
Motionevent.action_cancel:

LOGMANAGER.D ("onintercepttouchevent Cancel");

Setparentscrollable (TRUE);//When the finger is released, let the parent ScrollView regain Ontouch permission

Break
Default
Break

}
return super.onintercepttouchevent (EV);

}


/**
* Whether to give the rollover event to the parent ScrollView
*
* @param
Flag
*/
private void Setparentscrollable (Boolean flag) {

Parentscrollview.requestdisallowintercepttouchevent (!flag);//The Parentscrollview here is the one outside the ListView ScrollView


}

What I would like to propose here is that the ListView can scroll if the ListView itself is less highly than the child view in the ListView.

If the height of the ListView is wrap_content in XML, there may be problems, such as: the height of the ListView will turn into a child view
The same height.

It is better to have the height of the ListView fixed, usually when we write the ListView layout is also given a fixed value. such as Fill_parent or 100dip

I wrote a parameter called MaxHeight. Sets the maximum height of the listview. is to ensure the height of the listview is fixed.


private int maxheight;
public int getmaxheight () {
Return
MaxHeight;
}
public void setmaxheight (int maxheight) {

This.maxheight = MaxHeight;
}
@Override
protected void
onmeasure (int widthmeasurespec, int heightmeasurespec) {
Todo
auto-generated Method Stub
if (MaxHeight >-1) {

Heightmeasurespec = Measurespec.makemeasurespec (MaxHeight,
Measurespec.at_most);
}
Super.onmeasure (Widthmeasurespec,
HEIGHTMEASURESPEC);
System.out.println (Getchildat (0));
}

Android ScrollView nested listview (GO)

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.