之前那篇博客的那个点击单个checkbox刷新还是会消失
这次开发中再次用到,优化了下,其实不难就是开发中经常接触这种组件组合
首先主Activity:
package com.listviewtest;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class ListViewTest extends ListActivity {
/** Called when the activity is first created. */
Context context = null;
List<Integer> mList = new ArrayList<Integer>();
CheckBox checkbox;
private RelativeLayout mRelativeLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = getApplicationContext();
setListAdapter(new MyListAdapter());
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
mRelativeLayout = (RelativeLayout) v;
checkbox = (CheckBox) mRelativeLayout.getChildAt(1);
if (mList.contains(new Integer(position))) {
checkbox.setChecked(false);
mList.remove(new Integer(position));
}else {
checkbox.setChecked(true);
mList.add(new Integer(position));
}
}
class MyListAdapter extends BaseAdapter{
String data[] = new String[]{"11111", "22222", "33333", "44444","55555", "66666", "77777", "88888","99999", "10101", "12121", "13131"};
public int getCount() {
return data.length;
}
public Object getItem(int position) {
return data[position];
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (null == convertView){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item, parent, false);
}
TextView text = (TextView)convertView.findViewById(R.id.info);
checkbox = (CheckBox)convertView.findViewById(R.id.checkstatus);
checkbox.setTag(new Integer(position));
text.setText(data[position]);
if (mList != null) {
checkbox.setChecked((mList.contains(new Integer(position))));
} else {
checkbox.setChecked(false);
}
return convertView;
}
}
}
其次是main.xml
<?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"
>
<ListView
android:id ="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:scrollbarStyle="outsideOverlay"
android:cacheColorHint="#00000000"
android:fadingEdge="none"/>
</LinearLayout>
还有list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/info" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:layout_alignParentLeft="true"
android:paddingLeft="5dip"/>
<CheckBox
android:id="@+id/checkstatus" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:focusable="false"
android:clickable="false" android:paddingRight="10dip"
android:layout_alignParentRight="true"/>
</RelativeLayout>