【问题标题】:how to disable item click for particular positions in grid view in android如何在android的网格视图中禁用特定位置的项目点击
【发布时间】:2023-04-06 01:58:01
【问题描述】:

我正在使用网格视图,其中我为每个单元格使用文本视图。单击网格单元格时,我正在使用 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;
    }

【问题讨论】:

    标签: android


    【解决方案1】:

    比如你关于条件的代码:

     @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    
    ...
    
            if (day_color[1].equals("GREY")) {
                textView.setTextColor(Color.LTGRAY);
                //greyvalues.get(position)CalendarEvents
                convertView.setClickable(false);
    ...
    
        }
    

    您的情况可能是:

                    if (position == 0){
                        isEnabled(position)
                    }
    

    代码功能:

     @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    
          ...
    
          if (position == 0){
              isEnabled(position)
          }
          ...
    
        }
    
    @Override
    public boolean isEnabled(int position){
        if(position == 0)
            return false;
    
        else
            return true;
    }
    
    @Override
    public boolean areAllItemsEnabled(){
    
        return true;
    }
    

    代码适合您的需求,我希望它对您有用。

    【讨论】:

      【解决方案2】:

      在您的适配器覆盖中

      @Override
      public boolean areAllItemsEnabled() {
          return false;
      }
      

      并实施

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

      【讨论】:

      • 您能解释一下这个解决方案吗?
      • 在方法isEnabled上,您可以设置职位的条件。
      【解决方案3】:

      我使用textView.setEnabled(false) 禁用了它的onItemClick()。它对我有用。

      【讨论】:

        【解决方案4】:

        您需要将其设置为textView。可能没有convert视图,调用setClickable会导致NPE。

        【讨论】:

          【解决方案5】:

          我已搜索此列表,但找不到任何适合我的答案。显然,我尝试了以下代码并且它有效。

          我在网格中有一个文本视图,并且文本视图被禁用,但是 onClick 方法适用于网格(因为我们需要捕获单击的位置)并且它始终处于启用状态。所以,我决定像这样检查网格的子元素是启用还是禁用...

          grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                  @Override
                  public void onItemClick(AdapterView<?> view, View cell, int position, long id)
                  {
                      if (grid.getChildAt(position).isEnabled()) //do whatever you want
          }
          });
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-10-06
            • 2020-02-17
            • 1970-01-01
            • 2022-07-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多