您的位置:首页 > 移动开发 > Android开发

关于Android的ListView个人小结

2014-11-13 10:36 330 查看
首先在主activity_main.xml布局文件中加入ListView控件:
<ListView
android:id="@+id/lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
></ListView>
因为应用开发中的需要,往往ListView中的item需要在用一个activity_item.xml布局文件来规定item布局:

<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="55dip"
android:orientation="horizontal"
android:layout_marginTop="20dip"
>
<TextView
android:id="@+id/fileitem"
android:layout_width="267dip"
android:layout_height="40dip"
android:textSize="10pt"
android:gravity="center_vertical"
android:layout_marginLeft="10dip"
/>
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
android:layout_toRightOf="@id/fileitem"
android:layout_alignParentTop="true"
android:layout_marginRight="5dip"

/>
</RelativeLayout >
特别注意当item里面有一些控件带有触点聚焦检测时要把这些属性全部改成false(如上图下划线部分属性)。
下面开始代码开始来干活了:

ListView lv = (ListView)
this.findViewById(R.id.lv);
将布局文件中的控件与代码中的变量绑定在一起。

list = new ArrayList<HashMap<String, Object>>();
此时list里面是空白没有装值

adapter = new MyAdapter(this,list, R.layout.activity_item,
new String[] {
"item_tv", "item_cb" }, newint[] {
R.id.fileitem, R.id.checkbox });
适配器对象创建。

lv.setAdapter(adapter);
设置适配器(adapter)
lv.setEnabled(true);
ListView控件有效
lv.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View view,
int position,
long arg3) {
// adapter.getView(position, view, arg0);
ViewHolder holder = (ViewHolder) view.getTag();
holder.cb.toggle();// 在每次获取点击的item时改变checkbox的状态
MyAdapter.isSelected.put(position, holder.cb.isChecked());// 同时修改map的值保存状态
}
});
监听事件是否发生并做相应的操作。
下面开始关于适配器的构建操作:

public class MyAdapterextends BaseAdapter
{

public static HashMap<Integer, Boolean>isSelected;
private Context context = null;
private LayoutInflater inflater = null;
private List<HashMap<String, Object>> list = null;
private String keyString[] = null;
private String itemString = null; // 记录每个item中textview的值
private int idValue[] = null;// id值

public MyAdapter(Context context, List<HashMap<String, Object>> list,
int resource, String[] from,int[] to) {
this.context = context;
this.list = list;
keyString =
new String[from.length];
idValue =new
int[to.length];
System.arraycopy(from, 0,
keyString, 0, from.length);
System.arraycopy(to, 0,
idValue, 0, to.length);
inflater = LayoutInflater.from(context);
init();
}

public void init() {
isSelected =
new HashMap<Integer, Boolean>();
for (int i = 0; i <list.size(); i++) {
isSelected.put(i,false);
}
}

@Override
public View getView(int position, View view, ViewGroup arg2) {
ViewHolder holder = null;
if (holder ==
null) {
holder = new ViewHolder();
if (view ==null) {
view =
inflater.inflate(R.layout.activity_item,
null);
}
holder.tv = (TextView) view.findViewById(R.id.fileitem);
holder.cb = (CheckBox) view.findViewById(R.id.checkbox);
view.setTag(holder);
} else
{
holder = (ViewHolder) view.getTag();
}
HashMap<String, Object> map =
list.get(position);
if (map !=
null) {
itemString = (String) map.get(keyString[0]);
holder.tv.setText(itemString);
}
holder.cb.setChecked(isSelected.get(position));
return view;
}
public class ViewHolder {
public TextViewtv =
null;
public CheckBoxcb =
null;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: