如何在android中的网格视图中禁用项目单击特定位置

use*_*237 9 android

我正在使用网格视图,其中我使用每个单元格的文本视图.我正在使用onitemclick在单击网格单元格时执行某些操作.我想在网格视图中禁用项目单击特定位置.我怎么做.我使用convertView.setclickable(false)来获取getView中的特定位置,它给出了空指针异常.我怎么做??这是我的代码.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
  if (convertView == null) {
    textView = new TextView(context);
    textView.setLayoutParams(new GridView.LayoutParams(35, 35));
  } else {
    textView = (TextView) convertView;
  }

        textView.setTextSize(10);
        day_color = list.get(position).split("-");
        textView.setText(day_color[0]);

        if (day_color[1].equals("GREY")) {
            textView.setTextColor(Color.LTGRAY);
            //greyvalues.get(position)CalendarEvents
            convertView.setClickable(false);

        }
        if (day_color[1].equals("BLACK")) {
            textView.setTextColor(Color.BLACK);
        }
        if ((day_color[1].equals("BLUE"))) {
            textView.setTextColor(Color.RED);
        }

        setColor = Color.TRANSPARENT;
        if (position >= startPos && position <= endPos
                && selectdayselected != true) {
            setColor = Color.DKGRAY;
        }
        if (startPos == position && selectdayselected == true)
            setColor = Color.DKGRAY;
        textView.setBackgroundColor(setColor);


        return textView;
    }
Run Code Online (Sandbox Code Playgroud)

slu*_*und 40

在适配器覆盖中

@Override
public boolean areAllItemsEnabled() {
    return false;
}
Run Code Online (Sandbox Code Playgroud)

并实施

@Override
public boolean isEnabled(int position) {
   // Return true for clickable, false for not
   return false;
}
Run Code Online (Sandbox Code Playgroud)

  • 请解释@slund (3认同)

Bri*_*ley 1

您需要将其设置为打开textView。可能没有convert view,调用时会导致NPE setClickable