Coexistence of scrollview viewpager listview

Source: Internet
Author: User

Scenario Description:

Scrollview is the outer sliding control of the entire interface. The layout nested in it is viewpager, and one page in viewpager is the listview control.

Problem description:

The listview in viewpager cannot be displayed after obtaining data, that is, it cannot be expanded, and the outer scrollview cannot slide.

Solution:

1. Expand the listview. (The online saying is that scrollview and listview nesting are not advisable, but the design interface in development is not required or can't be used, so let's talk about it.) to open the listview, you can only rely on setting the height of each item in the adapter, then calculate the total height occupied by all items to get the height of the listview and reset the height of the listview (the tool class is online, and I am talking more or sticking it to the following ).

Package COM. goodwin. finance. util; import android. view. view; import android. view. viewgroup; import android. widget. listadapter; import android. widget. listview;/*** file name: COM. goodwin. finance. util. viewutil * Author: Xiong Jie Wilson * Date: 14-9-30 * Time: * Development Tool: intellij idea 12.1.1 * development language: Java, Android * development framework: * version: v0.1 * <strong> All Layout-related tool classes in the software </strong> * <p> </P> */public class viewutil {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);}/*** get the listview height, and then set the viewpager height * @ Param listview * @ return */public static int setlistviewheightbasedonchildren1 (listview) {// obtain the corresponding listview adapter listadapter = listview. getadapter (); If (listadapter = NULL) {// pre-condition return 0;} int totalheight = 0; For (INT I = 0, Len = listadapter. getcount (); I <Len; I ++) {// listadapter. getcount () returns the number of data items. View listitem = listadapter. getview (I, null, listview); listitem. measure (0, 0); // calculate the width and height of the subitem view totalheight + = listitem. getmeasuredheight (); // calculate the total height of all subitems} viewgroup. layoutparams Params = listview. getlayoutparams (); Params. height = totalheight + (listview. getdividerheight () * (listadapter. getcount ()-1); // listview. getdividerheight () obtains the height occupied by separators between subitems. // Params. the height finally shows the required height for the entire listview. setlayoutparams (Params); Return Params. height ;}}

The usage is to call the tool class method after obtaining the data filling listview adapter.

 

                    infoListAdapter = new TheInfoListAdapter(this);                    infoListAdapter.setInfoList(infoList);                    lvInfoList.setAdapter(infoListAdapter);                    infoListAdapter.notifyDataSetChanged();                    int listViewHeight = ViewUtil.setListViewHeightBasedOnChildren1(lvInfoList);                    ViewGroup.LayoutParams params = vpImgpager.getLayoutParams();                    params.height = listViewHeight;                    vpZifuduo.setLayoutParams(params);


2. Why is 2nd methods of the tool class called? Because after the first method is called, it only affects the height of listview and does not affect the height of its outer viewpager, therefore, I reload this method and return this height. While changing the listview height, I also set the viewpager height dynamically. This solves the problem that the outer scrollview cannot slide.

 

Note the following details:

The listview adapter item uses linearlayout to layout and specify a fixed height for it (this fixed height value is not harmful to compatibility), which facilitates calculation of the listview height.

 

3. Another problem is that the left and right sliding of viewpager conflicts with that of scrollview.

 

The solution is to customize a scrollview and then override an onintercepttouchevent () method.

The following is the scrollview that I have rewritten in this example:

Package COM. goodwin. finance. common. view; import android. content. context; import android. OS. handler; import android. util. attributeset; import android. view. motionevent; import android. widget. scrollview; public class myscrollview extends scrollview {private float xdistance, ydistance, xlast, ylast, we used to save the distance of Y, and then compared */Private int lastscrolly; Public myscrollview (context) {This (context, null);} public myscrollview (context, attributeset attrs) {This (context, attrs, 0);} public myscrollview (context, attributeset attrs, int defstyle) {super (context, attrs, defstyle );} /*** set the rolling interface * @ Param onscrolllistener */Public void setonscrolllistener (onscrolllistener) {This. Onscrolllistener = onscrolllistener;}/*** is used to obtain the y distance of myscrollview scrolling when the user's finger leaves myscrollview, and then calls back to */private handler = new handler () in the onscroll method () {public void handlemessage (Android. OS. message MSG) {int scrolly = myscrollview. this. getscrolly (); // The distance at this time is not the same as the recorded distance. Send a message to handler in 5 milliseconds if (lastscrolly! = Scrolly) {lastscrolly = scrolly; handler. sendmessagedelayed (handler. obtainmessage (), 5);} If (onscrolllistener! = NULL) {onscrolllistener. onscroll (scrolly) ;}};/*** override ontouchevent. When the user's hand is on the myscrollview, * directly call back the Y-direction distance of myscrollview to the onscroll method. When the user raises his hand, * myscrollview may still slide, so when the user raises his hand, we send a message to handler every Five milliseconds, and process * myscrollview sliding distance */@ override public Boolean ontouchevent (motionevent eV) {If (onscrolllistener! = NULL) {onscrolllistener. onscroll (lastscrolly = This. getscrolly ();} switch (ev. getaction () {Case motionevent. action_up: Handler. sendmessagedelayed (handler. obtainmessage (), 5); break;} return Super. ontouchevent (EV);}/*** rolling callback interface **/public interface onscrolllistener {/*** callback method, returns the y distance of myscrollview sliding */Public void onscroll (INT scrolly );} /*** solves the conflict between viewpager and scrollview gestures */@ override public Boolean onintercepttouchevent (motionevent eV) {Switch (ev. getaction () {Case motionevent. action_down: xdistance = ydistance = 0f; xlast = eV. getx (); ylast = eV. gety (); break; Case motionevent. action_move: Final float curx = eV. getx (); Final float Cury = eV. gety (); xdistance + = math. ABS (curx-xlast); ydistance + = math. ABS (Cury-ylast); xlast = curx; ylast = Cury; If (xdistance> ydistance) {return false ;}} return Super. onintercepttouchevent (EV );}}


 

 

 

 

 

 

Coexistence of scrollview viewpager listview

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.