我有网格视图,我正在显示一些使用Picasso库下载的图片。我的目标是:当我点击某些图片时,其他人应该获得灰度。但我不知道如何实现这一目标。
我的GridViewAdapter:
public class GridViewAvatarAdapter extends ArrayAdapter<AvatarItem> {
private Context mContext;
private int layoutResourceId;
private ArrayList<AvatarItem> mGridData = new ArrayList<AvatarItem>();
public GridViewAvatarAdapter(Context mContext, int layoutResourceId, ArrayList<AvatarItem> mGridData) {
super(mContext, layoutResourceId, mGridData);
this.layoutResourceId = layoutResourceId;
this.mContext = mContext;
this.mGridData = mGridData;
}
public void setGridData(ArrayList<AvatarItem> mGridData) {
this.mGridData = mGridData;
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
final ViewHolder holder;
if (row == null) {
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.imageView = (ImageView) row.findViewById(R.id.grid_item_avatar_image);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
AvatarItem item = mGridData.get(position);
Picasso.with(mContext).load(item.getAvatarURL()).transform(new CircleTransform()).into(holder.imageView);
return row;
}
static class ViewHolder {
ImageView imageView;
}
}
而且我知道我应该使用:
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//Get item at position
AvatarItem item = (AvatarItem) parent.getItemAtPosition(position);
}
});
在我的GridView片段中。我也知道如何制作imageView灰度,但我不知道在mGridView.setOnItemClickListener()(在gridView片段中)更改ImageView
答案 0 :(得分:2)
如果你试图设置eveything但你点击的那个我会做这样的事情
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//Get item at position
gridViewadapter.itemClicked(position);
}
});
然后你的适配器就是这个
public class GridViewAvatarAdapter extends ArrayAdapter<AvatarItem> {
private Context mContext;
private int layoutResourceId;
private int currentSelectedItem = -1;
private ArrayList<AvatarItem> mGridData = new ArrayList<AvatarItem>();
public GridViewAvatarAdapter(Context mContext, int layoutResourceId, ArrayList<AvatarItem> mGridData) {
super(mContext, layoutResourceId, mGridData);
this.layoutResourceId = layoutResourceId;
this.mContext = mContext;
this.mGridData = mGridData;
}
public void itemClicked(int position)
{
currentSelectedItem = position;
notifyDataSetChanged();
}
public void setGridData(ArrayList<AvatarItem> mGridData) {
this.mGridData = mGridData;
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
final ViewHolder holder;
if (row == null) {
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.imageView = (ImageView) row.findViewById(R.id.grid_item_avatar_image);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
AvatarItem item = mGridData.get(position);
Picasso.with(mContext).load(item.getAvatarURL()).transform(new CircleTransform()).into(holder.imageView);
if(currentSelectedItem != -1 && currentSelectedItem != position)
{
//MAKE IMAGE GRAYSCALE HERE
}
else
{
//MAKE IMAGE COLOR HERE
}
return row;
}
static class ViewHolder {
ImageView imageView;
}
}
然后,当你想要将它们全部重置为彩色时,你可以用item调用-1来
答案 1 :(得分:0)
该逻辑没有融入适配器的另一个选择是:
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
int numberOfViews = parent.getCount();
for(int i = 0; i < numberOfViews; i++) {
//If i is not the image you clicked
if(i != position) {
AvatarItem item = (AvatarItem) parent.getItemAtPosition(i);
//Run whatever function that converts to greyscale
item.toGreyScale();
}
}
//Get item at position
AvatarItem item = (AvatarItem) parent.getItemAtPosition(position);
//Run whatever function you want to on the clicked item
item.doSomething();
}
});