简单说下思路吧, 设置boolean值标记,通过gridView的条目点击记录下被点击的条目,改变被点击条目的背景色即可实现
设置boolean标记
private boolean FaultBodyChoiced,FaultHandleBarChoiced,FaultChainChoiced,FaultwheelChoiced,FaulElockChoiced;
//创建集合存储被点击的条目
private ArrayList<String> AllFaultLock=new ArrayList<>();
private ArrayList<String> AllFaultbike=new ArrayList<>();
private List<String> FaultBody=new ArrayList<>();
private List<Integer> FaultBodyId=new ArrayList<>();
private List<String> FaultHandleBar=new ArrayList<>();
private List<Integer> FaultHandleBarId=new ArrayList<>();
private List<String> FaultChain=new ArrayList<>();
private List<Integer> FaultChainId=new ArrayList<>();
private List<String> Faultwheel=new ArrayList<>();
private List<Integer> FaultwheelId=new ArrayList<>();
private List<String> FaulElock=new ArrayList<>();
private List<Integer> FaulElockId=new ArrayList<>();
/**
* 设置故障gridview数据
*/
private void initGvData() {
gvFaultBikeBody.setAdapter(new ArrayAdapter<>(getApplicationContext(),R.layout.fault_init_info,FaultBody));
gvFaultBikeHandlebar.setAdapter(new ArrayAdapter<>(getApplicationContext(),R.layout.fault_init_info,FaultHandleBar));
gvFaultBikeWheel.setAdapter(new ArrayAdapter<>(getApplicationContext(),R.layout.fault_init_info,Faultwheel));
gvFaultBikeChain.setAdapter(new ArrayAdapter<>(getApplicationContext(),R.layout.fault_init_info,FaultChain));
gvFaultElock.setAdapter(new ArrayAdapter<>(getApplicationContext(),R.layout.fault_init_info,FaulElock));
//车身故障
gvFaultBikeBody.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//点击后改变boolean标记状态
FaultBodyChoiced=!FaultBodyChoiced;
TextView tvBody= (TextView) view.findViewById(R.id.tv_fault_info);
if (FaultBodyChoiced){
//设置背景色进行显示
tvBody.setBackgroundResource(R.mipmap.faultchoiced);
AllFaultbike.add(FaultBody.get(position));
}else{
tvBody.setBackgroundResource(R.mipmap.faultunchoiced);
if (AllFaultbike.contains(FaultBody.get(position))){
AllFaultbike.remove(FaultBody.get(position));
}
}
}
});
//车把
gvFaultBikeHandlebar.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
FaultHandleBarChoiced=!FaultHandleBarChoiced;
TextView tvBody= (TextView) view.findViewById(R.id.tv_fault_info);
if (FaultHandleBarChoiced){
tvBody.setBackgroundResource(R.mipmap.faultchoiced);
AllFaultbike.add(FaultHandleBar.get(position));
}else{
tvBody.setBackgroundResource(R.mipmap.faultunchoiced);
//重复点击后要从集合中移除数据
if (AllFaultbike.contains(FaultHandleBar.get(position))){
AllFaultbike.remove(FaultHandleBar.get(position));
}
}
}
});
//车轮
gvFaultBikeWheel.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
FaultwheelChoiced=!FaultwheelChoiced;
TextView tvBody= (TextView) view.findViewById(R.id.tv_fault_info);
if (FaultwheelChoiced){
tvBody.setBackgroundResource(R.mipmap.faultchoiced);
AllFaultbike.add(Faultwheel.get(position));
}else{
tvBody.setBackgroundResource(R.mipmap.faultunchoiced);
if (AllFaultbike.contains(Faultwheel.get(position))){
AllFaultbike.remove(Faultwheel.get(position));
}
}
}
});
//车链
gvFaultBikeChain.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
FaultChainChoiced=!FaultChainChoiced;
TextView tvBody= (TextView) view.findViewById(R.id.tv_fault_info);
if (FaultChainChoiced){
tvBody.setBackgroundResource(R.mipmap.faultchoiced);
AllFaultbike.add(FaultChain.get(position));
}else{
tvBody.setBackgroundResource(R.mipmap.faultunchoiced);
if (AllFaultbike.contains(FaultChain.get(position))){
AllFaultbike.remove(FaultChain.get(position));
}
}
}
});
//电子锁
gvFaultElock.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
FaulElockChoiced=!FaulElockChoiced;
TextView tvBody= (TextView) view.findViewById(R.id.tv_fault_info);
if (FaulElockChoiced){
tvBody.setBackgroundResource(R.mipmap.faultchoiced);
AllFaultLock.add(FaulElock.get(position));
}else{
tvBody.setBackgroundResource(R.mipmap.faultunchoiced);
if (AllFaultLock.contains(FaulElock.get(position))){
AllFaultLock.remove(FaulElock.get(position));
}
}
}
});
}