关于列表项视图按下时的弹出菜单

时间:2017-01-09 11:09:19

标签: android

我需要一个问题的帮助我现在找不到任何解决方案。 我想要实现的目标:

我有一个自定义ListView适配器。对于每个Listitem,我想添加popup menu之类的 支付,不支付,交付,不交付。

2 个答案:

答案 0 :(得分:1)

菜单xml

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

        <item  
            android:id="@+id/one"  
            android:title="played"/>  

        <item  
            android:id="@+id/two"  
            android:title="not played"/>  


    </menu>  

代码:

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    PopupMenu popup = new PopupMenu(MainActivity.this, listView);  
                    //Inflating the Popup using xml file  
                    popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu()); 
          popup.show(); 
  popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {  
             public boolean onMenuItemClick(MenuItem item) {  
              Toast.makeText(MainActivity.this,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();  
              return true;  
             }  
            });  

                    Intent intent = new Intent(MainActivity.this, EditActivity.class);
                    intent.putExtra("id", id);
                    intent.putExtra("food", foods.getFoods().get((int) id));
                    startActivityForResult(intent, FOOD_EDIT);
                }
            });

答案 1 :(得分:0)

尝试一下:

<强> RES /菜单/ mylistmenu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/paid"
        android:orderInCategory="100"
        android:title="Paid"/>

    <item
        android:id="@+id/notpaid"
        android:orderInCategory="101"
        android:title="Not Paid"/>

    <item
        android:id="@+id/delivered"
        android:orderInCategory="103"
        android:title="Delivered"/>

    <item
        android:id="@+id/notdelivered"
        android:orderInCategory="104"
        android:title="Not Delivered"/>
</menu>

ListItem OnClick Popup

//状态TextView

TextView statustxt = (TextView) findViewById(R.id.statusText);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            PopupMenu popup = new PopupMenu(view.getContext(),view );
             MenuInflater inflater = popup.getMenuInflater();
              inflater.inflate(R.menu.mylistmenu, popup.getMenu());

              //Handle Menu Click
              popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) {
                {
                    switch (item.getItemId()){
                        case option_one:
                            statustxt.setText("Paid");
                        case option_two:
                            statustxt.setText("Not Paid");
                        case option_three:
                            statustxt.setText("Delivered");
                        case option_four:
                            statustxt.setText("Not Delivered");
                    }
                }
                return false;
            }
        });
            popup.show();

            Intent intent = new Intent(MainActivity.this, EditActivity.class);
            intent.putExtra("id", id);
            intent.putExtra("food", foods.getFoods().get((int) id));
            startActivityForResult(intent, FOOD_EDIT);
        }
    });