首页 > 代码库 > 【Android】一个activity中垂直排列两个listview

【Android】一个activity中垂直排列两个listview

为了实现这种效果,一开始我想到的办法是ilistVew中加上一个footerview,footerview里面嵌套一个listview,但是实际操作之后发现footerview里的listview只显示一项,这个问题困扰了半天,一直没有找到合适的解决办法,直到昨天晚上偶然看到一篇博文介绍说,scrollview里嵌套listview也出现了同样的问题,解决办法是动态设置listview的高度,于是我用这种方法也试了试,结果还真解决了。


MainActivity.java

package com.example.test2listview;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private ListView listView;
	private ListView headlistView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ListViewAdapter adapter = new ListViewAdapter(getApplicationContext());
		ListViewAdapter2 adapter2 = new ListViewAdapter2(
				getApplicationContext());
		listView = (ListView) findViewById(R.id.listView);
		View v = this.getLayoutInflater().inflate(R.layout.footerview, null);
		headlistView = (ListView) v.findViewById(R.id.head_listView);
		headlistView.setAdapter(adapter2);
		setListViewHeightBasedOnChildren(headlistView);
		listView.addFooterView(v);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Toast.makeText(getApplicationContext(), arg3 + "",
						Toast.LENGTH_SHORT).show();
			}
		});
		headlistView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Toast.makeText(getApplicationContext(), arg3 + "====",
						Toast.LENGTH_SHORT).show();
			}
		});
	}
	
	/**
	 * 动态设置listview的高度
	 * @param listView
	 */
	public void setListViewHeightBasedOnChildren(ListView listView) {
		ListAdapter adapter = listView.getAdapter();
		if(adapter != null) {
			int totalHeight = 0;
			for(int i=0; i<adapter.getCount(); i++) {
				View listItem = adapter.getView(i, null, listView);
				listItem.measure(0, 0);
				totalHeight += listItem.getMeasuredHeight();
			}
			ViewGroup.LayoutParams params = listView.getLayoutParams();
			params.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1));
			((MarginLayoutParams) params).setMargins(0, 0, 0, 0);
			listView.setLayoutParams(params);
			System.out.println(params.height + "===" + adapter.getCount());
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	private class ListViewAdapter extends BaseAdapter {

		private LayoutInflater inflater;

		public ListViewAdapter(Context context) {
			inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public int getCount() {
			return 5;
		}

		@Override
		public Object getItem(int arg0) {
			return arg0;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.list_item, null);
			}
			TextView txt = (TextView) convertView.findViewById(R.id.text);
			txt.setText("第" + getItem(position) + "个item");
			return convertView;
		}

	}

	private class ListViewAdapter2 extends BaseAdapter {

		private LayoutInflater inflater;

		public ListViewAdapter2(Context context) {
			inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public int getCount() {
			return 10;
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.list_item, null);
			}
			TextView txt = (TextView) convertView.findViewById(R.id.text);
			txt.setText("第" + getItem(position) + "个item");
			return convertView;
		}

	}

}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>

</RelativeLayout>

footerview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="分隔标识=================" />

    </LinearLayout>

    <ListView
        android:id="@+id/head_listView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>

</LinearLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
	<TextView 
	    android:id="@+id/text"
	    android:drawableTop="@drawable/ic_launcher"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:gravity="center_horizontal"
	    android:text="test"/>
</LinearLayout>

程序运行效果图:



代码地址: