我有 [TextView TextView ImageButton] 在 android 上的列表视图。我使用 ArrayAdapter 实现列表视图。根据我的要求,我需要列表视图的单次和长按事件,因此我使用registerForContextMenu(getListView())进行长按,单次按下我使用onListItemClick。这工作正常。之后,我还需要在列表视图中为 imageButton 提供 onClick 事件,并为 imageButton 实现 onclick。
现在我的问题是单按事件和长按事件在 listView 上为 imageButton 实现 onClick 侦听器后不起作用。但 ImageButton onClick 侦听器工作正常。
我将属性android:focusable="false"添加到 imageButton ,但这对我没有帮助。
如何使用 ImageButton 的 onclick 侦听器获取单次和长按事件?请帮助我。
组.xml:
......
<ListView
android:id="@id/android:list"
android:focusable="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
.....
group_row.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
......
android:focusable="false" />
<ImageButton
android:id="@+id/group_row_gadd"
.....
android:focusable="false"
android:background="@drawable/add" />
<TextView
android:id="@+id/group_row_grow"
....
android:focusable="false" />
</RelativeLayout>
我的数组适配器类:
public class GroupAdapter extends ArrayAdapter<String> implements OnClickListener{
....
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.group_row, parent, false);
name = (TextView) rowView.findViewById(R.id.group_row_gname);
number = (TextView) rowView.findViewById(R.id.group_row_grow);
add = (ImageButton) rowView.findViewById(R.id.group_row_gadd);
add.setTag(position);
add.setOnClickListener(this);
return rowView;
}
public void onClick(View v) {
Log.e("onclick", "onclick");
Integer position = (Integer) v.getTag();
switch (v.getId()) {
case R.id.group_row_gadd:
break;
}
}
}
主类:
public class Group extends ListActivity{
.....
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.group);
registerForContextMenu(getListView());
.....
}// OnCreate End
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
.....
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.group_edit_remove, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
.....
return false;
}
}