【问题标题】:Android ListView's setOnItemClickListener from PopupWindow not called未调用来自 PopupWindow 的 Android ListView 的 setOnItemClickListener
【发布时间】:2012-06-28 11:25:17
【问题描述】:

我正在尝试从 PopupWindow 显示 ListView。但是当我尝试调用 ListView 的 setOnItemClickListener 时,什么都没有。这里是 Java 文件

PopupWindowActivity.java

public class PopupWindowActivity extends Activity {
    String[] data = { "DATA 1", "DATA 2", "DATA 3", "DATA 4", "DATA 5", "DATA 6" };
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a);
    final Button btnOpenPopup = (Button) findViewById(R.id.openpopup);
    btnOpenPopup.setOnClickListener(new Button.OnClickListener() {

        public void onClick(View arg0) {
            LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
                    .getSystemService(LAYOUT_INFLATER_SERVICE);
            View popupView = layoutInflater.inflate(R.layout.main, null);
            final PopupWindow popupWindow = new PopupWindow(popupView,
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

            ListView listView = (ListView) popupView.findViewById(R.id.listView1);
            listView.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,data));
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    // TODO Auto-generated method stub
                    System.out.println("Item Clicked");
                    popupWindow.dismiss();
                }
            });

            popupWindow.showAsDropDown(btnOpenPopup, 20, -5);

        }
    });
}

}

这是第一个xml文件 a.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" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />
<Button
    android:id="@+id/openpopup"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Open Popup Window" />

</LinearLayout>

这里它膨胀了 xml 文件 ma​​in.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:background="@drawable/recording"
android:orientation="vertical" >

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginBottom="30sp"
    android:layout_marginLeft="30sp"
    android:layout_marginRight="30sp"
    android:layout_marginTop="100sp" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="#00000000" >
    </ListView>
</RelativeLayout>

</LinearLayout>

我做错了什么?

谢谢

【问题讨论】:

  • 可以试试 listView.setAdapter(new ArrayAdapter(MyBannerActivity.this, ? 代替 getApplicationContext(),
  • @Dheeresh Singh:就这样。什么都没发生
  • 按钮点击事件正常但listview的项目点击列表器不工作

标签: java android popupwindow


【解决方案1】:

您的代码只需稍作改动,您的代码就会听到您的列表点击事件

final PopupWindow popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,true);

您忘记在 PopupWindow 构造函数中提及 focusable 设置 true

【讨论】:

  • 哦,对了,当然是安卓。为什么我会假设 PopupWindow 在默认情况下不会像我使用的所有其他视图一样具有焦点。伙计,我有时真是个白痴。
【解决方案2】:

有同样的问题,但在我的情况下,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;
}

}

【讨论】:

  • 每当您使用键盘时,这似乎是更好的答案。感谢这个想法!
  • 很高兴它有帮助!如果您认为此答案有帮助,请考虑投票,以便其他人也能找到此解决方案
  • 这是保持“setFocusble(false)”的答案!
【解决方案3】:

希望对您有所帮助 .

在按钮ClickListener之外声明listview并列出onClickListener。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-10
    相关资源
    最近更新 更多