本文涉及两个问题:
- ListView中checkbox错位问题
- oncreateContextMenuListener失效问题
activity_listview_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/textView"
android:textSize="25sp"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<CheckBox
android:focusable="false"
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="end" />
</LinearLayout>
MainActivity.java:
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class MainActivity extends ListActivity {
private static List<A> strs = new ArrayList<A>();
private static final int ITEM1 = Menu.FIRST;
private static final int ITEM2 = Menu.FIRST + 1;
private MyAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
A a;
for (int i = 0; i < 50; i++) {
a = new A("item" + i, A.TYPE_NOCHECKED);
strs.add(a);
}
mAdapter = new MyAdapter(this);
setListAdapter(mAdapter);
registerForContextMenu(getListView());
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("选择操作");
menu.add(0, ITEM1, 0, "删除");
menu.add(0, ITEM2, 0, "编辑");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
final int id = (int) info.id;
if (-1 == id) {
super.onContextItemSelected(item);
}
switch (item.getItemId()) {
case ITEM1:
strs.remove(strs.get(id));
mAdapter.notifyDataSetChanged();
break;
default:
break;
}
return true;
}
public static class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public MyAdapter(Context context) {
// Cache the LayoutInflate to avoid asking for a new one each time.
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return strs.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
final int index = position;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.activity_listview_item, null);
holder = new ViewHolder();
holder.textView = (TextView) convertView.findViewById(R.id.textView);
holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
strs.get(index).type = A.TYPE_CHECKED;
} else {
strs.get(index).type = A.TYPE_NOCHECKED;
}
}
});
// Bind the data efficiently with the holder.
holder.textView.setText(strs.get(position).name);
if (strs.get(position).type == A.TYPE_CHECKED) {
holder.checkBox.setChecked(true);
} else {
holder.checkBox.setChecked(false);
}
return convertView;
}
}
static class ViewHolder {
TextView textView;
CheckBox checkBox;
}
class A {
public static final int TYPE_CHECKED = 1;
public static final int TYPE_NOCHECKED = 0;
String name;
int type;
public A(String name, int type) {
this.name = name;
this.type = type;
}
}
}
总结:
1.用ViewHolder缓存,防止数据乱序和图片闪烁
解决办法:由于是因为index错误造成的,那么只要保证index值与当前positon保持一至即可,只要把添加监听器的方法加到初始化view中checkBox状态的代码之前即可.这样即始由于初始化造成调用了onCheckedChange()方法,也因为其中index值是最新的,而依然不会错乱.
2.如果ListView中的单个Item的view中存在checkbox,button等view,会导致ListView.setOnItemClickListener无效,事件会被子View捕获到,ListView无法捕获处理该事件.
解决方法:
在checkbox、button对应的view处加android:focusable="false"
android:clickable="false" android:focusableInTouchMode="false"
其中focusable是关键
从OnClickListener调用getSelectedItemPosition(),Click 和selection 是不相关的,Selection是通过D-pad or trackball 来操作的,Click通常是点击操作的。
arg2参数才是点击事件位置的参数
第二种方法 就是在自己的getView中
convertView = mInflater . inflate ( R . layout . list_item_text , null );
convertView . setClickable ( true );
convertView . setOnClickListener ( clickListener );
public OnClickListener myClickListener = new OnClickListener () {
public void onClick ( View v ) {
//code to be written to handle the click event
}
};