【问题标题】:Context menu is not showing on Android list item long pressAndroid列表项长按时未显示上下文菜单
【发布时间】:2013-12-05 09:39:32
【问题描述】:

我被我添加的列表视图上的长按卡住了

registerForContextMenu(this.objListView);

关于 on create 方法。我的 onCreateContextMenu 如下

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    System.out.println("ENTERED IN THE CONTEXT MENU BLOCK");
    if (v.getId() == R.id.booksLV) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
        String objBook = userVector.get(info.position);
        menu.setHeaderTitle(objBook);
        String[] menuItems = {"Edit","Delete" };
        for (int i = 0; i < menuItems.length; i++) {
            menu.add(Menu.NONE, i, i, menuItems[i]);
        }
    }
}

 <ListView
            android:id="@+id/booksLV"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="0.5"

            android:dividerHeight="1px"
            android:paddingRight="1dip"
             >
        </ListView>

自定义适配器组件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listitem_test"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:focusable="true"
    android:gravity="center_vertical"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/nameLL"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/pure_white_color"
        android:clickable="true"
        android:focusable="true"
        android:orientation="horizontal"
        android:padding="10dip" >

        <TextView
            android:id="@+id/nameTV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@+id/TextView01" 
            android:textAppearance="?android:attr/textAppearanceSmall">
        </TextView>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/headingLL"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/off_white_color" >

        <TextView
            android:id="@+id/headingTV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="5dip"
            android:text="@+id/TextView01" 
              android:textColor="@color/darkGray"
               android:textAppearance="?android:attr/textAppearanceSmall">
        </TextView>
    </LinearLayout>

</LinearLayout>

创建

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.savedInstanceState = savedInstanceState;
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int width = size.x;
        int height = size.y;
        System.out.println(width + " ++++++++++++++++++++ " + height);
        Toast.makeText(this, width + " " + height, Toast.LENGTH_LONG).show();
        if (width == 240 && height == 400) {
            setContentView(R.layout.activity_flat_accountlist_exp_h);
        } else if (width == 480 && height == 856) {
            setContentView(R.layout.activity_flat_accountlist_exp_h);
        } else if (width == 480 && height == 854) {
            setContentView(R.layout.activity_flat_accountlist_exp_h);
        } else if (width == 600 && height == 976) {
            setContentView(R.layout.activity_flat_accountlist_exp_mdpi_h);
        } else if (width == 600 && height == 1024) {
            setContentView(R.layout.activity_flat_accountlist_exp_mdpi_h);
        } else {
            setContentView(R.layout.activity_flat_accountlist);
        }

        float scale = this.getResources().getDisplayMetrics().density;
        mSearchView = (SearchView) findViewById(R.id.searchView);

        objListView = (ListView) findViewById(R.id.booksLV);

        registerForContextMenu(objListView);

        selectedIndex = (TextView) findViewById(R.id.selectedIndex);


        showProcessDialog();
        getAccounts();



    }

长按不显示上下文菜单

请帮帮我

谢谢

【问题讨论】:

  • 长按不显示上下文菜单
  • @Amith R.id.booksLV 是列表视图 ID 吗?
  • 你能贴出你的list item的xml代码吗?
  • @ Tamilan @ Abhishek V 我已经更新了问题请看

标签: android listview


【解决方案1】:

在 Listview 上尝试以下代码以获取上下文菜单。

首先在 listview 上注册您的内容菜单..

ListView mListView = (ListView)findViewById(R.id.listview);
registerForContextMenu(mListView);

在内容菜单上添加菜单...

@Override
    public void onCreateContextMenu(ContextMenu menu, 
                    View v, ContextMenuInfo menuInfo) {
        menu.add(0, 1, 0, "Add");
        menu.add(0, 2, 1, "Rename");
        menu.add(0, 3, 2, "Delete");
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        super.onContextItemSelected(item);

        if(item.getTitle().equals("Add")) {
            //Add code
        } else if(item.getTitle().equals("Rename")) {
            //Rename code
        } else if(item.getTitle().equals("Delete")) {
            //Delete code
        }
        return true;
    };

【讨论】:

  • 我正在使用自定义适配器
  • 自定义适配器有什么用? ContextMenu 还是 ListView??
【解决方案2】:

您必须在 onCreate 方法中将 longclicklistener 添加到您的视图中

this.objListView.setOnLongClickListener(new OnLongClickListener() { 
    @Override
    public boolean onLongClick(View v) {
        // Here open the context menu
        openContextMenu(this.objListView);
    }
});

希望对你有帮助

【讨论】:

    【解决方案3】:

    你的 onCreateContextMenu() 方法被调用了吗?

    如果是,您是否尝试在添加项目后调用超级:

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    
        System.out.println("ENTERED IN THE CONTEXT MENU BLOCK");
        if (v.getId() == R.id.booksLV) 
        {
            AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
            String objBook = userVector.get(info.position);
            menu.setHeaderTitle(objBook);
            String[] menuItems = {"Edit","Delete" };
            for (int i = 0; i < menuItems.length; i++) {
                menu.add(Menu.NONE, i, i, menuItems[i]);
            }
    
        }
        super.onCreateContextMenu(menu, v, menuInfo);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-04
      • 2022-11-13
      • 1970-01-01
      • 2011-03-25
      相关资源
      最近更新 更多