gpt4 book ai didi

Android Listview item长按不响应CheckBox事件

转载 作者:行者123 更新时间:2023-11-29 20:52:09 27 4
gpt4 key购买 nike

这就是我要实现的功能:Listview 项目样式使用自定义布局 XML 文件。当我长按 ListView 项目时,复选框是可见的并且可以响应点击事件。 enter image description here

但是当我点击 ListView 或复选框时,它没有任何响应事件。为什么?

这是我的列表适配器代码的一部分:

@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;
}

这是点击事件:

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();
}

}
});

最佳答案

如果我没理解错的话,我认为你忘记在List Adapter复选框的父布局中插入:

android:descendantFocusability="blocksDescendants">

类似这样的东西(这是一个随机的例子,所以你必须遵循你的布局,我创建这个只是为了让你能够理解你必须把这些信息放在哪里):

<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>

关于Android Listview item长按不响应CheckBox事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28855332/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com