Java 在 Android 的 GridView 中创建可点击的图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/738817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create a clickable image in a GridView in Android
提问by Tai Squared
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:
我必须在 ImageAdapter 类中添加 imageView.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?
它将如何指示点击了什么图像?如何创建合适的处理程序?
采纳答案by Alejandro Mezcua
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?
您可以在其中获得单击的网格中项目的位置以及网格单元格内的视图。那是你需要的吗?
回答by Tai Squared
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.
通过使位置成为最终位置并向 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;
}
回答by Jianhong
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(final int position . .) 意识到位置在 28 个项目后“重置”,而在 gridview 中的第 28 个项目后位置回到 0。
i suspected the final keyword is creating problem and after removing it i was able to get the positions as expected.
我怀疑 final 关键字造成了问题,删除它后我能够按预期获得职位。
Below is a sample code with the click event being called in the activity that is showcasing the gridview.
下面是在展示 gridview 的活动中调用 click 事件的示例代码。
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)+"]");
}
};
}
}