我试图制作自定义ListView. ListView包含一个包含TextView和Switch的RelativeLayout.当您按下Switch时,Switch必须从true更改为false(反之亦然).
这是我的getView方法:
public View getView(int position,View convertView,ViewGroup parent) { View vi = convertView; if (convertView == null) vi = inflater.inflate(R.layout.item_gproblem,null); //vi.setClickable(true); Tried this //vi.setFocusable(true); TextView txt_comment_description = (TextView) vi .findViewById(R.id.txt_comment_description); txt_comment_description.setText(MyTasks.allGComments.get(position) .getProblemDescription()); //txt_comment_description.setFocusable(false); Tried this //txt_comment_description.setClickable(false); Switch switch_comment = (Switch) vi.findViewById(R.id.switch_comment); //switch_comment.setFocusable(false); Tried this //switch_comment.setClickable(false); //First time running getMyGComments returns a empty ArrayList if (MyTasks.allCustomers.get(ServerData.myID - 1).getMyGComments() .size() > position) { switch_comment.setChecked(MyTasks.allCustomers .get(ServerData.myID - 1).getMyGComments().get(position) .isProblemValue()); } return vi; }
这是我的onClickListener:
list_quality.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent,View view,int position,long id) { //Do Something } });
单击TextView,Switch或两个对象之间的空格时,不会调用我的onItemClickListener.当我按下开关时,开关正常工作(状态改变).但我没有调用我的onItemClickListener.我试图禁用Switch和TextView的可点击和可聚焦,但这也不起作用.
执行setOnItemClickListeren.