日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

设置ListView中的所有Item均不可点击

發布時間:2023/12/14 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 设置ListView中的所有Item均不可点击 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

設置ListView中的所有Item均不可點擊

正常地說一般要是使一個控件不可點擊,不外乎使用以下幾種方法
- setEnable
- setClickable
- setFocusable

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

1、首先需要在Adapter中添加一個標志位

`boolean isAllItemEnable=true`

2、重寫isEnable方法

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

3、增加是否開啟全選的兩個方法

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

4、修改getView方法

@Overridepublic 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、調用

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

最后,附上本人自己寫的一個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);}}@Overridepublic boolean isEnabled(int position) {return isAllItemEnable;}public void disableAllItemChoser() {isAllItemEnable = false;notifyDataSetChanged();}public void enableItemChoser() {isAllItemEnable = true;notifyDataSetChanged();}@Overridepublic int getCount() {return stringType.length;}@Overridepublic Object getItem(int arg0) {return null;}@Overridepublic long getItemId(int arg0) {return 0;}@Overridepublic 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();}// 針對不同的ListView填充不同的數據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;}}

總結

以上是生活随笔為你收集整理的设置ListView中的所有Item均不可点击的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。