[英]Android Listview item long click don't respond CheckBox event
This is what I want to achieve function: The Listview item style use custom layout XML file. 这就是我要实现的功能: Listview项样式使用自定义布局XML文件。 When I long press on the listview item, the checkbox is VISIBLE and can respond to a click event.
当我长按列表视图项时,此复选框可见,并且可以响应单击事件。
But when I click the listview or checkbox, it doesn't have any response event.Why? 但是当我单击列表视图或复选框时,它没有任何响应事件。为什么?
This is part of my List Adapter code: 这是我的列表适配器代码的一部分:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view = null;
final ViewHolder holder;
if (null != convertView && convertView instanceof LinearLayout) {
view = convertView;
holder = (ViewHolder) view.getTag();
} else {
view = View.inflate(notifyFragment.getActivity(),R.layout.item_notify, null);
holder = new ViewHolder();
holder.title = (TextView) view.findViewById(R.id.notify_title);
holder.timestamp = (TextView) view.findViewById(R.id.notify_timestamp);
holder.content = (TextView) view.findViewById(R.id.notify_content);
holder.cbox = (CheckBox)view.findViewById(R.id.notify_cbox);
holder.markFlag = (ImageView)view.findViewById(R.id.notify_markflag);
view.setTag(holder);
}
Item_Notify inform_item = notify_list.get(position);
holder.title.setText(inform_item.get_title());
holder.timestamp.setText(inform_item.get_timestamp());
holder.content.setText(inform_item.get_content());
if (isShow) {
holder.cbox.setVisibility(View.VISIBLE);
Boolean flag = notifyFragment.recodeStatu.get(position);
if (flag == null) {
holder.cbox.setChecked(false);
} else {
holder.cbox.setChecked(flag);
}
} else {
holder.cbox.setVisibility(View.GONE);
}
return view;
}
static class ViewHolder {
TextView title;
TextView content;
TextView timestamp;
CheckBox cbox;
ImageView markFlag;
}
This is Click Event : 这是Click事件 :
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
listAdapter.isShow = true;
listAdapter.notifyDataSetChanged();
ll_notify_action.setVisibility(View.VISIBLE);
return true;
}
});
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (listAdapter.isShow) {
CheckBox cb = (CheckBox) view.findViewById(R.id.notify_cbox);
boolean isCheck = !cb.isChecked();
if (isCheck) {
count++;
} else {
count--;
}
btn_del.setText("Delete(" + count + ")");
recodeStatu.put(position, isCheck);
cb.setChecked(isCheck);
} else {
Toast.makeText(getActivity(), "click " + position, Toast.LENGTH_SHORT).show();
}
}
});
If i understood right, i think that you forgot to insert in the parent layout of the checkbox of List Adapter
: 如果我理解正确,我认为您忘记插入“
List Adapter
”复选框的父布局中:
android:descendantFocusability="blocksDescendants">
Something like this (this is a random example so you have to follow your layout, i created this only to make you able to understand where you have to put that information): 像这样的东西(这是一个随机的示例,因此您必须遵循自己的布局,我创建它的目的只是为了使您能够理解将信息放置在何处):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
如果我没记错的话,由于ListViewItem具有一个ClickEventListener,因此任何嵌入式View都不会发生单击之类的事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.