Android ScrollView nested listview conflict

Source: Internet
Author: User

Nesting using Listview,listview in ScrollView only displays a little more than one line. A conflict occurs when both are nested.
Since the ListView itself is inherited from the ScrollView, once nested ScrollView in the ScrollView,
Then there will be problems with the ScrollView height calculation.
And we can't get the results we want.
Let's go to the point below and we'll discuss nested listview cases in ScrollView.
Core solution: Rewrite the Onmesure method of the ListView or GridView. The same applies to the GridView.

    1. protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
    2. int expandspec = Measurespec.makemeasurespec (Integer.max_value >> 2,
    3. Measurespec.at_most);
    4. Super.onmeasure (Widthmeasurespec, Expandspec);
    5. }
Copy Code


Nested ListView in ScrollView:

  1. Package Com.android.xiaomolongstudio.example.scrollviewlistview;
  2. Import java.util.ArrayList;
  3. Import java.util.List;
  4. Import android.app.Activity;
  5. Import Android.os.Bundle;
  6. Import Android.view.Menu;
  7. Import Android.widget.ArrayAdapter;
  8. Import Android.widget.ListView;
  9. /**
  10. *
  11. * @author Little Deanne Dragon
  12. *
  13. */
  14. public class Mainactivity extends Activity {
  15. ListView ListView;
  16. @Override
  17. protected void OnCreate (Bundle savedinstancestate) {
  18. Super.oncreate (savedinstancestate);
  19. Setcontentview (R.layout.activity_main);
  20. ListView = (ListView) Findviewbyid (R.id.listview);
  21. Listview.setadapter (New arrayadapter<string> (This,
  22. Android. R.layout.simple_expandable_list_item_1, GetData ()));
  23. }
  24. Private List<string> GetData () {
  25. list<string> data = new arraylist<string> ();
  26. for (int i = 0; i <; i++) {
  27. Data.add ("Test" + i);
  28. }
  29. return data;
  30. }
  31. @Override
  32. public boolean Oncreateoptionsmenu (Menu menu) {
  33. Inflate the menu; This adds items to the action bar if it is present.
  34. Getmenuinflater (). Inflate (R.menu.activity_main, menu);
  35. return true;
  36. }
  37. }
Copy Code


Custom ListView:

  1. Package Com.android.xiaomolongstudio.example.scrollviewlistview;
  2. Import Android.content.Context;
  3. Import Android.util.AttributeSet;
  4. Import Android.widget.ListView;
  5. public class Mylistview extends ListView {
  6. Public Mylistview (Context context) {
  7. Super (context);
  8. }
  9. Public Mylistview (context context, AttributeSet Attrs) {
  10. Super (context, attrs);
  11. }
  12. Public Mylistview (context context, AttributeSet attrs, int defstyle) {
  13. Super (context, attrs, Defstyle);
  14. }
  15. @Override
  16. protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
  17. int expandspec = Measurespec.makemeasurespec (Integer.max_value >> 2,
  18. Measurespec.at_most);
  19. Super.onmeasure (Widthmeasurespec, Expandspec);
  20. }
  21. }
Copy Code

Without the buttons above, the one in the page directly shows the ListView content, how to show the head in the beginning.
ScrollView has an attribute Mscrollview.scrollto (x, y) to display the position.
But actually did not achieve the effect, checked that Mscrollview.scrollto (x, y) initial initialization of no effect.
Finally I used the Mscrollview.smoothscrollto (0,0);

Reference: http://bbs.itheima.com/thread-128002-1-1.html

Android ScrollView nested listview conflict

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.