我正在制作具有不同背景和文本颜色的自定义 ListView 。我已经从我的自定义适配器中覆盖了 setSelected(int position) 方法。它在 7 英寸标签中工作正常,但在 Samsung Galaxy Note 平板电脑(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);
}
设置选择方法为
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);
}
关于Android 自定义 ListView - 更改所选项目的背景和文本颜色不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17463696/