在Android系统中,ListView的用法稍微复杂一点,配置Adpater就有几种方法,如ArrayAdapter,SimpleAdapter等。有很多都是用ListActivity ,然后用Adapter来显示布局。本例中没有用到ListActivity 。
ListView是在android中是一个经常用到的控件,ListView里面的每个子项Item可以使用一个字符串,也可以是一个组合控件,下面是本例将要实现的功能列表:
1. 准备ListView要显示的数据,使用一维或多维 动态数组 保存数据(此处为模拟数据,实际开发中会解析从后台返回的json数据,具体见我另外一篇博文);
2. 构建 适配器,适配器就是Item数组,动态数组有多少元素就生成多少个Item;
3. 把 适配器 添加到ListView,并显示出来;
4. 在ListView的Item添加CheckBox并可以执行单击事件,设置单击事件,对应的Item显示对应Item的数据,通过getChildAt(0)找控件;
5. 实现长按后弹出menu对话框效果;
效果截图:
UI代码如下:
mylist.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ListView android:id="@+id/ListView01" android:layout_width="wrap_content" android:layout_height="wrap_content" > </ListView> </LinearLayout>
设置ListView的 Item样式 xml文件如下:
mylistitem.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/RelativeLayout01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingBottom="4dip" android:paddingLeft="12dip" android:paddingRight="12dip" > <CheckBox android:id="@+id/CheckBox01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:focusable="false" android:paddingTop="12dip" > </CheckBox> <TextView android:id="@+id/topTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/toptextview_text" android:textSize="20sp" > </TextView> <TextView android:id="@+id/bottomTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/topTextView" android:text="@string/bottomtextview_text" > </TextView> </RelativeLayout>
到这一步,顺便解答上面第4个问题:如何设置在 ListView中调协CheckBox之后还可以执行单击或长按事件,在Android中 CheckBox的点击事件优先级比ListView高,当页面一加载时,会默认聚焦在CheckBox上,为了防止这个问题出来,我们要在Android的控件上的属性加上Bool值:
android:focusable=”false”
最后就是Java源代码,之后我再一一解答上面的所有问题:
java代码:
package com.example.mylistview01; import java.util.ArrayList; import java.util.HashMap; import android.app.Activity; import android.os.Bundle; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.MenuItem; import android.view.View; import android.view.View.OnCreateContextMenuListener; import android.view.Window; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.SimpleAdapter; import android.widget.TextView; import android.widget.Toast; /** * @ClassName: MainActivity * @Description: android实现listview * @author xzy * @date 2015-1-29 上午9:57:57 * */ public class MainActivity extends Activity { private ListView myListView; @SuppressWarnings({ "unchecked", "rawtypes" }) @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //设置无标题 requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.mylist); myListView = (ListView) findViewById(R.id.ListView01); ArrayList listitem = new ArrayList(); for (int i = 0; i < 10; i++) { HashMap map = new HashMap(); map.put("ItemTitle", "文章" + i); map.put("ItemText", "Android 小項目" + i); listitem.add(map); } SimpleAdapter adapter = new SimpleAdapter(this, //上下文 listitem, //数据 R.layout.mylistitem, //listview中item的布局xml new String[]{"ItemTitle","ItemText"},//map中的key new int[]{R.id.topTextView, R.id.bottomTextView});//要绑定的控件 myListView.setAdapter(adapter); myListView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) { RelativeLayout lr = (RelativeLayout) arg1; TextView mText = (TextView) lr.getChildAt(1); Toast.makeText(MainActivity.this, "你點擊了第" + arg2 + "項的" + mText.getText().toString(), Toast.LENGTH_SHORT).show(); } }); myListView .setOnCreateContextMenuListener(new OnCreateContextMenuListener() { @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { ListView lr = (ListView) v; RelativeLayout myte = (RelativeLayout) lr.getChildAt(0); TextView dd = (TextView) myte.getChildAt(1); menu.setHeaderIcon(R.drawable.ic_launcher); menu.setHeaderTitle(dd.getText().toString()); menu.add(1, 0, 0, "高亮"); menu.add(0, 1, 0, "置頂"); } }); } @Override public boolean onContextItemSelected(MenuItem item) { // TODO Auto-generated method stub String Temp = ""; switch (item.getItemId()) { case 0: Temp = "高亮"; break; case 1: Temp = "置頂"; break; default: break; } Toast.makeText(this, Temp + "處理", 1000).show(); return super.onContextItemSelected(item); } }