Java 项目单击侦听器上的 GridView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18918633/
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
GridView on item click listener
提问by user2796963
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(ScenesActivity.this, position + "#Selected",
Toast.LENGTH_SHORT).show();
final Intent intent = new Intent(getApplicationContext(), ScenesDetailActivity.class);
intent.putExtra(TestConstants.SELCTED_SCENE_KEY, position);
startActivity(intent);
}
});
TestConstants.java
测试常量.java
public interface TestConstants {
String SELCTED_SCENE_KEY="SELCTED_SCENE"; }
I want to get ScenesDetailActivity after click the slected scene key. It seems that the above code doesn't work as expected. Can someone help me to slove it? Thanks a lot! Simonides
单击选定的场景键后,我想获取 ScenesDetailActivity。上面的代码似乎没有按预期工作。有人可以帮我解决它吗?非常感谢!西蒙尼德斯
回答by Mostafa Wasat
does the gridView have a custom layout ?
gridView 有自定义布局吗?
if yes, go to the custom layout xml file and change these attributes
如果是,请转到自定义布局 xml 文件并更改这些属性
android:clickable="false"
android:focusable="false"
回答by phreakhead
Here's the cleanest way to do it: call performItemClick() on the GridView from within each button's click listener in your Adapter. That way you can still use the GridView's onItemClickListener like normal.
这是最简洁的方法:从适配器中每个按钮的单击侦听器中调用 GridView 上的 performItemClick()。这样你仍然可以像往常一样使用 GridView 的 onItemClickListener。
// in your ListAdapter
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
// set btn to be your item's view
...
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((GridView) parent).performItemClick(v, position, 0);
}
});
}