【问题标题】:If i change first item background color then after some items another item also get changed如果我更改第一个项目的背景颜色,那么在某些项目之后另一个项目也被更改
【发布时间】:2016-09-13 08:34:18
【问题描述】:

我也在 getView 中尝试过,这是为了突出显示所选项目。这是我的代码。我已经看到了与此相关但对我不起作用的每个答案。

 slots.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                for(int i=0;i<list.size();i++)
                {
                    View v=getViewByPosition(i,slots);

                    if(i==position)
                    {
                        v.setBackgroundColor(getResources().getColor(R.color.colorAccent));
                    }
                    else {
                        v.setBackgroundColor(getResources().getColor(R.color.AppBackground));
                    }

                }

            }
        });
        progress.dismiss();
    }

    public View getViewByPosition(int pos, HorizontalListView listView) {
        final int firstListItemPosition = listView.getFirstVisiblePosition();
        final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;

        if (pos < firstListItemPosition || pos > lastListItemPosition ) {
            return listView.getAdapter().getView(pos, null, listView);
        } else {
            final int childIndex = pos - firstListItemPosition;
            return listView.getChildAt(childIndex);
        }
    }

【问题讨论】:

  • 您是否在适配器中使用 ViewHolder 模式?
  • 如果你必须设置backgnd然后det它在适配器类。

标签: android listview scroll android-recyclerview horizontallistview


【解决方案1】:

ListView\RecyclerView的主要思想是View的复用。因此,当您在屏幕上滚动第一个实时项目时,移动到新的向下项目。 创建 Holder 类,包含 vie 列表。绑定方法,当你设置项目的正确颜色时,基于你的适配器模型。sample

【讨论】:

    猜你喜欢
    • 2012-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 2019-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多