我实际上遇到了一个我不知道如何解决的问题...
事实上,当我点击它时,我想改变颜色并在listView上显示图片。
问题是当我点击我的第一个项目时,第0个,第9个,第18个等...也改变了...它就像View onItemClick只接受listview的可见部分(显示的项目) ,没有滚动)
我不知道如何解决它。
我的适配器:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater
.inflate(R.layout.item_anomalies, null);
holder = new ViewHolder();
holder.txtTitre = (TextView) convertView
.findViewById(R.id.titre);
holder.img = (ImageView) convertView
.findViewById(R.id.img);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final AnomalieRowItem rowItem = (AnomalieRowItem) getItem(position);
holder.txtTitre.setText(rowItem.getLibelle());
if (position % 5 == 0) {
holder.img.setBackgroundColor(0xffED695A);
} else {
if (position % 4 == 0) {
holder.img.setBackgroundColor(0xffEB5443);
} else {
if (position % 3 == 0) {
holder.img.setBackgroundColor(0xffEA4735);
} else {
if (position % 2 == 0) {
holder.img.setBackgroundColor(0xffE93825);
} else {
holder.img.setBackgroundColor(0xffD52815);
}
}
}
}
return convertView;
}
我的听众:
private void initListener() {
m_ListeAnomalie.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
AnomalieRowItem item = m_ListeItem.get(position);
ImageView imgCheck = (ImageView) view.findViewById(R.id.check);
LinearLayout layout = (LinearLayout) view.findViewById(R.id.item);
if (item.isChecked()) {
layout.setBackgroundColor(0xffefefef);
item.setChecked(false);
imgCheck.setVisibility(View.GONE);
} else {
layout.setBackgroundColor(0xffdddddd);
item.setChecked(true);
imgCheck.setVisibility(View.VISIBLE);
}
m_Adapter.notifyDataSetChanged();
}
});
m_Valider.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
end();
}
});
}
感谢您的帮助
编辑:具有模数和背景的部分工作正常,我的问题在于听众,它就像是在OnItemClick函数中注入的正确视图编辑2:有了图片,我认为它更好
答案 0 :(得分:1)
if (position % 5 == 0) {
holder.img.setBackgroundColor(0xffED695A);
} else {
if (position % 4 == 0) {
holder.img.setBackgroundColor(0xffEB5443);
} else {
if (position % 3 == 0) {
holder.img.setBackgroundColor(0xffEA4735);
} else {
if (position % 2 == 0) {
holder.img.setBackgroundColor(0xffE93825);
} else {
holder.img.setBackgroundColor(0xffD52815);
}
}
}
}
// add this lines in getView() after above logic.
if (rowItem.isChecked()) {
layout.setBackgroundColor(0xffefefef);
imgCheck.setVisibility(View.GONE);
} else {
layout.setBackgroundColor(0xffdddddd);
imgCheck.setVisibility(View.VISIBLE);
}
答案 1 :(得分:0)
您没有在getView中使用rowItem.isChecked()
。