【问题标题】:SetBackgroundColor affects multiple list itemsSetBackgroundColor 影响多个列表项
【发布时间】:2015-05-19 13:08:59
【问题描述】:

我正在开发一个自助服务终端应用程序(某些应用程序可用)。我制作了一个列表视图,显示手机上安装的所有应用程序。在此列表中,我希望用户选择允许在信息亭中使用的应用程序。如果用户选择一行(显示 1 个应用程序),则需要更改颜色并放入单独的列表(allowedAppList)中。

奇怪的是,当用户按下列表项时,颜色会正确更改。但它也会改变它下面每 8 个项目的颜色。

当一个列表项被按下时,我也会记录(见代码),它只会改变被点击的列表项的允许,而不是下面的每 8 个项目。

请帮帮我

//我只添加了点击列表项的代码

private void addClickListener(){
    allowedApps = new ArrayList<AppDetail>();
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        boolean isAllowed;

        public void onItemClick(AdapterView<?> av, View v, int pos,
                                long id) {

            isAllowed = apps.get(pos).allowed;
            if (isAllowed == false){
                apps.get(pos).allowed = true;
                Log.i("ALLOWED", apps.get(pos).name.toString());
                v.setBackgroundResource(R.color.green);

            }else if (isAllowed == true) {
                apps.get(pos).allowed = false;
                Log.i("NOT ALLOWED", apps.get(pos).name.toString());
                v.setBackgroundResource(R.color.white);

            }
        }
    });

编辑 适配器代码(我加载安装的应用程序的位置)

private PackageManager manager;
private List<AppDetail> apps;
private void loadApps(){
    manager = getPackageManager();
    apps = new ArrayList<AppDetail>();

    Intent i = new Intent(Intent.ACTION_MAIN, null);
    i.addCategory(Intent.CATEGORY_LAUNCHER);
    List<ResolveInfo> availableActivities = manager.queryIntentActivities(i, 0);
    for(ResolveInfo ri:availableActivities){
            AppDetail app = new AppDetail();
            app.label = ri.loadLabel(manager);
            app.name = ri.activityInfo.packageName;
            app.icon = ri.activityInfo.loadIcon(manager);
            app.allowed = false;
            apps.add(app);
    }
    Log.i("applist", apps.toString());
}

private ListView list;
private void loadListView(){
    list = (ListView)findViewById(R.id.apps_list);

    ArrayAdapter<AppDetail> adapter = new ArrayAdapter<AppDetail>(this,
            R.layout.list_item,
            apps) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView == null){
                convertView = getLayoutInflater().inflate(R.layout.list_item, null);
            }

            ImageView appIcon = (ImageView)convertView.findViewById(R.id.item_app_icon);
            appIcon.setImageDrawable(apps.get(position).icon);

            TextView appLabel = (TextView)convertView.findViewById(R.id.item_app_label);
            appLabel.setText(apps.get(position).label);

            TextView appName = (TextView)convertView.findViewById(R.id.item_app_name);
            appName.setText(apps.get(position).name);

            if (apps.get(position).allowed == true){
                convertView.setBackgroundColor(getResources().getColor(R.color.green));
            }

            return convertView;
        }
    };

    list.setAdapter(adapter);
}

【问题讨论】:

  • 请发布您的适配器代码,这可能是您的问题的原因。
  • 我认为错误在于您使用的是 Viewholder 但您没有为单击的视图设置标签,而是发布代码

标签: java android listview arraylist listitem


【解决方案1】:

问题是 ListView 中的行视图被回收。因此,一种解决方案是在该行后面的 AppDetail 对象上设置一个布尔属性以指示它已被单击,并在适配器的 getView() 函数中设置背景颜色。 相关代码如下所示:

public void onItemClick(AdapterView<?> av, View v, int pos,
                            long id) {
    [...]
    ((AppDetail) av.getAdapter().getItem(pos)).setClicked(true);
    [...]
}

在你的适配器中

public View getView(final int position, final View convertView, final ViewGroup parent) {
    [...]
    convertView.setBackgroundColor(getResources().getColor(apps.getPosition(position).isClicked() ? R.color.green : R.color.white));
    [...]
}

【讨论】:

  • 谢谢!我知道我做错了什么。我无法从 onItemClick 中的项目中获取位置。
  • 我觉得可以,不就是第三个参数吗?
  • 我的错,打错了!谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-12
  • 2017-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多