Solve the Problem of incomplete display of nested Listview in ScrollView and sliding conflict in Listview

Source: Internet
Author: User

Solve the Problem of incomplete display of nested Listview in ScrollView and sliding conflict in Listview

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 context) {super (context);} public ListViewForScrollView (Context context, AttributeSet attrs) {super (Context, attrs );} public ListViewForScrollView (Context 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 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 );}

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 context) {super (context);} public InnerListview (Context context, AttributeSet attrs) {super (context, attrs);} public InnerListview (Context 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!

Related Article

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.