Nested Listview in android ScrollView

Source: Internet
Author: User

Nested Listview in android ScrollView

If you follow the usual settings, the ListView in the ScrollView cannot display all of them, and it cannot slide. The Code searched from stackover flow can be used in the test!

1> set click monitoring events for listView:

 

ListView lv = (ListView) findViewById(R.id.layout_lv);lv.setOnTouchListener(new OnTouchListener() {    // Setting on Touch Listener for handling the touch inside ScrollView    @Override    public boolean onTouch(View v, MotionEvent event) {    // Disallow the touch request for parent scroll on touch of child view    v.getParent().requestDisallowInterceptTouchEvent(true);    return false;    }});
2> set a highly adaptive method for listView:
 
/**** Method for Setting the Height of the ListView dynamically. **** Hack to fix the issue of not showing all the items of the ListView **** when placed inside a ScrollView ****/ public static void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) return; int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED); int totalHeight = 0; View view = null; for (int i = 0; i < listAdapter.getCount(); i++) { view = listAdapter.getView(i, view, listView); if (i == 0) view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT)); view.measure(desiredWidth, MeasureSpec.UNSPECIFIED); totalHeight += view.getMeasuredHeight(); } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); listView.setLayoutParams(params); listView.requestLayout(); }
3> apply the settings to listView:
 
ListView list = (ListView) view.findViewById(R.id.ls); setListViewHeightBasedOnChildren(list);
OK ~~~

 

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.