我得到了解決方案,感謝所有回覆的人。
這是我做的。
在我adpater
public HashMap<Integer, Boolean> hashMapSelected;
在我的適配器構造
hashMapSelected = new HashMap<>();
for (int i = 0; i < headers.size(); i++) {
hashMapSelected.put(i, false);
}
在我的適配器getView
if (hashMapSelected.get(position) == true) {
viewHolder.imageView.setColorFilter(ContextCompat.getColor(mContext, R.color.violet));
viewHolder.textView.setTextColor(ContextCompat.getColor(mContext, R.color.violet));
} else {
viewHolder.imageView.setColorFilter(ContextCompat.getColor(mContext, R.color.gridsepration));
viewHolder.textView.setTextColor(ContextCompat.getColor(mContext, R.color.gridsepration));
}
一個額外的方法來清除適配器其他項目
public void makeAllUnselect(int position) {
hashMapSelected.put(position, true);
for (int i = 0; i < hashMapSelected.size(); i++) {
if (i != position)
hashMapSelected.put(i, false);
}
}
終於我的網格視圖setOnItemClickListener
adapter.makeAllUnselect(position);
adapter.notifyDataSetChanged();
最終我的適配器看起來像這樣
public class DataAdapter extends BaseAdapter {
private Context mContext;
private TypedArray images;
private List<String> headers;
public HashMap<Integer, Boolean> hashMapSelected;
public DataAdapter(Context context, TypedArray images, List<String> headers) {
this.mContext = context;
this.images = images;
this.headers = headers;
hashMapSelected = new HashMap<>();
for (int i = 0; i < headers.size(); i++) {
hashMapSelected.put(i, false);
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return images.length();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public void makeAllUnselect(int position) {
hashMapSelected.put(position, true);
for (int i = 0; i < hashMapSelected.size(); i++) {
if (i != position)
hashMapSelected.put(i, false);
}
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.groupby_grid_item, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else
viewHolder = (ViewHolder) convertView.getTag();
viewHolder.textView.setText(headers.get(position));
viewHolder.imageView.setImageResource(images.getResourceId(position, -1));
if (hashMapSelected.get(position) == true) {
viewHolder.imageView.setColorFilter(ContextCompat.getColor(mContext, R.color.violet));
viewHolder.textView.setTextColor(ContextCompat.getColor(mContext, R.color.violet));
} else {
viewHolder.imageView.setColorFilter(ContextCompat.getColor(mContext, R.color.gridsepration));
viewHolder.textView.setTextColor(ContextCompat.getColor(mContext, R.color.gridsepration));
}
return convertView;
}
private class ViewHolder {
TextView textView;
ImageView imageView;
public ViewHolder(View view) {
textView = (TextView) view.findViewById(R.id.groupbyHeader);
imageView = (ImageView) view.findViewById(R.id.groupbyImage);
}
}
}
而且我的GridView OnItemClick
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
selectedGroupBy = getHeaders().get(position);
adapter.makeAllUnselect(position);
adapter.notifyDataSetChanged();
}
});
發佈您的代碼.. – rafsanahmad007