Android One activity multiple listview

Source: Internet
Author: User

When we write three ListView directly in the layout file, three scroll bars appear, and each ListView displays only one item, and more is displayed to change. What's the best way to do that?


There are some ways to do this: include a large layout with a scrollview, and add the three ListView to the inside.

Create a new layout file:

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:orientation= "Vertical" >


<scrollview
Android:id= "@+id/scrollview"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:fadingedge= "None"
android:scrollbars= "Vertical" >
<linearlayout
Android:id= "@+id/linearlayout"
Android:layout_width= "Match_parent"
android:layout_height= "Fill_parent"
android:orientation= "Vertical" >
</LinearLayout>
</ScrollView>


</LinearLayout>


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 ();
Textview.settextcolor (color.black);
All.addview (textView);


arraylist
hashmap<string, object> map;
for (int i = 1; I <=; i + +) {
map = new hashmap<string, object> ();
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 ();
Textview.settextcolor (color.black);
All.addview (textView);


arraylist
hashmap<string, object> map;
for (int i = one; I <=; i + +) {
map = new hashmap<string, object> ();
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 ();
Textview.settextcolor (color.black);
All.addview (textView);


arraylist
hashmap<string, object> map;
for (int i = +; I <=; i + +) {
map = new hashmap<string, object> ();
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);
}
}


The height where the ListView is set is the focus.


Project source code here, need to be able to download


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.