@author:liuxinming
XML属性 | 相关方法 | 说明 |
android:choiceMode | 设置AbsListView的选择行为,支持下列属性值 none:不显示任何选中项 value=0 singleChoice:允许单选 value=1 multipleChoice:允许多选 value=2 multipleChoiceModal:允许多选 value=3 |
|
android:DrawSelectorOnTop | setDrawSelectoronTop(boolean) | true:选中的列项将会显示在上面 |
android:fastScrollEnabled | 设置是否允许快速滚动 true:显示滚动图标,并允许用户拖动该滚动图标进行快速滚动 |
|
android:listSelector | setSelector(int) | 指定被选中的列表项上绘制的Drawable |
android:scrollingCache | true:该组件在滚动时将会使用绘制缓存 | |
android:smoothScrollbar | setSmoothScrollbarEnable(boolean) | false:则不在header View之后绘制分割线 |
android:stackFromBottom | 设置是否从底端开始排列列表项 | |
android:textFilterEnable | 设置是否对列表项进行过滤 注释:当该AbsListView对应的Adapter实现了Filter接口时该属性才会起作用 |
|
android:transcriptMode | 设置该组件的滚动模式。该属性支持如下属性 disabled:关闭滚动。默认值 normal:当该AbsListView受到数据改变通知,且最后一个列表项可见时,该AbsListView将会滚动到底端 alwaysScroll:该AbsListView总会自动滚动到底端 |
android:divider | 设置List列表项的分割线(即可用颜色分割,也可用Drawable分割) |
android:dividerHeight | 分割线的高度 |
android:entries | 指定一个数组资源,Android将根据该数组资源来生成ListView |
android:footerDividersEnabled | false:则不在footer View之前绘制分隔线 |
android:headerDividersEnabled | false:则不在header View之后绘制分隔线 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <!-- 使用数组资源给出列表项 --> <ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:entries="@array/code" android:divider="#f00" android:dividerHeight="2dp" android:headerDividersEnabled="false" /> </LinearLayout>
<?xml version="1.0" encoding="UTF-8"?> <resources> <string-array name="code"> <item>Android</item> <item>PHP</item> <item>Java</item> <item>C</item> <item>C++</item> </string-array> </resources>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <!-- 定义两个ListView --> <ListView android:id="@+id/list01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:divider="#f00" android:dividerHeight="2dp" android:headerDividersEnabled="false" /> <ListView android:id="@+id/list02" android:layout_width="fill_parent" android:layout_height="wrap_content" android:divider="#f00" android:dividerHeight="2dp" android:headerDividersEnabled="false" /> </LinearLayout>Android_ListView/res/layout/array_item.xml
<?xml version="1.0" encoding="UTF-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/TextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="24sp" android:padding="10dp" android:shadowColor="#f0f" android:shadowDx="4" android:shadowDy="4" android:shadowRadius="2" />
<?xml version="1.0" encoding="utf-8"?> <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/TextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="24sp" android:checkMark="@drawable/ok" android:shadowColor="#f0f" android:shadowDx="4" android:shadowDy="4" android:shadowRadius="2"/>
package com.example.android_listview; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.ArrayAdapter; import android.widget.ListView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView list01 = (ListView) findViewById(R.id.list01); ListView list02 = (ListView) findViewById(R.id.list02); //定义数组 String[] data1 = {"屌丝","高富帅","白富美"}; String[] data2 = {"Android","PHP","Java","C","C++"}; //将数组包装为ArrayAdapter ArrayAdapter<String> adapter1 = new ArrayAdapter<String> (this,R.layout.array_item,data1); ArrayAdapter<String> adapter2 = new ArrayAdapter<String> (this,R.layout.checked_item,data2); //为ListView 设置 Adapter list01.setAdapter(adapter1); list02.setAdapter(adapter2); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
package com.example.android_listview; import android.app.ListActivity; import android.os.Bundle; import android.widget.ArrayAdapter; /** * @author LiuxinMing [email protected] */ public class MainActivity extends ListActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 无需使用布局文件 String[] arr = {"屌丝","高富帅","白富美"}; // 创建ArrayAdapter对象 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, arr); // 设置该窗口显示列表 setListAdapter(adapter); } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" > <!-- 定义一个ListView --> <ListView android:id="@+id/list01" 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="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" > <!-- 定义一个ImageView,用于作为列表项的一部分 --> <ImageView android:id="@+id/header" android:layout_width="100dp" android:layout_height="100dp" android:scaleType="centerCrop" android:paddingLeft="10dp" /> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" > <!-- 定义一个TextView,用于作为列表项的一部分 --> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:textColor="#f0f" android:paddingLeft="10dp" /> <!-- 定义一个TextView,用于作为列表项的一部分 --> <TextView android:id="@+id/desc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="14dp" android:paddingLeft="10dp" /> </LinearLayout> </LinearLayout>
package com.example.android_simpleadapter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.os.Bundle; import android.app.Activity; import android.util.Log; import android.view.Menu; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.SimpleAdapter; public class MainActivity extends Activity { private String tag = "SimpleAdapterActivity"; private String[] names = new String[] { "觉悟", "学习", "能力" }; private String[] descs = new String[] { "对工作生活的觉悟", "对新鲜事物的认知能力", "对学习的掌握能力" }; private int[] imageId = new int[] { R.drawable.consciousness, R.drawable.learn, R.drawable.ability }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //创建一个List集合,List集合的元素是Map List<Map<String,Object>> listItems = new ArrayList<Map<String,Object>>(); for (int i=0; i<names.length;i++) { Map<String,Object> listItem = new HashMap<String,Object>(); listItem.put("header", imageId[i]); listItem.put("personName", names[i]); listItem.put("desc", descs[i]); listItems.add(listItem); } //创建一个 SimpleAdapter SimpleAdapter simpleAdapter = new SimpleAdapter(this,listItems, R.layout.simple_item, new String[] {"personName","header","desc"}, new int[] {R.id.name,R.id.header,R.id.desc} ); ListView list = (ListView) findViewById(R.id.list01); //为ListView 设置Adapter list.setAdapter(simpleAdapter); //为ListView的列表项的点击事件绑定事件监听器 list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub Log.d(tag, names[position]+"被单击了"); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }