【发布时间】:2018-11-30 03:28:57
【问题描述】:
是否可以在单击时更改列表视图项的颜色,并且在再次单击之前它将是该颜色?我使用适配器从 firebase 获取数据。
【问题讨论】:
是否可以在单击时更改列表视图项的颜色,并且在再次单击之前它将是该颜色?我使用适配器从 firebase 获取数据。
【问题讨论】:
如果你只想改变一次颜色:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
view.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
}
});
您可以使用以下方式切换列表视图项的更改:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
LoadListerListViewObject currentObject = loadListerListViewObjectArrayList.get(position);
//If the object is inactive...
if (!currentObject.getIsActivated()) {
//Set the object as active and change the color to green
loadListerListViewObjectArrayList.set(position, new LoadListerListViewObject(currentObject.getDate(), currentObject.getTagNumber() true));
view.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
//If the object is active...
} else {
//Set the object as active and change the color to grey
loadListerListViewObjectArrayList.set(position, new LoadListerListViewObject(currentObject.getDate(), currentObject.getTagNumber(), false));
view.setBackgroundColor(getResources().getColor(R.color.colorGreyForButton));
}
}
});
这使用关联列表视图对象的属性来检查项目是否已被选中,然后根据此更改颜色。我想你也想“不改变”颜色。你可能需要这样的东西。
【讨论】:
是的,可以在单击时更改列表视图项目的颜色,并且在您再次单击它之前它将是该颜色。 只需在您的适配器中编写一个项目单击并根据您的条件更改颜色。
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//Here you have view and position . so use both in a way you want.
}
});
如果您需要另一个示例,请告诉我。 #KeepCoding
【讨论】: