OnClickListener 不适用于 GridView 中的第一项

2024-03-02

我在创建基于 GridView 的日历时遇到问题。这是网格:

这应该是一个填充了事件的日历,因此我让我的适配器实现 OnClickListener,并为日历中的每个按钮设置该侦听器。它适用于每个按钮EXCEPT第一个(在本例中为 30)。当我单击它时,它不起作用,但是当我尝试单击第一个按钮后单击另一个按钮时,它会在单击另一个按钮之前执行第一个按钮的单击。

我扫描了大约10页的相关问题,没有发现有人有这个问题。请帮助!

正如所问,这是我的代码的 getView 函数:

    public View getView(int position, View convertView, ViewGroup parent)
    {
        View row = convertView;
        ViewHolder holder;
        if (row == null)
        {
            holder = new ViewHolder();
            LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.calendar_day_gridcell, parent, false);
            holder.gridCell = (Button) row.findViewById(R.id.calendar_day_gridcell);
            holder.multiDayEvent = (EventLengthView)row.findViewById(R.id.eventLengthView);
        }
        else{
            holder = (ViewHolder)row.getTag();
        }

        int calendarGridHeight = (calendarView.getHeight()-5)/(getCount()/7);
        AbsListView.LayoutParams params = new AbsListView.LayoutParams(
                android.view.ViewGroup.LayoutParams.FILL_PARENT,
                calendarGridHeight);
        row.setLayoutParams(params);

        //Change the background drawable depending on the position in the calendar
        if ((position+1) % 7 == 0){
            holder.gridCell.setBackgroundDrawable(getResources().getDrawable(R.drawable.calendar_button_selector_end_row));
        }
        if (getCount() - position < 8){
            holder.gridCell.setBackgroundDrawable(getResources().getDrawable(R.drawable.calendar_button_selector_end_column));
        }
        if (position == getCount()-1){
            holder.gridCell.setBackgroundDrawable(getResources().getDrawable(R.drawable.calendar_button_selector_end));
        }
        holder.gridCell.setOnClickListener(this);

        holder.gridCell.setTag(null);//clear tags

        // ACCOUNT FOR SPACING
        String[] day_color = list.get(position).split("-");
        int theday = Integer.parseInt(day_color[0]);
        int themonth = Integer.parseInt(day_color[2]);
        int theyear = Integer.parseInt(day_color[3]);
        String date = DateFormat.format("dd/M/yy", new Date(theyear,themonth,theday)).toString();
        if ((!eventsMap.isEmpty()) && (eventsMap != null))
        {
            if (eventsMap.containsKey(date))
            {
                holder.multiDayEvent.SetMeasure(calendarView.getWidth()/7, calendarGridHeight);

                holder.multiDayEvent.setVisibility(View.VISIBLE);
                //holder.singleDayEvent.setVisibility(View.VISIBLE);
                Event event = (Event) eventsMap.get(date);
                holder.multiDayEvent.AddEvent(event);
                holder.gridCell.setTag(event);
            }
            else{
                //holder.singleDayEvent.setVisibility(View.GONE);
                holder.multiDayEvent.setVisibility(View.GONE);
            }
        }

        // Set the Day GridCell
        holder.gridCell.setText(Integer.toString(theday));

        if (day_color[1].equals("GREY"))
        {
            holder.gridCell.setTextColor(Color.GRAY);
        }
        if (day_color[1].equals("WHITE"))
        {
            holder.gridCell.setTextColor(Color.WHITE);
        }
        if (day_color[1].equals("BLUE"))
        {
            holder.gridCell.setTextColor(Color.BLUE);
        }

        row.setTag(holder);
        return row;
    }

    public class ViewHolder{
        Button gridCell;
        ImageView singleDayEvent;
        EventLengthView multiDayEvent;
    }

    public void onClick(View view)
    {
        if (view.getTag() != null){
            Event event = (Event)view.getTag();

            eventListView.setAdapter(new EventListAdapter(CalendarScreen.this, event));
            eventListViewLayout.setVisibility(View.VISIBLE);
            eventListViewLayout.startAnimation(fadeIn);
        }
        else if (eventListViewLayout.getVisibility() == View.VISIBLE){
            onBackPressed();
        }
    }

除左上角的第一个网格单元外,每个网格单元都会调用 onClick


我遇到了同样的问题。将 setLayoutParams 保留在 if(view == null) 子句中可以解决问题。您不必以这种方式牺牲视图回收。

like :

 if(row == null){
   // inflate row
   row.setLayoutParams(params);
   //remaining code
 }else{
    holder = (ViewHolder)row.getTag();
 }
 // everything else

我不知道为什么它有效,但它对我有用。我还注意到 stackoverflow 中有关同一问题的所有代码也在 if 子句之外使用了 setLayoutParams 。这是我第一次,所以我不知道是否可以将其发布到任何地方。希望有帮助。

资料来源:大量的试验和错误。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

OnClickListener 不适用于 GridView 中的第一项 的相关文章

随机推荐

  • 获取图像上显示屏4个角的坐标

    我正在尝试获取图像上屏幕 显示 的 4 个角 我有从同一位置拍摄的两张图像 所以我认为好的起点是提取两张图像 第一张图像和第二张图像 之间的差异 只是屏幕上的图像发生了变化 所以我想获取显示屏角的上 下左 右 X Y 坐标 我正在使用带有
  • cURL 和 Golang POST 的不同响应 - 无法理解为什么

    我正在尝试使用 golang 从服务器获取响应http客户 我希望通过 go 执行的请求应与以下内容相同curl命令 curl data fulladdress 22280 S 209th Way 2C Queen Creek 2C AZ
  • 如何在 Android 中将评级栏居中

    I have created a custom RatingBar as described in one of the tutorials given here on stack overflow and it s looking gre
  • 收集要安装的项目时发生错误

    当我尝试更新 Android SDK 时 出现以下错误 我应该怎么办 An error occurred while collecting items to be installed session context was profile
  • 通过 Python API 加载 JSONL 数据时检索 BigQuery 验证错误

    将 JSONL 文件加载到 BigQuery 时 如何检索与验证错误相关的更多信息 问题不是为了解决问题 示例代码 from google cloud bigquery import LoadJobConfig QueryJobConfig
  • 什么 ABI(如果有)限制 [u]intmax_t 的大小?

    从1999年版开始 ISO C标准定义了一个标准头
  • 使用 SFINAE 在 GCC 和 Clang 上给出不同的结果

    我正在学习如何利用 SFINAE 来发挥我的优势 我试图用它来根据是否存在来选择函数实现serialize 对象中的函数 这是我用来确定类型是否定义了 serialize 函数的代码 template
  • Crashlytics 报告错误的行号

    在过去的几周里 我在导入语句行中看到了很多与行号有关的崩溃 例如 这个堆栈跟踪在第 2 行发生崩溃 Fatal Exception java lang IllegalArgumentException Unexpected TLS vers
  • VanillaJS 到 VueJS 错误:Flickity 的错误元素:轮播

    我想得到这个轻快的例子 https codepen io desandro pen RNQgpB CodePen 在 VueJS 组件中工作 HTML div class carousel div class carousel cell d
  • Laravel ORM从自引用表获取N级层次结构JSON

    我在用LARAVEL 4 with MySQL后端 我有一个self referencing带列的表id name type and parent Here parent is foreign key列的Id 表中数据如下 id name
  • 使用 Angular-CLI 安装第 3 方应用程序

    我是 angular cli 的新手 我想安装 npm 库 mdbootstrap 我按照这里的说明进行操作 Angular CLI 说明 https github com angular angular cli global librar
  • WebStorm错误:表达式语句不是赋值或调用

    我正在使用 WebStorm 但收到一个我无法理解的错误 Node js MongoDB var mongoose require mongoose mongoose Promise global Promise mongoose conn
  • 正确检查 nil sqlite 文本列

    我有一个分配给字符串的 sqlite 字符串列 我需要在分配之前确保它不为零 我正在这样做 char isNil sqlite3 column text selectstmt 2 if isNil nil myName NSString s
  • OS X Byobu 垂直分割

    我目前在 OS X 上使用 iTerm2 和 byobu 但我不知道如何在 OS X 上垂直分割屏幕 在 Linux 上 我可以使用 Ctrl F2 垂直分割屏幕 但在 OSX 上则不行 不行 这要归功于苹果的不同键盘 我用谷歌搜索了所有解
  • java 缓冲图像:检测黑色像素

    我有这个简单的代码来浏览 24 位彩色 Windows bmp 文件 BufferedImage mapa BMPDecoder read new File maps map bmp final int xmin mapa getMinX
  • 什么时候会发生下溢?

    我陷入了计算的情况1 77e 308 10触发下溢异常 但计算1 777e 308 10才不是 这很奇怪 因为 当浮点的真实结果发生下溢 操作的幅度小于 即接近于零 可表示为普通浮点数的最小值 目标数据类型 来自算术下溢 维基百科 换句话说
  • 三.js透明物体遮挡

    在 Three js 场景中 我希望有一个不可见的对象 但仍然遮挡场景中的其他对象 就好像它是可见的一样 这可以通过 Three js 库实现吗 这是一个例子 Suppose I have a three js scene that con
  • 在elasticsearch实例上打开开放的GCE防火墙以进行外部连接

    我刚刚使用 Click to Deploy 在 GCE 中创建了一个 elasticsearch 集群 但我只能使用隧道来访问它 就我而言 这不是一个选择 因为我不想向其他开发人员授予 gcloud 访问权限 我想为elasticsearc
  • 访问生成 T-SQL 中的 ROW_NUMBER() 等结果的查询

    MS Access 中有 ROW NUMBER 函数吗 如果有 请让我知道它的任何语法 因为我被困在这里 我尝试过论坛 但我得到了 sql server 语法 以下是我的查询 select ROW NUMBER OVER ORDER BY
  • OnClickListener 不适用于 GridView 中的第一项

    我在创建基于 GridView 的日历时遇到问题 这是网格 这应该是一个填充了事件的日历 因此我让我的适配器实现 OnClickListener 并为日历中的每个按钮设置该侦听器 它适用于每个按钮EXCEPT第一个 在本例中为 30 当我单