ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android – 打开对话框正好在单击项目上方菜单中

2019-06-09 08:12:01  阅读:121  来源: 互联网

标签:android android-listview android-dialog


我已经创建了自定义对话框,并希望将其绑定到我的列表项.
我希望此对话框在长按列表项时的行为类似于上下文菜单.

换句话说,我不希望此对话框出现在屏幕的中央,而是显示在列表中的项目附近.

我花了很多时间搜索,但遗憾的是没有结果.有一些好的解决方案吗?

解决方法:

   btn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        // infalte menu

        PopupMenu popup = new PopupMenu(activity, v);
        popup.getMenuInflater().inflate(R.menu.clipboard_popup,
                popup.getMenu());
        popup.show();
        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {

                switch (item.getItemId()) {
                case R.id.install:

                    // Or Some other code you want to put
                    // here.. This is just an example.
                    Toast.makeText(
                            activity,
                            " Install Clicked at position " + " : "
                                    + position, Toast.LENGTH_LONG)
                            .show();

                    break;
                case R.id.addtowishlist:

                    Toast.makeText(
                            activity,
                            "Add to Wish List Clicked at position "
                                    + " : " + position,
                            Toast.LENGTH_LONG).show();

                    break;

                default:
                    break;
                }

                return true;
            }
        });

    }
});

更新了自定义布局的答案

 PopupWindow popupwindow_obj = popupDisplay();
 popupwindow_obj.showAsDropDown(clickbtn, -40, 18); 

 // where u want show on 
 //view click event popupwindow.showAsDropDown(view, x, y);

    public PopupWindow popupDisplay() 
    { 

        final PopupWindow popupWindow = new PopupWindow(this);

          // inflate your layout or dynamically add view
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

            View view = inflater.inflate(R.layout.mylayout, null);

            Button item = (LinearLayout) view.findViewById(R.id.button1);

            popupWindow.setFocusable(true);
            popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
            popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
            popupWindow.setContentView(view);

            return popupWindow;
        }

mylayout.xml

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Window test" />
    </LinearLayout>

标签:android,android-listview,android-dialog
来源: https://codeday.me/bug/20190609/1204011.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有