'Why is ListPopupWindow OnItemClickListener not called?
For some reason I can't get this popup to react to clicks, onItemClick is not called.
String[] actions = new String[]{"1", "2", "3"};
ArrayAdapter adapter = new ArrayAdapter<String>(getContext(), R.layout.layout_popup_list_item, actions);
popupWindow = new ListPopupWindow(getContext());
popupWindow.setAnchorView(btnConferenceActions);
popupWindow.setAdapter(adapter);
popupWindow.setWidth(measureContentWidth(adapter));
popupWindow.setDropDownGravity(Gravity.END);
popupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
hideConferenceActionsList();
switch (position){
case 0:
Log.i("TAG", "Item 0");
break;
case 1:
Log.i("TAG", "Item 1");
break;
case 2:
Log.i("TAG", "Item 2");
break;
}
});
popupWindow.show();
This popup is shown inside custom view, and it shows fine except it's not reating to clicks
Solution 1:[1]
if you move popupWindow.show();
up a couple lines into the setOnItemClickListener()
method, does that correct it?
String[] actions = new String[]{"1", "2", "3"};
ArrayAdapter adapter = new ArrayAdapter<String>(getContext(), R.layout.layout_popup_list_item, actions);
popupWindow = new ListPopupWindow(getContext());
popupWindow.setAnchorView(btnConferenceActions);
popupWindow.setAdapter(adapter);
popupWindow.setWidth(measureContentWidth(adapter));
popupWindow.setDropDownGravity(Gravity.END);
popupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
hideConferenceActionsList();
switch (position){
case 0:
Log.i("TAG", "Item 0");
break;
case 1:
Log.i("TAG", "Item 1");
break;
case 2:
Log.i("TAG", "Item 2");
break;
popupWindow.show(); /* If it is placed here */
}
});
Solution 2:[2]
I had a similar problem and solved by modifying my popup list item layout as follows.
Before(Not working):
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/Text.Regular"
android:maxLines="1"
android:ellipsize="end"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"
/>
After:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/Text.Regular"
android:maxLines="1"
android:ellipsize="end"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
/>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Bumaza |