上下文菜单不适用于行布局中的开关
Context Menu doesn't work with there is Switch in row layout
我的片段中有一个带有自定义适配器(从 ArrayAdapter 扩展而来)的 ListView。
list.xml:
...
<ListView android:id="@android:id/list" android:layout_width="match_parent"
android:layout_height="match_parent" />
...
rowlayout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:id="@+id/alarm_list_rowlayout_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginLeft="20dp"
android:layout_marginTop="15dp">
</TextView>
<Switch
android:id="@+id/alarm_list_rowlayout_switch_active"
android:layout_width="0dp"
android:layout_height="20dp"
android:showText="false"
android:layout_marginRight="20dp"
android:layout_marginTop="15dp"
android:layout_weight="1">
</Switch>
</LinearLayout>
fragment.java:
...
mListView = (AbsListView) view.findViewById(android.R.id.list); //my list
mListView.setAdapter(adapter);
mListView.setOnItemClickListener(this);
registerForContextMenu(mListView); //set up context menu
...
如果行布局中没有 switch
,则一切正常。但是,如果它添加了,则单击行时什么也没有发生。
如何解决这个问题,同时使用上下文菜单和切换?
添加
android:descendantFocusability="blocksDescendants"
到您的 LinearLayout。
添加
android:textIsSelectable="false"
到您的 TextView
添加
android:focusable="false"
到您的 Switch
我的片段中有一个带有自定义适配器(从 ArrayAdapter 扩展而来)的 ListView。 list.xml:
...
<ListView android:id="@android:id/list" android:layout_width="match_parent"
android:layout_height="match_parent" />
...
rowlayout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:id="@+id/alarm_list_rowlayout_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginLeft="20dp"
android:layout_marginTop="15dp">
</TextView>
<Switch
android:id="@+id/alarm_list_rowlayout_switch_active"
android:layout_width="0dp"
android:layout_height="20dp"
android:showText="false"
android:layout_marginRight="20dp"
android:layout_marginTop="15dp"
android:layout_weight="1">
</Switch>
</LinearLayout>
fragment.java:
...
mListView = (AbsListView) view.findViewById(android.R.id.list); //my list
mListView.setAdapter(adapter);
mListView.setOnItemClickListener(this);
registerForContextMenu(mListView); //set up context menu
...
如果行布局中没有 switch
,则一切正常。但是,如果它添加了,则单击行时什么也没有发生。
如何解决这个问题,同时使用上下文菜单和切换?
添加
android:descendantFocusability="blocksDescendants"
到您的 LinearLayout。
添加
android:textIsSelectable="false"
到您的 TextView
添加
android:focusable="false"
到您的 Switch