Android:带有可点击网格项和嵌套视图(按钮、复选框)的网格视图 [英] Android: Grid view with clickable grid items and nested views (buttons, checkboxes)
问题描述
基本上,我想实现一个 GridView
,其中项目本身是可点击的,但在 GridView
中是可点击的 Button
s 和 <代码>复选框es.
Basically, I would like to implement a GridView
wherein the items themselves are clickable, but within the GridView
are clickable Button
s and Checkbox
es.
这是一个示例布局:
Gridview
-----------------------------------------
|[ImageView] |[ImageView] |
|[TextView] |[TextView] |
|[Button][Checkbox] |[Button][Checkbox] |
-----------------------------------------
|[ImageView] |[ImageView] |
|[TextView] |[TextView] |
|[Button][Checkbox] |[Button][Checkbox] |
-----------------------------------------
基本上,我想要做的是,当用户单击CheckBox
时,现在可以从GridView
中选择多个项目.当用户单击Button
时,会显示一个Popup
.当用户点击其他任何地方时,就会启动一个新的 Activity
.这样做的全部意义不是长按以显示上下文菜单,而是想要一个按钮来代替它.
Basically, what i want to do is, when the user clicks the CheckBox
, multiple items can now be selected from the GridView
. When the users clicks the Button
, A Popup
is shown. When the user clicks anywhere else, a new Activity
is started. The whole point of this is instead of long-pressing to show the context menu, I would like a button to take its place.
关于如何处理这种情况有什么建议吗?在我当前的设置中,如果我在适配器的 GetView()
方法中添加 Button
,则只有 Button
是可点击的.整个 GridView
项目是不可点击的.当我删除 Button
时,GridView
项可以再次点击.似乎只有整个 GridView
Item 或 Button
是可点击的(响应 OnClickListener()
.有没有办法让它们都可点击?
Any suggestions on how I can approach this scenario? In my current setup, if I add the Button
within the adapter's GetView()
method, only the Button
is clickable. The whole GridView
item is not clickable. When I remove the Button
, the GridView
item is clickable again. It seems that it's only the whole GridView
Item or the Button
is clickable (responds to OnClickListener()
. Is there a way to make them both clickable?
推荐答案
这篇文章很旧,但仅供参考/googlers,我有以下解决方案:
This post is old, but just for reference/googlers, I have the following solution:
您只需要在 imageAdapter 的 getView 中编写按钮的所有事件,复选框.然后在 gridview 标签下的 layout.xml 文件中添加以下几行:
You need to write all the events for buttons, checkboxes in getView of the imageAdapter only. Then in your layout.xml file under the gridview tag add these lines:
android:clickable="true"
android:descendantFocusability="beforeDescendants"
并将这些行添加到您的按钮和复选框中:
and add these lines to your button and checkboxes:
android:focusable="false"
android:focusableInTouchMode="false"
如果您希望在单击/触摸 gridview 项目的任何其他区域时启动其他活动,则需要使用(标准代码)/(您自己的匹配实现):
And if you want some other activity to start if any other area of the gridview item is clicked/touched you would need to use the (standard code)/(your own matching implementation):
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();
}
干杯,});
这篇关于Android:带有可点击网格项和嵌套视图(按钮、复选框)的网格视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!