Android one Activity multiple listview

Source: Internet
Author: User

When we write three listview items directly in the layout file, three scroll bars will appear, and each listview will display only one item, and more will be displayed after modification. How can this problem be solved?


The solution is: Use a ScrollView to include a large layout, and then add the three listview to it.

Create a layout file:


Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">


Android: id = "@ + id/scrollview"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: fadingEdge = "none"
Android: scrollbars = "vertical">
Android: id = "@ + id/linearlayout"
Android: layout_width = "match_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">






A Java file:

Package com. test;
Import java. util. ArrayList;
Import java. util. HashMap;


Import android. app. Activity;
Import android. graphics. Color;
Import android. OS. Bundle;
Import android. view. Gravity;
Import android. view. LayoutInflater;
Import android. view. ViewGroup. LayoutParams;
Import android. view. Window;
Import android. widget. LinearLayout;
Import android. widget. ListView;
Import android. widget. ScrollView;
Import android. widget. TextView;


Import com. listviewtest. R;


/**
* Three Listview share a scroll bar.
* @ Author mark
*
*/
Public class MainActivity extends Activity {
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
RequestWindowFeature (Window. FEATURE_NO_TITLE); // remove the title bar.
SetContentView (R. layout. main );

InitView ();

AddFirstListview ();

AddSecondListview ();

AddThirdListview ();

// ScrollView. addView (all );
}


ScrollView scrollView;
LinearLayout all;
Private void initView (){
LayoutInflater inflater = LayoutInflater. from (MainActivity. this );
// LinearLayout linearLayout = (LinearLayout) inflater. inflate (R. layout. main, null );
// Layout_topic = (LinearLayout) linearLayout. findViewById (R. id. linearlayout );

All = (LinearLayout) findViewById (R. id. linearlayout );
ScrollView = (ScrollView) findViewById (R. id. scrollview );


}

Private void addFirstListview (){
TextView textView = new TextView (MainActivity. this );
TextView. setLayoutParams (new LayoutParams (LayoutParams. FILL_PARENT, LayoutParams. WRAP_CONTENT ));
TextView. setText ("First List ");
TextView. setGravity (Gravity. LEFT );
TextView. setTextSize (18 );
TextView. setTextColor (Color. BLACK );
All. addView (textView );


ArrayList > Arraylist = new ArrayList > (); // Generate a dynamic array
HashMap Map;
For (int I = 1; I <= 10; I ++ ){
Map = new HashMap ();
Map. put ("text", "text" + I );


Arraylist. add (map );
}

ListViewAdapter listViewAdapter = new ListViewAdapter (MainActivity. this, arraylist );
ListView listView = new ListView (MainActivity. this );
Int height = arraylist. size () * (int) getResources (). getDimension (R. dimen. listview_item_height );
ListView. setLayoutParams (new LayoutParams (LayoutParams. FILL_PARENT, height ));
ListView. setDividerHeight (1 );

ListView. setAdapter (listViewAdapter );
All. addView (listView );
}

Private void addSecondListview (){
TextView textView = new TextView (MainActivity. this );
TextView. setLayoutParams (new LayoutParams (LayoutParams. FILL_PARENT, LayoutParams. WRAP_CONTENT ));
TextView. setText ("Second List ");
TextView. setGravity (Gravity. LEFT );
TextView. setTextSize (18 );
TextView. setTextColor (Color. BLACK );
All. addView (textView );


ArrayList > Arraylist = new ArrayList > (); // Generate a dynamic array
HashMap Map;
For (int I = 11; I <= 20; I ++ ){
Map = new HashMap ();
Map. put ("text", "text" + I );


Arraylist. add (map );
}

ListViewAdapter listViewAdapter = new ListViewAdapter (MainActivity. this, arraylist );

ListView listView = new ListView (MainActivity. this );
Int height = arraylist. size () * (int) getResources (). getDimension (R. dimen. listview_item_height );
ListView. setLayoutParams (new LayoutParams (LayoutParams. FILL_PARENT, height ));
ListView. setDividerHeight (1 );

ListView. setAdapter (listViewAdapter );
All. addView (listView );
}

Private void addThirdListview (){
TextView textView = new TextView (MainActivity. this );
TextView. setLayoutParams (new LayoutParams (LayoutParams. FILL_PARENT, LayoutParams. WRAP_CONTENT ));
TextView. setText ("third List ");
TextView. setGravity (Gravity. LEFT );
TextView. setTextSize (18 );
TextView. setTextColor (Color. BLACK );
All. addView (textView );


ArrayList > Arraylist = new ArrayList > (); // Generate a dynamic array
HashMap Map;
For (int I = 21; I <= 30; I ++ ){
Map = new HashMap ();
Map. put ("text", "text" + I );


Arraylist. add (map );
}

ListViewAdapter listViewAdapter = new ListViewAdapter (MainActivity. this, arraylist );
ListView listView = new ListView (MainActivity. this );
Int height = arraylist. size () * (int) getResources (). getDimension (R. dimen. listview_item_height );
ListView. setLayoutParams (new LayoutParams (LayoutParams. FILL_PARENT, height ));
ListView. setDividerHeight (1 );

ListView. setAdapter (listViewAdapter );
All. addView (listView );
}
}


Setting the height of the listview is important.


The project source code is here and can be downloaded.


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.