简体   繁体   English

OnLong Click ListView Pop-UP

[英]OnLong Click ListView Pop-UP

I would like to implement in my listview the longclick, up there I succeeded perfectly :) Now I would like to create a sort of pop-up with more possibilities, for example, paste, delete, remove, etc ... To make you understand I found a picture on the internet :) Ve mail below. 我想在我的listview中实现longclick,在那里我完美地成功了:)现在我想创建一种具有更多可能性的弹出窗口,例如,粘贴,删除,删除等...让你明白我在互联网上找到了一张图片:)以下邮件。 Thank you in advance 先感谢您

在此输入图像描述

I think the preferred way is to use action mode for these types of menus. 我认为首选的方法是对这些类型的菜单使用动作模式。 Take a look at this I think this will have all the info you need. 看看这个,我想这将包含您需要的所有信息。 It seems a bit overwhelming, but it's not that bad to implement. 这看起来有点压倒性,但实施并不是那么糟糕。 http://developer.android.com/guide/topics/ui/menus.html http://developer.android.com/guide/topics/ui/menus.html

Try to use pop menu like this.. 尝试使用像这样的弹出菜单..

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button button = (Button) findViewById(R.id.button2);
    registerForContextMenu(button);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    getMenuInflater().inflate(R.menu.main, menu);
}

public void showPopup(View v) {
    PopupMenu popup = new PopupMenu(this, v);
    getMenuInflater().inflate(R.menu.main, popup.getMenu());
    popup.show();
    popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem arg0) {
            Toast.makeText(MainActivity.this, "" + arg0, Toast.LENGTH_SHORT)
                    .show();
            return false;
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

} }

And xml like this.. 和xml这样..

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:id="@+id/edit"
    android:title="edit"/>
<item
    android:id="@+id/delete"
    android:title="delete"/>
<item
    android:id="@+id/exit"
    android:title="exit"/>

Try this below code.. 试试以下代码..

in java 在java中

 listView.setOnItemLongClickListener(new OnItemLongClickListener()
            {

                @Override
                public boolean onItemLongClick(AdapterView<?> arg0, View v,
                        int position, long id) {
                    // TODO Auto-generated method stub

                    LayoutInflater layoutInflater= (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);             
                    final View popupView = layoutInflater.inflate(R.layout.list_popup, null);              
                    final PopupWindow popupWindow = new PopupWindow(popupView,155,LayoutParams.WRAP_CONTENT);

                    ListView listView = (ListView) popupView.findViewById(R.id.list_view);

                    listView.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,menu));


                    popupWindow.showAsDropDown(listView, 50, 0);

                    return false;
                }

            });

list_popup.xml list_popup.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                       android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:gravity="center"
                    android:background="#44444d"
                 >      
        <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="#00000000"
        android:divider="#5c7089"
        android:dividerHeight="2dp"
        android:layout_marginTop="3dp"/>


</LinearLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM