我有一个使用此自定义适配器的gridView:
package com.example.awesomefilebuilderwidget;
IMPORTS
public class GridViewAdapter extends BaseAdapter {
private Context Context;
// Keep all Images in array list
public ArrayList<Integer> drawables = new ArrayList<Integer>();
CheckBox mCheckBox=null;
// Constructor
public GridViewAdapter(Context c){
Context = c;
Log.d("GridViewAdapter", "Constructor is set");
drawables.add(R.drawable.add_button);
Log.d("GridViewAdapter", "add_button added");
drawables.add(R.drawable.pattern1);
Log.d("GridViewAdapter", "pattern1 added");
drawables.add(R.drawable.pattern2);
Log.d("GridViewAdapter", "pattern2 added");
drawables.add(R.drawable.trashcan);
Log.d("GridViewAdapter", "trashcan added");
drawables.add(R.drawable.ic_launcher);
Log.d("GridViewAdapter", "ic_launcher added");
}
public void setCheckBox(CheckBox checkbox){
mCheckBox=checkbox;
}
@Override
// How many items are in the data set represented by this Adapter
public int getCount() {
return drawables.size();
}
@Override
// Get the data item associated with the specified position in the
// data set
public Object getItem(int position) {
return drawables.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Try to reuse the views
ImageView view = (ImageView) convertView;
boolean checked = (mCheckBox==null)?false:(((CheckBox) mCheckBox).isChecked());
// if convert view is null then create a new instance else reuse it
if (view == null) {
view = new ImageView(Context);
Log.d("GridViewAdapter", "new imageView added");
view.setId(R.id.iconImageView_id);
}
view.setImageResource(drawables.get(position));
view.setScaleType(ImageView.ScaleType.CENTER_CROP);
view.setLayoutParams(new android.widget.GridView.LayoutParams(70, 70));
view.setTag(String.valueOf(position));
return view;
}
}
我希望能够将onItemClick侦听器设置为构造函数中创建的其中一个图像,特别是add_button。我从环顾四周了解到我需要在我的适配器的getView()方法中执行此操作,但我不太确定如何操作。我已经尝试创建一个单独的imageView,然后在gridView之外添加add_button
图像,但这只会引起我的探测,所以我决定在实际的gridView中进行。我怎么能这样做?
我的活动中也有这个:
android.widget.GridView gridView = (android.widget.GridView) findViewById(R.id.GRIDVIEW1);
// Instance of Adapter Class
GridViewAdapter mGridViewAdapter= new GridViewAdapter(this);
gridView.setAdapter(mGridViewAdapter);
CheckBox mCheckBox = (CheckBox) findViewById(R.id.addCheckbox);
mGridViewAdapter.setCheckBox(mCheckBox);
gridView.setOnItemClickListener(this);
如果我这样做,我该如何设置它以便OnItemClickListener只对add_button
imageView执行某个操作?
答案 0 :(得分:0)
在OnItemClickListener中,放置一个if语句来检查单击的项是否为add_button。如果是,请采取行动。