繁体   English   中英

Android自定义列表视图-更改所选项目的背景和文本颜色不起作用?

[英]Android custom listview - change background and text color of selected item is not working?

我正在用不同的背景和文本颜色制作自定义列表视图。 我从我的自定义适配器中重写了setSelected(int position)方法。 它可以在7英寸的标签上正常工作,但不能在Samsung Galaxy Note Tablet(10.1英寸)上工作。

if(selectedPosition == position){
          ((TextView)convertView.findViewById(R.id.folderName)).setTextColor(Color.WHITE);
          ((LinearLayout)convertView.findViewById(R.id.folderLayout)).setBackgroundResource(R.drawable.folders_list_bg_s);
      }else{
          ((TextView)convertView.findViewById(R.id.folderName)).setTextColor(Color.BLACK); 
          ((LinearLayout)convertView.findViewById(R.id.folderLayout)).setBackgroundResource(R.drawable.folders_list_bg);
      }

和setselection方法为

public void setSelected(int position) {
    selectedPosition = position;
}

修改适配器后,我们需要使用notifyDatasetChanged()进行通知

if(selectedPosition == position){
      ((TextView)convertView.findViewById(R.id.folderName)).setTextColor(Color.WHITE);
      ((LinearLayout)convertView.findViewById(R.id.folderLayout)).setBackgroundResource(R.drawable.folders_list_bg_s);
      notifyDatasetChanged();
  }else{
      ((TextView)convertView.findViewById(R.id.folderName)).setTextColor(Color.BLACK); 
      ((LinearLayout)convertView.findViewById(R.id.folderLayout)).setBackgroundResource(R.drawable.folders_list_bg);
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM