【问题标题】:ListView in a Popup WIndow弹出窗口中的 ListView
【发布时间】:2012-07-13 19:11:59
【问题描述】:

我已经搜索了几个小时,但找不到任何可以解决我的问题的方法。我有一个使用弹出窗口的应用程序。在该弹出窗口中,我想在手机上显示联系人列表,这部分我正在工作。我创建了一个扩展 ArrayAdapter 的适配器,并让它在列表中填充电话联系人。

但现在我想与这些联系人互动,即。单击一个并向用户显示联系人的信息。我将 OnItemClickListener() 添加到列表中,但它从未被调用。我尝试使单元格不可聚焦,我尝试将 onClickListeners 添加到每个项目,但这并没有突出显示单元格。我被卡住了。

下面是我用来设置 ListView 并将 onItemClick 侦听器添加到列表视图的代码。

ListView contactList = (ListView) contentView.findViewById(R.id.contacts_list);
  ContactsProcessing processContacts = new ContactsProcessing();
  contactList.setAdapter(processContacts.getListAdapter());
  contactList.setItemsCanFocus(false);
  contactList.setFocusableInTouchMode(false);
  contactList.setOnItemClickListener(new OnItemClickListener() {

     @Override
     public void onItemClick(AdapterView<?> l, View v, int position, long id) {
        // TODO Auto-generated method stub
        // super.onListItemClick(l, v, position, id);
        Toast toast = new Toast(MainActivity.this);
        toast.setText("Item Clicked");
        toast.show();
        m_ButtonFeedback.hapticFeedback();
        Log.d("MainActivity", "Item Clicked");


     }
  });

在同一个弹出窗口中,我还有一个退出按钮。它按我预期的方式工作。

ImageButton exit = (ImageButton) contentView.findViewById(R.id.contacts_close_button);
  exit.setOnClickListener(new OnClickListener() {

     @Override
     public void onClick(View _v) {
        window.dismiss();

     }
  });

这是我的单元格的 XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" 
android:background="@android:color/transparent"
android:id="@+id/contact_cell_root" >

<TextView
    android:id="@+id/contact_cell_headerText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:paddingTop="2dp"
    android:paddingBottom="2dp"
    android:paddingLeft="15dp"
    android:scrollbars="none"
    android:textColor="#29abe2"
    android:textSize="18sp"
    android:textStyle="bold"
    android:visibility="gone" />

<View
    android:id="@+id/contact_cell_header_divider" 
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:background="#29abe2"
    android:visibility="gone"/>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:background="@android:color/transparent"
    android:gravity="center"
    android:paddingRight="10dp"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/contact_cell_user_image"
        android:adjustViewBounds="true"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:scaleType="centerInside"
        android:src="@drawable/contact_default"
        android:focusable="false" />

    <LinearLayout 
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:orientation="vertical"
        android:paddingBottom="5dp"
        android:paddingTop="5dp" >

        <TextView
            android:id="@+id/contact_cell_titleText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:ellipsize="end"
            android:singleLine="true"
            android:text="John Smith"

            android:textStyle="bold"
            android:textSize="16sp" 
            android:textColor="@android:color/black"/>

        <TextView
            android:id="@+id/contact_cell_companyName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/titleText"
            android:ellipsize="end"
            android:text="Company Name"

            android:textSize="14sp"
            android:textColor="@android:color/darker_gray" />
    </LinearLayout>
    <ImageView
        android:id="@+id/contact_cell_imageView"    
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:gravity="center_vertical"
        android:src="@drawable/ic_launcher"
        android:scaleType="centerInside"
        android:focusable="false"
         />
</LinearLayout>
<View
    android:id="@+id/contact_cell_divider" 
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:background="#e7e7e7"
    />

</LinearLayout>

【问题讨论】:

  • 要在其中添加列表视图的 PopupWindow 编码在哪里?我在 PopupWindow 中添加 ListView 时遇到问题

标签: android listview onclick popupwindow


【解决方案1】:

事实证明,弹出窗口不是可聚焦的。简单的解决方法是告诉弹出窗口可聚焦。

PopupWindow window;
window = new PopupWindow(context);
window.setFocusable(true);

一旦获得焦点,视图中的所有组件都处于活动状态。

【讨论】:

  • 当您这样做时,您会将 onKey 事件释放到底层 Activity。这意味着您无法捕获和使用它们。
  • 谢谢斯科特,但在我的情况下不起作用!为此,我创建了一个自定义适配器,并在 getView() 中添加了视图中的 onCLickListener()。
【解决方案2】:

看起来我的解决方案是 Scott 已经尝试过的解决方案之一,但如果有人仍在寻找某种实现,我将在此处发布。

所以我遇到了同样的问题,但在我的情况下,setFocusble(false) 是必需的(在我的情况下,使用ListPopupWindow 不是解决方案,因为项目中的很多东西已经使用了基础PopupWindow 的功能,包括扩展)。

如果有人在相同的情况下,有一种基于错误讨论here(post #9)的解决方法

主要思想是ListView的层次结构仍然接收触摸事件,因此我们可以手动触发onItemClick()。

但是,这种方法与真正的ListView 的触摸处理并非 100% 相同(就像点击一行时没有选择发光一样),这对我来说目前做得很好。

如果有人对此问题有更精确的解决方案,请分享。

所以,这里是完整的Adapter 的代码,可以在PopupWindow 中与ListView 一起使用,即setFocusable(false):

私有类 CustomAdapter 扩展 ArrayAdapter {

private LayoutInflater mInflater;
private ListView mOwningListView;

public CustomAdapter(Context context, List<String> objects, ListView listView) {
    super(context, android.R.layout.simple_list_item_1, objects);
    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mOwningListView = listView;
}


@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.font_pick_row, null);
    }
    // this is the key point of workaround
    convertView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            /*
             *  as every row is still receiving their touches
             *  we can use this to manually trigger onItemClick
             *  since it doesn't firing in popupWindow.setFocusable(false)  
             */
            mOwningListView.getOnItemClickListener().onItemClick(mOwningListView, v, position, getItemId(position));

        }
    });
    //... other stuff
    return convertView;
}

}

【讨论】:

  • 这是围绕焦点(false)的好方法,我花了几个小时修复了我的应用程序的行为后才实现了这个解决方案,现在这实际上使我放弃的事情成为可能在。非常感谢您的贡献。
【解决方案3】:

解决这个问题的一种方法是创建一个对话框,然后使用 setItems() 命令,如果你看这里:

http://developer.android.com/guide/topics/ui/dialogs.html

向下滚动到“添加列表”,有一个例子,在这里你可以在每个项目上定义 onClick()

【讨论】:

    猜你喜欢
    • 2013-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多