設(shè)置ListView中的所有Item均不可點(diǎn)擊

設(shè)置ListView中的所有Item均不可點(diǎn)擊

正常地說(shuō)一般要是使一個(gè)控件不可點(diǎn)擊,不外乎使用以下幾種方法

  • setEnable
  • setClickable
  • setFocusable

但是對(duì)于ListView如果直接使用以上方法均失效,想要達(dá)到使ListView下所有Item均不可點(diǎn)擊的狀態(tài)(置灰),需要重寫Adapter下的isEnable方法,并且配合getView達(dá)到上述效果

1、首先需要在Adapter中添加一個(gè)標(biāo)志位
boolean isAllItemEnable=true

2、重寫isEnable方法
@Override public boolean isEnabled(int position) { return isAllItemEnable; }

3、增加是否開(kāi)啟全選的兩個(gè)方法

public void disableAllItemChoser() {
        isAllItemEnable = false;
        notifyDataSetChanged();
    }
    public void enableItemChoser() {
        isAllItemEnable = true;
        notifyDataSetChanged();
    } 

4、修改getView方法
@Override public View getView(int position, View convertView, ViewGroup arg2) { ViewHolder holder; if (convertView == null) { ... } else { ... } // 控制是否置灰 if (!isAllItemEnable) { holder.textView_contactsinfo.setEnabled(false); holder.checkBox_contactsinfo.setEnabled(false); } else { holder.textView_contactsinfo.setEnabled(true); holder.checkBox_contactsinfo.setEnabled(true); } return convertView; }

5、調(diào)用

  • 設(shè)置所有不可選
    myAdapter.disableAllItemChoser()

  • 設(shè)置所有可選
    myAdapter.enableItemChoser()

最后,附上本人自己寫的一個(gè)Adapter,原理和上述一樣
public class MessInfoAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private String[] stringType;
private boolean isAllItemEnable = true;

    public MessInfoAdapter(Context context, String[] stringType) {

        this.stringType = stringType;
        this.mInflater = LayoutInflater.from(context);
        functionSelectedList = new SparseBooleanArray();
        for (int i = 0; i < mData.length; i++) {
            functionSelectedList.put(i, false);
        }

        attachmentSelectedList = new SparseBooleanArray();
        for (int i = 0; i < attachmentData.length; i++) {
            attachmentSelectedList.put(i, false);
        }
    }

    @Override
    public boolean isEnabled(int position) {
        return isAllItemEnable;
    }

    public void disableAllItemChoser() {
        isAllItemEnable = false;
        notifyDataSetChanged();
    }

    public void enableItemChoser() {
        isAllItemEnable = true;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return stringType.length;
    }

    @Override
    public Object getItem(int arg0) {
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(
                    R.layout.contactsfill_list_item, null);
            holder.textView_contactsinfo = (TextView) convertView
                    .findViewById(R.id.textview_contactsinfo);
            holder.checkBox_contactsinfo = (CheckBox) convertView
                    .findViewById(R.id.checkbox_contactsinfo);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        // 針對(duì)不同的ListView填充不同的數(shù)據(jù)
        if (java.util.Arrays.equals(stringType, mData)) {
            holder.textView_contactsinfo.setText(mData[position]);
            holder.checkBox_contactsinfo.setChecked(functionSelectedList
                    .get(position));
        } else if (java.util.Arrays.equals(stringType, attachmentData)) {
            holder.textView_contactsinfo.setText(attachmentData[position]);
            holder.checkBox_contactsinfo.setChecked(attachmentSelectedList
                    .get(position));
        }
        if (!isAllItemEnable) {
            holder.textView_contactsinfo.setEnabled(false);
            holder.checkBox_contactsinfo.setEnabled(false);
        } else {
            holder.textView_contactsinfo.setEnabled(true);
            holder.checkBox_contactsinfo.setEnabled(true);
        }
        return convertView;
    }

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容