android 一个Activity多个listview

当我们直接在布局文件中写三个listview的时候,会出现三个滚动条,并且每个listview都只显示一个item,要改动才显示更多。怎么做才好了?


办法是有得:用一个ScrollView包含一个大布局,然后把这三个listview加到里面去。

新建一个布局文件:

<?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>


一个Java文件:

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;


/**
 * 三个Listview共享一个滚动条
 * @author mark
 *
 */
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
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("第一个列表");
textView.setGravity(Gravity.LEFT);
textView.setTextSize(18);
textView.setTextColor(Color.BLACK);
all.addView(textView);


ArrayList<HashMap<String, Object>> arraylist = new ArrayList<HashMap<String, Object>>(); // 生成动态数组
HashMap<String, Object> map;
for (int i = 1; i <= 10; 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("第二个列表");
textView.setGravity(Gravity.LEFT);
textView.setTextSize(18);
textView.setTextColor(Color.BLACK);
all.addView(textView);


ArrayList<HashMap<String, Object>> arraylist = new ArrayList<HashMap<String, Object>>(); // 生成动态数组
HashMap<String, Object> map;
for (int i = 11; i <= 20; 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("第三个列表");
textView.setGravity(Gravity.LEFT);
textView.setTextSize(18);
textView.setTextColor(Color.BLACK);
all.addView(textView);


ArrayList<HashMap<String, Object>> arraylist = new ArrayList<HashMap<String, Object>>(); // 生成动态数组
HashMap<String, Object> map;
for (int i = 21; i <= 30; 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);
}
}


其中设置listview的高度是重点。


项目源代码在这里,需要可以下载


android 一个Activity多个listview,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。