我的gridview自定义项是一个图像按钮。我想在单击图像按钮时更改图像按钮图像。您可以在下面看到我的自定义适配器类getView方法。但是图像按钮图像不会改变。
ImageButton btn1;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.myItem, null);
btn1 = (ImageButton) view.findViewById(R.id.btn1);
}
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
btn1.setImageResource(R.id.image1);
}
});
return view;
}
答案 0 :(得分:0)
请尝试这种方式,希望这有助于您解决问题。
public class CustomGridAdapter extends BaseAdapter {
private Context context;
private ArrayList<HashMap<String,Integer>> imagesList;
public CustomGridAdapter(Context context,ArrayList<HashMap<String,Integer>> imagesLis) {
this.context =context;
this.imagesList =imagesLis;
}
@Override
public int getCount() {
return imagesList.size();
}
@Override
public Object getItem(int position) {
return imagesList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.myItem, null);
holder.btn1 = (TextView) convertView.findViewById(R.id.btn1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.btn1.setImageResource(imagesList.get(position).get("normalImage"));
holder.btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((ImageButton)v).setImageResource(imagesList.get(position).get("selectedImage"));
}
});
return convertView;
}
static class ViewHolder {
ImageButton btn1;
}
}