首页 > 其他 > 详细

解决ScrollView嵌套ListView单行显示实例

时间:2014-04-08 21:38:55      阅读:551      评论:0      收藏:0      [点我收藏+]
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <ListView 
            android:id="@+id/listview"
            android:layout_width="match_parent"
            android:divider="@null"
            android:layout_height="wrap_content"></ListView>
</LinearLayout>

</ScrollView>


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

</LinearLayout>


package com.example.srollviewtest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	ListView listView;
	TextView txt;

	String[] name = { "小燕子", "小王子", "诗情画意", "买红薯的小叔" };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mian);
		listView = (ListView) findViewById(R.id.listview);
		txt = (TextView) findViewById(R.id.txt);
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < name.length; i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("name", name[i]);
			list.add(map);
		}
		SimpleAdapter simpleAdapter = new SimpleAdapter(this, list,
				R.layout.showname, new String[] { "name" },
				new int[] { R.id.txt });
		listView.setAdapter(simpleAdapter);
		setListViewHeightBasedOnChildren(listView);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int item,
					long id) {
				if (view instanceof LinearLayout) {
					TextView textView = (TextView) ((LinearLayout) view)
							.findViewById(R.id.txt);
					Toast.makeText(MainActivity.this,
							textView.getText().toString(), Toast.LENGTH_SHORT)
							.show();
				}

			}
		});
	}

	/**
	 * 解决ScrollView嵌套ListView显示单行信息的关键代码
	 * 
	 * @param listView
	 *            ListView对象
	 */
	private 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);
	}

}


解决ScrollView嵌套ListView单行显示实例,布布扣,bubuko.com

解决ScrollView嵌套ListView单行显示实例

原文:http://blog.csdn.net/x605940745/article/details/23188469

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