草庐IT

android - ArrayAdapter 自定义 View 状态在滚动时重复

coder 2023-12-21 原文

我有一个 ListView,其中填充了自定义 View 项。自定义 View 由一个图标、一个标签和一个复选框组成。当我第一次创建列表时,一切都按预期显示。如果我向下滚动列表,图标和标签在列表的下方继续正确,但复选框状态开始混淆,显示除了我选择的项目之外的其他项目。

示例:我的列表开始时没有为任何项目设置为选中的复选框。我在屏幕上看到 10 个项目。我切换了项目 10 上的复选框。它会适当更新。如果我向下滚动列表,我会发现第 20 项、第 30 项等的复选框从已经切换的复选框开始,即使它们永远无法与之交互。如果我反复来回滚动,越来越多的项目以无法识别的模式显示为已选中。

在我的 Activity 中列出设置:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.default_list);

        profile = (Profile) i.getParcelableExtra("profile");

        ArrayList<Application> apps = new ApplicationListRetriever(this).getApplications(true);

        adapter = new ApplicationsAdapter(this, R.layout.application_list_item, apps, getPackageManager(), profile);
        setListAdapter(adapter);        
    }

应用适配器:

public class ApplicationsAdapter extends ArrayAdapter<Application> {
    private ArrayList<Application> items;
    private PackageManager pm;
    private Profile profile;

    private ArrayList<ApplicationListener> listeners = new ArrayList<ApplicationListener>();

    public ApplicationsAdapter(Context context, int textViewResourceId,
            ArrayList<Application> objects, PackageManager pm, Profile profile) {
        super(context, textViewResourceId, objects);

        this.pm = pm;
        items = objects;
        this.profile = profile;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater li = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = li.inflate(R.layout.application_list_item, null);
        }
        final Application info = items.get(position);
        if (info != null) {
            TextView text = (TextView) v.findViewById(R.id.label);
            CheckBox check = (CheckBox) v.findViewById(R.id.check);
            ImageView img = (ImageView) v.findViewById(R.id.application_icon);

            //see if the app already is associated and mark checkbox accordingly
            for (Application app : profile.getApps()) {
                if (info.getPackageName().equals(app.getPackageName())) {
                    check.setChecked(true);
                    break;
                }
            }

            check.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    for (ApplicationListener listener : listeners) {
                        listener.applicationReceived(info, isChecked);
                    }
                }
            });

            try {
                text.setText(info.getName());
            }
            catch (Exception ex) {
                Log.e("ApplicationsAdapter", "Label could not be set on adapter item", ex);
            }

            if (img != null) {

                try {
                    img.setImageDrawable(pm.getApplicationIcon(info.getPackageName()));
                } catch (NameNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }

        return v;
    }
}

列表项布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="wrap_content">
    <ImageView android:id="@+id/application_icon"
        android:layout_centerVertical="true" android:layout_alignParentLeft="true"
        android:layout_width="36dp" android:layout_height="36dp" android:paddingRight="3dp" android:adjustViewBounds="true" />
    <TextView android:layout_width="wrap_content"
        android:layout_centerVertical="true" android:layout_toRightOf="@+id/application_icon"
        android:layout_height="wrap_content" android:id="@+id/label"
        android:text="Application Name" />
    <CheckBox android:id="@+id/check"
        android:layout_centerVertical="true" android:layout_alignParentRight="true"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_gravity="right" />
</RelativeLayout>

另外值得注意的是,如果我在调用 check.setChecked(true); 的行上设置断点,它只会在我检查的原始项目出现在屏幕上时触发该点,而不会出现任何显示为已选中的其他项目。

为什么后面的项目会显示为已选中或我可以尝试如何修复它有什么想法吗?

最佳答案

是View回收造成的。调用 getView 时,您应该完全刷新 View 的状态。尽管您几乎刷新了所有内容,但您却忘记了一件小事。

假设列表在屏幕上显示 5 个项目并且选中了第二个项目。然后用户向下滚动另外 5 个项目 - 但是由于 View 回收,它们实际上与用户滚动之前的 5 个 View 相同,所以屏幕上的项目之一仍然会被检查,即使它不应该被检查,因为在您上面的代码中,如果没有匹配的包,您没有将复选框设置为未选中(因此它将保持选中状态),您假设它已经未选中(由于查看回收可能不是)。

修复很简单:只需在您的逻辑检查是否需要选中之前将复选框设置为未选中:

        // Do not assume the checkbox is unchecked to begin with, it might not be
        // if this view was recycled, so force it to be unchecked by default and only
        // check it if needed.
        check.setChecked(false); 
        for (Application app : profile.getApps()) {
            if (info.getPackageName().equals(app.getPackageName())) {
                check.setChecked(true);
                break;
            }
        }

关于android - ArrayAdapter 自定义 View 状态在滚动时重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7540504/

有关android - ArrayAdapter 自定义 View 状态在滚动时重复的更多相关文章

  1. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  2. ruby - 在 Ruby 程序执行时阻止 Windows 7 PC 进入休眠状态 - 2

    我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0

  3. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  4. ruby-on-rails - 渲染另一个 Controller 的 View - 2

    我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>

  5. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  6. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

  7. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  8. ruby-on-rails - 跳过状态机方法的所有验证 - 2

    当我的预订模型通过rake任务在状态机上转换时,我试图找出如何跳过对ActiveRecord对象的特定实例的验证。我想在reservation.close时跳过所有验证!叫做。希望调用reservation.close!(:validate=>false)之类的东西。仅供引用,我们正在使用https://github.com/pluginaweek/state_machine用于状态机。这是我的预订模型的示例。classReservation["requested","negotiating","approved"])}state_machine:initial=>'requested

  9. ruby-on-rails - 如何在我的 Rails 应用程序 View 中打印 ruby​​ 变量的内容? - 2

    我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby​​中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R

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

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

随机推荐