我已经设置了一个上下文菜单,以在短按时显示我在 ListView 中单击的项目
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Pick an action");
theView = v;
TextView desc = (TextView) theView.findViewById(R.id.class_description);
String description = (String) desc.getText();
TextView id = (TextView) v.findViewById(R.id.class_id);
Integer ID = Integer.valueOf(String.valueOf(id.getText()));
menu.add(1, ID, 0, getString(R.string.add_to_calender));
menu.add(2, ID, 0, getString(R.string.subscribe_alterations));
assert description != null;
if (!description.equals("")) {
menu.add(3, ID, 0, getString(R.string.view_description));
}
menu.add(4, ID, 0, getString(R.string.view_alterations));
}
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getSherlockActivity(), "Clicked", Toast.LENGTH_SHORT).show();
registerForContextMenu(view);
view.setLongClickable(false);
getSherlockActivity().openContextMenu(view);
}
});
在我的列表视图适配器中,我有这行来切换按钮的可见性
holder.alter.setVisibility(View.VISIBLE);
if ((position % 2) == 0) {
holder.alter.setVisibility(View.INVISIBLE);
}
这是按钮
<Button
android:layout_width="20dp"
android:layout_height="wrap_content"
android:background="@drawable/alterations_btn"
android:textColor="@color/white"
android:layout_alignParentLeft="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold"
android:layout_marginRight="@dimen/padding_small"
android:id="@+id/is_alt"
android:layout_centerInParent="true"
android:text="@string/exclamation_point"/>
当按钮不可见时,上下文菜单有效,但当它可见时则无效。
谁能明白为什么?