android – 在向行添加按钮后,ListView行不可单击

前端之家收集整理的这篇文章主要介绍了android – 在向行添加按钮后,ListView行不可单击前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
目前,我有一个ListView行.单击该行上的任何区域将产生ListView单击事件.
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).

这是一个很好的解释:Having several clickable areas in a ListView

猜你在找的Android相关文章