activity_main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linear_inspect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:scrollbars="vertical" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" >
<LinearLayout
android:id="@+id/linear1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:scrollbars="vertical" >
<ListView
android:id="@+id/id_listview2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
<View
android:layout_width="match_parent"
android:layout_height="1.5px"
android:background="@android:color/white" />
<ListView
android:id="@+id/id_listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
</ScrollView>
</LinearLayout>
===========================================================================
item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linear_inspect"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#ff0077"
android:scrollbars="vertical" >
<TextView
android:id="@+id/id_btn"
android:gravity="center"
android:textColor="@android:color/white"
android:layout_width="match_parent"
android:layout_height="50dp"/>
</LinearLayout>
=================================================================================
package com.example.test;
import java.util.Arrays;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ListView mListView, mListView2;
private MyAdapter mAdapter;
private MyAdapter2 mAdapter2;
private List<String> mDatas = Arrays.asList("Hello", "Java", "Android",
"aa", "bb", "cc", "dd");
private List<String> mDatas2 = Arrays.asList("奋斗", "功夫", "更好", "啊啊", "报表",
"猜猜");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.id_listview);
mAdapter = new MyAdapter(this, mDatas);
mListView.setAdapter(mAdapter);
setListViewHeightBasedOnChildren(mListView);
mListView2 = (ListView) findViewById(R.id.id_listview2);
mAdapter2 = new MyAdapter2(this, mDatas2);
mListView2.setAdapter(mAdapter2);
setListViewHeightBasedOnChildren(mListView2);
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
Toast.makeText(getApplicationContext(),
position + "mListView1", Toast.LENGTH_SHORT).show();
}
});
mListView2.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
Toast.makeText(getApplicationContext(),
position + "mListView2", Toast.LENGTH_SHORT).show();
}
});
}
/***
* @func 动态设置listview的高度,实现一个activity中多个listview时,滑动时,多个listview整体滑动的效果
* @note 两个或多个listview外层必须是ScrollView->LinearLayout-> listview1,listview2
* 否则以下的函数会不起作用
*
* @param listView
*/
public void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
}
===============================================================================
public class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List<String> mDatas;
public MyAdapter(Context context, List<String> datas) {
mInflater = LayoutInflater.from(context);
mDatas = datas;
}
@Override
public int getCount() {
return mDatas.size();
}
@Override
public Object getItem(int position) {
return mDatas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.item, null);
holder.mBtn = (TextView) convertView.findViewById(R.id.id_btn);
holder.mBtn.setText(mDatas.get(position));
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
holder.mBtn.setText(mDatas.get(position));
}
return convertView;
}
private final class ViewHolder {
TextView mBtn;
}
}
======================================================================================
public class MyAdapter2 extends BaseAdapter {
private LayoutInflater mInflater;
private List<String> mDatas;
public MyAdapter2(Context context, List<String> datas) {
mInflater = LayoutInflater.from(context);
mDatas = datas;
}
@Override
public int getCount() {
return mDatas.size();
}
@Override
public Object getItem(int position) {
return mDatas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.item2, null);
holder.mBtn = (TextView) convertView.findViewById(R.id.id_btn2);
holder.mBtn.setText(mDatas.get(position));
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
holder.mBtn.setText(mDatas.get(position));
}
return convertView;
}
private final class ViewHolder {
TextView mBtn;
}
}
源码下载地址http://download.csdn.net/detail/anroidyanyou/9652586