某些设备上,Android中PopupWindow中的Listview onitemclick无法工作

11

我的ListView位于PopupWindow内。

当我展示PopupWindow并点击ASUS K00z fonepad设备中的Listview行时,它工作得很好。

但在HTC Z715e上不起作用(onitem click事件没有触发)

1.这是我的listview项布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ln_testpopitemcon"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="52dp"
    android:background="#3b8ed4"
    android:descendantFocusability="blocksDescendants">

<ImageView
    android:id="@+id/img_testiconmenu"
    android:layout_margin="10dp"
    android:layout_width="32dp"
    android:layout_height="32dp"
    android:src="@drawable/radio_selected"
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false"/>

    <TextView
        android:id="@+id/tv_testtitlemenu"
        android:gravity="left|center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"/>

</LinearLayout>

2. 这是我的弹出窗口布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ln_testpopocontainer"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:paddingLeft="2dp"
        android:paddingRight="2dp"
        android:paddingBottom="2dp"
        android:id="@+id/lv_testpop"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:choiceMode="singleChoice"
        android:dividerHeight="2dp"
        android:background="#00000000"
        android:orientation="vertical"/>

</LinearLayout>

3. 这是我的适配器

public class testmenuadapter extends BaseAdapter{
    private Context context;
    private ArrayList<MenuInfo> MenuList;
    private LayoutInflater Layf;

    public testmenuadapter(Context context, ArrayList<MenuInfo> menuList){
        this.context = context;
        this.MenuList = menuList;
        this.Layf = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return MenuList.size();
    }

    @Override
    public Object getItem(int position) {
        return MenuList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = Layf.inflate(R.layout.testpopoitem, null);

            holder.img_testiconmenu = (ImageView)convertView.findViewById(R.id.img_testiconmenu);
            holder.tv_testtitlemenu = (TextView)convertView.findViewById(R.id.tv_testtitlemenu);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder)convertView.getTag();
        }

        MenuInfo info = MenuList.get(position);

        if(info != null) {
            if (holder.tv_testtitlemenu != null) {
                holder.tv_testtitlemenu.setText(info.getTitle());
            }
        }

        return convertView;
    }

    public class ViewHolder
    {
        ImageView img_testiconmenu;
        TextView tv_testtitlemenu;
    }
}

4.这是我用来创建和显示弹出窗口的代码

final View actionview = inflater.inflate(R.layout.testpopo, (ViewGroup)getActivity().findViewById(R.id.ln_testpopocontainer));
this.testpopup = new PopupWindow(actionview, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
this.testpopup.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
this.testpopup.setOutsideTouchable(false);
this.testpopup.setAnimationStyle(R.style.Animation);

this.testpopuplistview = (ListView)this.testpopup.getContentView().findViewById(R.id.lv_testpop);

this.testmenupopup = new ArrayList<MenuInfo>();
this.testmenupopup.add(new MenuInfo("aaa", "AAA", 0, 0, false));
this.testmenupopup.add(new MenuInfo("bbb", "BBB", 0, 0, false));
this.testmenupopup.add(new MenuInfo("ccc", "CCC", 0, 0, false));

this.testpopadapter = new testmenuadapter(getActivity(), this.testmenupopup);

this.testpopuplistview.setAdapter(this.testpopadapter);
this.testpopuplistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        Toast.makeText(getActivity(), ((MenuInfo)adapterView.getItemAtPosition(position)).getTitle(), Toast.LENGTH_LONG).show();
    }
});

Button btnshowpop = (Button)findViewById(R.id.btn_showpop);
btnshowpop.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        testpopup.showAtLocation(rootView, Gravity.CENTER, 0, 0);
    }
});

如何解决它

编辑 我可以解决我的问题。

替换

this.testpopup = new PopupWindow(actionview, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

this.testpopup = new PopupWindow(actionview, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);

对不起浪费你的时间,我非常愚蠢。


1
我可以解决我的问题,请在问题的编辑部分查看。 - user2955394
1
非常感谢,我已经苦恼了两天,终于得到了你的解决方案,它非常棒。 - Sai
谢谢!它节省了我的时间。 - Dima
2个回答

3
您可以使用上下文菜单代替弹出菜单,它的工作方式与后者相同。 要创建上下文菜单,请参考此链接:

http://developer.android.com/guide/topics/ui/menus.html#context-menu

在此链接中,参考创建上下文菜单
按照这个例子,您可以添加上下文菜单—— 首先在onclicklistener中注册它。
注册上下文菜单:
registerForContextMenu(view)
for oncreate  
    @Override     
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo)  
 {  
        menu.add(Menu.NONE, CONTEXT_MENU_UNPAIR_ITEM, Menu.NONE, "UNPAIR");
        menu.add(Menu.NONE, DEFAULT_DEVICE, Menu.NONE, "USE AS CGM DEVICE");        
    }

For item selected in context menu  
@Override  
public boolean onContextItemSelected(MenuItem item)  
{  
 switch (item.getItemId())  
{  
 case CONTEXT_MENU_UNPAIR_ITEM:  
 //whatever u want
break  
case DEFAULT_DEVICE:  
 //whatever your logic accordind to u  
return(true);  
}  
return(super.onOptionsItemSelected(item));  
}                            

1
您可能希望在 ListView 中使用 ListAdapter 替代 BaseAdapter,并在实现 ListAdapter 的类中为 areAllItemsEnabledisItemEnabled 返回 True。

像下面这样...

class CustomListAdapter implements ListAdapter {

    Activity callingActivity;
    ArrayList<String> items;

    public CustomListAdapter(Activity act, ArrayList<String> list)
    {
        callingActivity = act;
        items = list;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) { 
    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        AbsListView.LayoutParams params = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);

        LinearLayout ll = new LinearLayout(callingActivity);
        ll.setLayoutParams(params);
        ll.setGravity(Gravity.CENTER);
        ll.setOrientation(LinearLayout.HORIZONTAL);
        ll.setPadding(5, 5, 5, 5);

        //
        // Code for your list item here...
        //

        return ll;
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }

    @Override
    public int getViewTypeCount() {
        return 1;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

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

    @Override
    public boolean isEnabled(int position) {
        return true;
    }

}

祝你好运。:)


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