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.
- protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
- int expandspec = Measurespec.makemeasurespec (Integer.max_value >> 2,
- Measurespec.at_most);
- Super.onmeasure (Widthmeasurespec, Expandspec);
- }
Copy Code
Nested ListView in ScrollView:
- Package Com.android.xiaomolongstudio.example.scrollviewlistview;
- Import java.util.ArrayList;
- Import java.util.List;
- Import android.app.Activity;
- Import Android.os.Bundle;
- Import Android.view.Menu;
- Import Android.widget.ArrayAdapter;
- Import Android.widget.ListView;
- /**
- *
- * @author Little Deanne Dragon
- *
- */
- public class Mainactivity extends Activity {
- ListView ListView;
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- Super.oncreate (savedinstancestate);
- Setcontentview (R.layout.activity_main);
- ListView = (ListView) Findviewbyid (R.id.listview);
- Listview.setadapter (New arrayadapter<string> (This,
- Android. R.layout.simple_expandable_list_item_1, GetData ()));
- }
- Private List<string> GetData () {
- list<string> data = new arraylist<string> ();
- for (int i = 0; i <; i++) {
- Data.add ("Test" + i);
- }
- return data;
- }
- @Override
- public boolean Oncreateoptionsmenu (Menu menu) {
- Inflate the menu; This adds items to the action bar if it is present.
- Getmenuinflater (). Inflate (R.menu.activity_main, menu);
- return true;
- }
- }
Copy Code
Custom ListView:
- Package Com.android.xiaomolongstudio.example.scrollviewlistview;
- Import Android.content.Context;
- Import Android.util.AttributeSet;
- Import Android.widget.ListView;
- public class Mylistview extends ListView {
- Public Mylistview (Context context) {
- Super (context);
- }
- Public Mylistview (context context, AttributeSet Attrs) {
- Super (context, attrs);
- }
- Public Mylistview (context context, AttributeSet attrs, int defstyle) {
- Super (context, attrs, Defstyle);
- }
- @Override
- protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
- int expandspec = Measurespec.makemeasurespec (Integer.max_value >> 2,
- Measurespec.at_most);
- Super.onmeasure (Widthmeasurespec, Expandspec);
- }
- }
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