首页 > 移动平台 > 详细

android 一个Activity多个listview

时间:2014-05-06 23:43:06      阅读:657      评论:0      收藏:0      [点我收藏+]

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

bubuko.com,布布扣

办法是有得:用一个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,布布扣,bubuko.com

android 一个Activity多个listview

原文:http://blog.csdn.net/lengguoxing/article/details/25144517

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!