我自定义了一个ListView布局,想在里面添加一个CheckBox按钮,我遇到了下面的第二个问题,虽然有解决方法但不知道具体怎么做,最好能给个完整的例子,谢谢大家了!
1、ListView item中加入checkbox后onListItemClick 事件无法触发。
原因:checkbox的优先级高于ListItem于是屏蔽了ListItem的单击事件。
解决方案:设置checkbox的android:focusable="false"
2、选择其中的checkbox,当滚动ListView的时候,会出现一些Checkbox选择错位的现象,
原因:为记住Checkbox的选择状态
解决方案:当选择Checkbox的时候,记下其状态,然后在getView方法中进行设置
我也写了一个,写得不好,请见谅。
[code="java"]
package com.billy.demo;
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.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class TestListViewAndCheckbox extends ListActivity {
/** Called when the activity is first created. */
Context context = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = getApplicationContext();
setListAdapter(new MyListAdapter());
}
class MyListAdapter extends BaseAdapter{
String data[] = new String[]{"apple", "pear", "banana", "orange","apple", "pear", "banana", "orange","apple", "pear", "banana", "orange"};
List<Integer> checkPosition = new ArrayList<Integer>(data.length);
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return data[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
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);
final CheckBox checkbox = (CheckBox)convertView.findViewById(R.id.checkstatus);
checkbox.setTag(new Integer(position));
text.setText(data[position]);
if (checkPosition != null){
checkbox.setChecked((checkPosition.contains(new Integer(position)) ? true : false));
}else{
checkbox.setChecked(false);
}
checkbox
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked){
if (!checkPosition.contains(checkbox.getTag())){
checkPosition.add((Integer)checkbox.getTag());
}
}else{
if (checkPosition.contains(checkbox.getTag())){
checkPosition.remove(checkbox.getTag());
}
}
}
});
return convertView;
}
}
}
[/code]
xml
[code="xml"]
<?xml version="1.0" encoding="utf-8"?>
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
android:id ="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:scrollbarStyle="outsideOverlay"
/>
[/code]
[code="xml"]
<?xml version="1.0" encoding="utf-8"?>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
android:id="@+id/checkstatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
/>
[/code]
只是一个demo,自己写的时候还要多注意。 list文字部分可点击
今天太晚了,明天我找个例子给你吧,希望能给你帮助!
我也是像你这样解决的。如果你的内容是一个文字,和一个CheckBox的话,你可以用系统的View, CheckedTextView希望可以满足你。