单击时更改列表视图项的颜色
Change colour of listview item when clicked
是否可以在单击时更改列表视图项的颜色,并且在再次单击之前一直是该颜色?我使用适配器从 firebase 获取数据。
是的,可以在单击时更改列表视图中某个项目的颜色,并且在您再次单击之前它将一直是该颜色。
只需在您的适配器中写一个项目,然后根据您的情况更改颜色。
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
如果你只想换一次颜色:
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));
}
}
});
这使用关联列表视图对象的 属性 来检查项目是否已被选中,然后根据此更改颜色。我想您也想要 "un-change" 颜色。您可能需要这样的东西。
是否可以在单击时更改列表视图项的颜色,并且在再次单击之前一直是该颜色?我使用适配器从 firebase 获取数据。
是的,可以在单击时更改列表视图中某个项目的颜色,并且在您再次单击之前它将一直是该颜色。 只需在您的适配器中写一个项目,然后根据您的情况更改颜色。
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
如果你只想换一次颜色:
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));
}
}
});
这使用关联列表视图对象的 属性 来检查项目是否已被选中,然后根据此更改颜色。我想您也想要 "un-change" 颜色。您可能需要这样的东西。