Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 一個Activity多個listview

android 一個Activity多個listview

編輯:關於Android編程

當我們直接在布局文件中寫三個listview的時候,會出現三個滾動條,並且每個listview都只顯示一個item,要改動才顯示更多。怎麼做才好了?

\

辦法是有得:用一個ScrollView包含一個大布局,然後把這三個listview加到裡面去。

新建一個布局文件:<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PD94bWwgdmVyc2lvbj0="1.0" encoding="utf-8"?>
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" >






一個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> arraylist = new ArrayList>(); // 生成動態數組
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("第二個列表");
textView.setGravity(Gravity.LEFT);
textView.setTextSize(18);
textView.setTextColor(Color.BLACK);
all.addView(textView);


ArrayList> arraylist = new ArrayList>(); // 生成動態數組
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("第三個列表");
textView.setGravity(Gravity.LEFT);
textView.setTextSize(18);
textView.setTextColor(Color.BLACK);
all.addView(textView);


ArrayList> arraylist = new ArrayList>(); // 生成動態數組
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);
}
}


其中設置listview的高度是重點。


項目源代碼在這裡,需要可以下載


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved