Android:ListVIew:更改背景onClick

我有ListVIew,并且我想在单击它时更改项目背景,以显示它已被选中.但是,当我使用此代码(在文本下)时,它每隔13个项目就会更改一次背景色.例如:如果我选择1个项目并向下滚动,它将更改每个13个项目的颜色(1-13-26 ..).我只想更改一项的背景.

lvpl.setOnItemClickListener( new AdapterView.OnItemClickListener() {
     @Override
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
           TextView tv = (TextView) view
           tv.setBackgroundColor(Color.argb(125,75,236,90));
           final_ids.add(ids.get(position));
     }
});

解决方法:

问题是您的列表适配器正在重新使用移出屏幕的视图.

解决方案是在适配器中为其他视图设置默认颜色

public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) convertView.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         = inflater.inflate(
                R.layout.your_list_item, null);

    }
if(postion!=SelectedPosition)
  {
   convertView.setBackgroundColor(default Color);
    }
  else
   {
    convertView.setBackgroundColor(Color.argb(125,75,236,90));
   }



    return convertView;

}
上一篇:带有嵌套的RecyclerView的RecyclerView-使嵌套的RecyclerView可作为一个整体单击


下一篇:Android Intent 显式