I have images displayed in a GridView as in this tutorial. I want to be able to click on a single image and do other events and I need to know what image was clicked.
我将在GridView中显示图像,如本教程中所示。我想要能够点击单个图像并执行其他事件,我需要知道点击了什么图像。
Do I have to add imageView.onKeyDown(keyCode, event) in the ImageAdapter class? Here is the code as it currently exists:
我必须添加imageView。在ImageAdapter类中onKeyDown(keyCode, event) ?以下是目前存在的代码:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
//does this need imageView.onKeyDown(keyCode, event)?
}
else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
How will it indicate what image was clicked? How do I create the proper handler?
它将如何指示被单击的图像?如何创建适当的处理程序?
3 个解决方案
#1
24
For a GridView you can use the setOnItemClickListener method to have an OnItemClickListener listener. That listener will give you a method that you must override with the signature
对于GridView,可以使用setOnItemClickListener方法获得OnItemClickListener侦听器。该侦听器将向您提供一个方法,您必须使用签名覆盖该方法
onItemClick(AdapterView<?> parent, View v, int position, long id)
where you get the position of the item in the grid that was clicked and the View that is inside the cell of the grid. Is that what you need?
您可以在网格中获取被单击的项的位置以及网格单元格内的视图。这是你需要的吗?
#2
6
I was able to get the position of the clicked image by making the position final and adding an onClick listener to the imageView. This logs the position of the image that was clicked.
我可以通过设置位置final并在imageView中添加onClick侦听器来获得单击图像的位置。它记录被单击的图像的位置。
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("onClick","position ["+position+"]");
}
});
}
else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
#3
6
i tried the above mentioned method getView(final int position . . .) realized that the position got "reset" after 28 items and the position when back to 0 after the 28th item in the gridview.
我尝试了上面提到的getView方法(最终int position . . .),发现在28个项目之后,位置得到了“reset”,在gridview中第28个项目之后,位置又回到了0。
i suspected the final keyword is creating problem and after removing it i was able to get the positions as expected.
我怀疑最后的关键字是创建问题,在删除它之后,我能够得到预期的位置。
Below is a sample code with the click event being called in the activity that is showcasing the gridview.
下面是一个示例代码,在显示gridview的活动中调用单击事件。
public class MainActivity extends Activity {
ArrayList<Integer> item_ids = new ArrayList<Integer>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
item_ids = //get your item ids method
GridView gridview = (GridView) findViewById(R.id.grid_featured);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(itemClickListener);
footer = (Footer)findViewById(R.id.layoutFooter);
footer.setActivity(this);
}
private OnItemClickListener itemClickListener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Log.d(TAG,"Position Clicked ["+position+"] with item id ["+item_ids.get(position)+"]");
}
};
}
}
#1
24
For a GridView you can use the setOnItemClickListener method to have an OnItemClickListener listener. That listener will give you a method that you must override with the signature
对于GridView,可以使用setOnItemClickListener方法获得OnItemClickListener侦听器。该侦听器将向您提供一个方法,您必须使用签名覆盖该方法
onItemClick(AdapterView<?> parent, View v, int position, long id)
where you get the position of the item in the grid that was clicked and the View that is inside the cell of the grid. Is that what you need?
您可以在网格中获取被单击的项的位置以及网格单元格内的视图。这是你需要的吗?
#2
6
I was able to get the position of the clicked image by making the position final and adding an onClick listener to the imageView. This logs the position of the image that was clicked.
我可以通过设置位置final并在imageView中添加onClick侦听器来获得单击图像的位置。它记录被单击的图像的位置。
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("onClick","position ["+position+"]");
}
});
}
else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
#3
6
i tried the above mentioned method getView(final int position . . .) realized that the position got "reset" after 28 items and the position when back to 0 after the 28th item in the gridview.
我尝试了上面提到的getView方法(最终int position . . .),发现在28个项目之后,位置得到了“reset”,在gridview中第28个项目之后,位置又回到了0。
i suspected the final keyword is creating problem and after removing it i was able to get the positions as expected.
我怀疑最后的关键字是创建问题,在删除它之后,我能够得到预期的位置。
Below is a sample code with the click event being called in the activity that is showcasing the gridview.
下面是一个示例代码,在显示gridview的活动中调用单击事件。
public class MainActivity extends Activity {
ArrayList<Integer> item_ids = new ArrayList<Integer>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
item_ids = //get your item ids method
GridView gridview = (GridView) findViewById(R.id.grid_featured);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(itemClickListener);
footer = (Footer)findViewById(R.id.layoutFooter);
footer.setActivity(this);
}
private OnItemClickListener itemClickListener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Log.d(TAG,"Position Clicked ["+position+"] with item id ["+item_ids.get(position)+"]");
}
};
}
}