从PopupWindow Android的ListView的setOnItemClickListen

2019-07-30 04:00发布

我试图显示来自PopupWindow一个ListView。 但是当我尝试调用ListView的setOnItemClickListener没什么haapen。 在这里它的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 main.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>

我究竟做错了什么?

谢谢

Answer 1:

在你的代码和BOOOM你的代码只是一个微小的变化会听你的列表中,单击事件

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

你忘记提及可聚焦设置在PopupWindow构造如此



Answer 2:

有同样的问题,但对我来说setFocusble(false)被要求(和使用ListPopupWindow是不是已经使用基地项目在我的情况的解决方案作为一个很多东西PopupWindow的功能,包括延长)。

如果有人在同样的情况有基于错误商榷的一种变通方法这里 (职位#9)

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

然而,这种做法是不是100%相同真正ListView的触摸操作(如没有选择的光芒,同时轻拍行)这个做得很好,我的时刻。

如果有人有这个问题进行更精确的解决方案,请分享。

所以,这里是完整的Adapter可以与使用的代码ListView里面PopupWindow这是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;
}

}



Answer 3:

愿这帮助你。

声明按钮ClickListener外ListView和列表onClickListener。



文章来源: Android ListView's setOnItemClickListener from PopupWindow not called