草庐IT

android - 查找 Listview 项目的像素位置似乎返回错误值

coder 2023-12-17 原文

我有一个 ListView,其中包含许多高度不同的项目(动物)。使用 OnScrollListener 我试图跟踪哪个项目与屏幕上的特定位置相交。假设有问题的位置是 creatureMarkerBottom = 140。当我运行代码时,下面的代码似乎返回了错误的数据:我不断收到误报和漏报。这是代码。该代码应该使标记变为实心或透明,具体取决于鸡是否与它相交。然而,褪色并不真正服从鸡是否接触到板/条。我的猜测是我获取 ListView 像素位置的方式有误。

OnScrollListener listviewScrollListener = new OnScrollListener() {
        int creatureLocationPixel[] = { 0, 0 };
        int creatureMarkerBottom;
        int creatureTop, creatureBottom;
        int[] creatureLocationPixel = { 0, 0 };
        View creatureView;
        boolean creatureMarkerIsFaded = false;

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {

        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            try {
                scrollBackgroundToFindCreature(visibleItemCount, firstVisibleItem);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        private void scrollBackgroundToFindCreature(int visibleItemCount, int index) {
            creatureMarkerSlabView.getLocationOnScreen(creatureLocationPixel);
            creatureMarkerBottom = creatureLocationPixel[1] + creatureMarkerSlabView.getHeight();
            Animal creature;
            boolean found = false;
            do {
                creature = mAdapter.getItem(index);
                creatureView = getViewForPosition(index);
                creatureView.getLocationOnScreen(creatureLocationPixel);
                creatureTop = creatureLocationPixel[1];
                creatureBottom = creatureTop + creatureView.getHeight();
                if (creatureTop < creatureMarkerBottom  && creatureMarkerBottom  < creatureBottom) {
                    found = true;
                } else {
                    index++;
                }
            } while (!found && index < visibleItemCount);

            if (creatureType.CHICKEN != creature.getType()) {
                if (!creatureMarkerIsFaded) {
                    creatureMarkerIsFaded = true;
                    for (int x = 0; x < creatureMarkerSlabView.getChildCount(); x++)
                        creatureMarkerSlabView.getChildAt(x).setAlpha(TRANSPARENCY_ALPHA);
                    creatureMarkerSlabView.setAlpha(TRANSPARENCY_ALPHA);
                }

            } else {
                if (creatureMarkerIsFaded) {
                    creatureMarkerIsFaded = false;
                    for (int x = 0; x < creatureMarkerSlabView.getChildCount(); x++)
                        creatureMarkerSlabView.getChildAt(x).setAlpha(255);
                    creatureMarkerSlabView.setAlpha(255);
                }
            }
        }

    };

public View getViewForPosition(int position) {
        int firstPosition = animalListview.getFirstVisiblePosition() - animalListview.getHeaderViewsCount();
        int wantedChild = position - firstPosition;
        // Say, first visible position is 8, you want position 10, wantedChild will now be 2
        // So that means your view is child #2 in the ViewGroup:
        if (wantedChild < 0 || wantedChild >= animalListview.getChildCount()) {
            return null;
        }
        return animalListview.getChildAt(wantedChild);
    }

最佳答案

问题解决说明understanding ListView childAt methodwhat limits the index for listview.getChildAt(index) .问题是我误解了 listView.getChildCount()adapter.getCount() 之间的关系。

简述:

根据我的实验,这是它的工作原理。 ListView 是适配器的一部分。因此,如果适配器有 500 个项目,而 ListView 有十 (10) 个。 ListView 中的十个代表一个动态 View 。因此,如果 firstVisibleItem 是适配器中的项目 217,则 ListView 的索引范围将为 (217,…,226),而 listView.getChildCount() 仍将返回 10。

因此答案是:

getChildAt(x) | x : [0+firstVisibleItem, listview.getChildCount()+firstVisibleItem)

关于android - 查找 Listview 项目的像素位置似乎返回错误值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19041222/

有关android - 查找 Listview 项目的像素位置似乎返回错误值的更多相关文章

  1. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  2. ruby - 如何在 buildr 项目中使用 Ruby 代码? - 2

    如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby​​

  3. ruby - 为什么 4.1%2 使用 Ruby 返回 0.0999999999999996?但是 4.2%2==0.2 - 2

    为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返

  4. ruby-on-rails - 项目升级后 Pow 不会更改 ruby​​ 版本 - 2

    我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby​​版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby​​版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘

  5. ruby-on-rails - 新 Rails 项目 : 'bundle install' can't install rails in gemfile - 2

    我已经像这样安装了一个新的Rails项目:$railsnewsite它执行并到达:bundleinstall但是当它似乎尝试安装依赖项时我得到了这个错误Gem::Ext::BuildError:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcheckingforlibkern/OSAtomic.h...yescreatingMakefilemake"DESTDIR="cleanmake"DESTDIR="

  6. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

  7. ruby - 检查字符串是否包含散列中的任何键并返回它包含的键的值 - 2

    我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案

  8. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  9. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  10. ruby-on-rails - 错误 : Error installing pg: ERROR: Failed to build gem native extension - 2

    我克隆了一个rails仓库,我现在正尝试捆绑安装背景:OSXElCapitanruby2.2.3p173(2015-08-18修订版51636)[x86_64-darwin15]rails-v在您的Gemfile中列出的或native可用的任何gem源中找不到gem'pg(>=0)ruby​​'。运行bundleinstall以安装缺少的gem。bundleinstallFetchinggemmetadatafromhttps://rubygems.org/............Fetchingversionmetadatafromhttps://rubygems.org/...Fe

随机推荐