直接贴代码 我不喜欢废话!看看吧!
package com.jclt.activity; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; /** * 测试ScrollView和listview * @author TanRuixiang * @date 2011年8月2日, PM 03:05:08 */ public class ScorllViewAndListViewActivity extends Activity { private ListView listView = null; private ListAdapter listAdapter = null; private List<Map<String,Object>> list = null ; private float density = 0 ; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //绑定ID到控件上面 listView = (ListView)findViewById(R.id.listview); DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); density = metrics.density; initDate(); } /** * 拿到整个数据List的大小</br> * 如果List不等于空 或者 List的大小大于零</br> * 实例化一个自定义的适配器,把自定义的适配器绑定到控件ListView上面</br> * 实例化一个自定义的监听器,把自定义的监听器绑定到控件ListView上面</br> * 判断屏幕的密度 </br> * 如果密度小于1.0 密度等于1.5</br> * 就给ListView手动设置宽度和高度</br> */ private void initDate(){ list = getData(); if(list != null && list.size() > 0){ //SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.list_item, new String[]{"name","age"}, new int[]{R.id.textviewName,R.id.textviewAge}); listAdapter = new ListAdapter(this); listView.setAdapter(listAdapter); listView.setOnItemClickListener(new OnclickListVieListener()); if(density < 1.0 || density == 1.5){ listView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, (int) (list.size() * 48 * density))); } else{ listView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, (int) (list.size() * 47 * density))); } } } /** * 获取到数据(此数据类型是一个List集合,List集合中是一个Map(通过键值对来获取Map中的数据)) * @return list */ /* private List<Map<String, Object>> getData() { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> map = new HashMap<String, Object>(); map.put("name", "yuanshouhui"); map.put("pass", "123456"); list.add(map); map = new HashMap<String, Object>(); map.put("name", "tanglong"); map.put("pass", "123456"); list.add(map); map = new HashMap<String, Object>(); map.put("name", "zhuangsan"); map.put("pass", "123456"); list.add(map); map = new HashMap<String, Object>(); map.put("name", "xiaoyuan"); map.put("pass", "123456"); list.add(map); map = new HashMap<String, Object>(); map.put("name", "xiaoqiang"); map.put("pass", "123456"); list.add(map); map.put("name", "yuanshouhui"); map.put("pass", "123456"); list.add(map); map = new HashMap<String, Object>(); map.put("name", "tanglong"); map.put("pass", "123456"); list.add(map); map = new HashMap<String, Object>(); map.put("name", "zhuangsan"); map.put("pass", "123456"); list.add(map); map = new HashMap<String, Object>(); map.put("name", "xiaoyuan"); map.put("pass", "123456"); list.add(map); map = new HashMap<String, Object>(); map.put("name", "xiaoqiang"); map.put("pass", "123456"); list.add(map); map.put("name", "yuanshouhui"); map.put("pass", "123456"); list.add(map); map = new HashMap<String, Object>(); map.put("name", "tanglong"); map.put("pass", "123456"); list.add(map); return list; }*/ private List<Map<String, Object>> getData(){ List<Map<String, Object>> list = new ArrayList<Map<String,Object>>(); for (int i = 0; i < 30; i++) { Map<String, Object> map = new HashMap<String, Object>(); map.put("name", "name"+i); map.put("age", "age"+i); list.add(map); } return list; } /** * 设置监听器(给ListView每一个Item设置一个监听器) * @author TanRuixiang * @date 2011年8月2日, PM 03:05:08 */ class OnclickListVieListener implements OnItemClickListener{ @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Toast.makeText(getApplicationContext(), "第"+arg2+"个触发监听器", 0).show(); } } /** * 设置一个适配器(把该适配器绑定到ListView上面) * @author TanRuixiang * @date 2011年8月2日, PM 03:05:08 */ class ListAdapter extends BaseAdapter{ private LayoutInflater inflater ; public ListAdapter(Context context){ this.inflater = LayoutInflater.from(context); } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } public final class Holder{ TextView gridName; TextView gridPrice; } @Override public View getView(int position, View convertView, ViewGroup parent) { Holder holder ; if(convertView == null){ holder = new Holder(); convertView = inflater.inflate(R.layout.list_item, null); holder.gridName = (TextView)convertView.findViewById(R.id.textviewName); holder.gridPrice = (TextView)convertView.findViewById(R.id.textviewAge); convertView.setTag(holder); }else{ holder =(Holder)convertView.getTag(); } holder.gridName.setText(("姓名:")+(String)list.get(position).get("name")); System.out.println(holder.gridName.getText()); holder.gridPrice.setText(("年龄:")+(String)list.get(position).get("age")); return convertView; } } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/textviewName" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/textviewAge" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ScrollView android:id="@+id/scorllview" android:layout_width="wrap_content" android:layout_height="wrap_content" > <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" > <ListView android:id="@+id/listview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:dividerHeight="1dip" ></ListView> </LinearLayout> </ScrollView> </LinearLayout>