Android ListView的OnItemClickListener在不可获取焦点的PopupWindow上无法工作

3
在我的Android应用程序中,我有一个EditText。当EditText获得焦点时,会显示一个PopupWindow,其中包含一个ListView。用户可以在PopupWindow显示的同时继续在EditText中输入,同时他/她应该能够单击ListView上的项目以关闭PopupWindow。(根据单击的ListView项目,除了关闭PopupWindow之外,还应该发生其他事情,但这在这里不重要。)
问题是,“继续输入”部分有效,但“单击项目”部分无效,因为ListView的OnItemClickListener从未被调用过,我无法弄清楚原因。
这是我的activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ddd"
    android:focusableInTouchMode="true"
    tools:context="com.example.popuptest3.MainActivity" >

    <EditText
        android:id="@+id/edtPhone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="phone" />

</RelativeLayout>

我的 popup_window.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff" >

    <ListView
        android:id="@+id/lstItems"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="60dp" />

    <Button
        android:id="@+id/btnDismiss"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Dismiss" />

</RelativeLayout>

My list_item.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp" >

    <TextView
        android:id="@+id/txtItem"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:text="Item Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

在我的MainActivity类中,我有以下的准备代码:
private PopupWindow popup = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    prepareControls();
}

private void prepareControls() {
    final EditText edtPhone = (EditText)findViewById(R.id.edtPhone);
    edtPhone.setOnFocusChangeListener(new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus)
                openPopup(edtPhone);
            else
                closePopup();
        }
    });
}

也就是说,当edtPhone获得焦点时,我打开PopupWindow,当edtPhone失去焦点时,我关闭PopupWindow。然后我有:

private void openPopup(View parent) {
    closePopup();

    // Inflate the view of the PopupWindow.
    LayoutInflater layoutInflater = LayoutInflater.from(this);
    View view = layoutInflater.inflate(R.layout.popup_window,
            new LinearLayout(this), false);
    final Activity activity = this;

    // Prepare the ListView.
    ListView lstItems = (ListView)view.findViewById(R.id.lstItems);
    lstItems.setAdapter(new ListAdapter(this));
    lstItems.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            hideKeyboardAndClearFocus(activity);
        }
    });

    // Prepare the Button.
    Button btnDismiss = (Button)view.findViewById(R.id.btnDismiss);
    btnDismiss.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            hideKeyboardAndClearFocus(activity);
        }
    });

    // Create and show the PopupWindow.
    popup = new PopupWindow(view, 400, 300, false);
    popup.showAtLocation(parent, Gravity.LEFT | Gravity.TOP, 0, 60);
}

btnDismiss 按钮不是我的计划的一部分,我在这里添加它仅用于演示目的。最后我有:

private void closePopup() {
    if (popup != null) {
        popup.dismiss();
        popup = null;
    }
}

private static void hideKeyboardAndClearFocus(Activity activity) {
    InputMethodManager manager = (InputMethodManager)activity.
            getSystemService(Activity.INPUT_METHOD_SERVICE);
    manager.hideSoftInputFromWindow(
            activity.getCurrentFocus().getWindowToken(), 0);
    activity.getWindow().getDecorView().clearFocus();
}

我认为这不需要解释,ListAdapter 类相当标准,因此我省略了它。

现在,当我点击 edtPhone 时,它会获得焦点,软键盘会弹出,PopupWindow 会打开。我可以在 edtPhone 中输入一些内容,当我点击 btnDismiss 时,edtPhone 失去焦点,软键盘关闭,PopupWindow 关闭,所有操作都正常。但是,如果我点击 lstItems 上的一个项目而不是 btnDismiss,则什么也不会发生。使用 Log 我可以看到 OnItemClickListener 没有被调用。有趣的是,ListView 可以滚动,但无法点击。

我知道这篇文章,但那个解决方案对我的目的没有用。如果我更改这一行:

popup = new PopupWindow(view, 400, 300, false);

to the following:

popup = new PopupWindow(view, 400, 300, true);

PopupWindow 变得可聚焦,ListView 可点击,很好,但是我就不能在 edtPhone 中继续输入了。实际上,如果在该活动中还有其他控件,它们也都无法访问,就像 PopupWindow 是“模态”的一样。

我尝试了 android:focusable="false",但仍没有成功。而且我不需要自动完成,所以 AutoCompleteTextView 不是我需要的。

那么为什么非可聚焦的 PopupWindow 上的 OnItemClickListener 没有被调用呢?我做错了什么吗?

2个回答

1
我通过在适配器的getView方法的参数convertview上设置OnClickListener解决了这个问题。然后调用listview的onItemClickListener的onItemClick方法。 无需使popupWindow具有焦点。实际上,只需使用ListPopupWindow更容易,因为我们只需要在getView()适配器方法中添加几行代码即可。
ListPopupWindow popupWindow = new ListPopupWindow(mContext);

    MentionsAdapter adapter = new MentionsAdapter(mContext, suggestions);
    popupWindow.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String username = mSuggestions.get(position);
            String mentionCompletion = username.substring(mCurrentQuery.length()).concat(" ");
            mEditor.getText().insert(mEditor.getSelectionEnd(), mentionCompletion);
            hideSuggestions();
        }
    });
   popupWindow.setAdapter(adapter);

MentionsAdapter getView方法

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(,R.layout.item_user,parent,false);

        }
        // this is the key point of workaround
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               popupWindow.getListView().getOnItemClickListener()
                 .onItemClick(listView, v, position, getItemId(position));
            }
        });
        return convertView;
    }

1
将弹出窗口设置为不可聚焦。
popupwindow.setFocusable(false);
popupwindow.setTouchable(true);
popupwindow.setOutsideTouchable(true);

扩展ListView并覆盖以下方法。
@Override
public boolean hasFocus() {
    return true;
}

@Override
public boolean isFocused() {
    return true;
}

@Override
public boolean hasWindowFocus() {
    return true;
}

这是AutoCompleteTextView实现建议弹出窗口的方式。 如果您需要使用DPAD键或其他导航键选择列表项,则需要将键事件从EditText传递到ListView。

这是相当久以前的事情了,而我已经继续前进了,所以恐怕我现在无法验证你的答案。但我理解你的建议,我认为你是正确的,所以我会接受它。谢谢! - ycsun
是的,这正好解决了我遇到的同样问题,即在弹出窗口内部的ListView中onItemLongClickListener没有被调用。 - D. Pappas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接