if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) { this.getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); this.getListView().setMultiChoiceModeListener(new ModeCallback()); this.getListView().setOnItemClickListener(new ListViewOnItemClickListener()); } else { // https://stackoverflow.com/questions/9754170/listview-selection-remains-persistent-after-exiting-choice-mode this.getListView().setOnItemLongClickListener(new ListViewOnItemLongClickListener()); this.getListView().setOnItemClickListener(new ListViewOnItemClickListener()); }
现在,对于每一行,我想添加一个小按钮.单击小按钮将产生按钮单击事件,这与ListView原始单击事件不同.
在我的ArrayAdapter中,我习惯了
public View getView(int position,View convertView,ViewGroup parent) { View rowView = convertView; if (rowView == null) { LayoutInflater inflater = activity.getLayoutInflater(); rowView = inflater.inflate(R.layout.watchlist_row_layout,null); ... } Button button = (Button)rowView.findViewById(R.id.button1); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Utils.showLongToast("BUTTON CLICK"); } });
单击该按钮将产生按钮单击事件.但是,单击按钮以外的区域,将不再产生任何ListView单击事件.
解决方法
android:focusable="false"
为你的按钮. ImageButtons,RadioButtons,……
Android has been primarily designed for a large set of input methods.
The entire system is completely capable of working with no touch
screen. To navigate through the UI,the user can use a directional pad
which focuses Views after Views if and only if those Views are
focusable. By default,all Android controls are focusable. In order to
prevent having controls that are not focus-reachable,the ListView
will simply prevent the selection (and click) of an itemview. By
design,the ListView blocks clicks of itemview containing at least one
focusable descendant but it doesn’t make the content focus-reachable
calling setItemsCanFocus(true).