【问题标题】:contextMenu for list view列表视图的 contextMenu
【发布时间】:2013-05-01 02:32:47
【问题描述】:

我正在使用列表视图。我使用适配器为该列表添加项目。我正在尝试为列表的每个项目添加上下文菜单,但我对此有问题。

这是我的代码

项目列表:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bgActivity"
android:orientation="horizontal" >

<TextView
    android:id="@+id/tv_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="10dp"
    android:textColor="@color/textColorContact"
    android:textSize="20sp"
    android:textStyle="bold" />

<Button
    android:id="@+id/pick_options"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_marginRight="10dp"
    android:background="@drawable/app_icon"
    android:text="@string/label_options" >
</Button>   

</RelativeLayout>

名单:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ListView
    android:id="@+id/lst_contacts"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/bgContactItem" />

</RelativeLayout>

菜单:

<menu 
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/CtxLblOpc1" android:title="OpcEtiqueta1"></item>
<item android:id="@+id/CtxLblOpc2" android:title="OpcEtiqueta2"></item>
</menu>

java代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contacts);

// Getting reference to listview
    ListView lstContacts = (ListView) findViewById(R.id.lst_contacts);
registerForContextMenu(lstContacts);
}

@Override
    public void onCreateContextMenu(ContextMenu menu, 
                                    View v, 
                                    ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_options, menu);


}


/** Actions for the ContextMenu items */
@Override
public boolean onContextItemSelected(MenuItem item) {
  //AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
  switch (item.getItemId()) {
      case R.id.CtxLblOpc1:
        Toast.makeText(getApplicationContext(), 
                         "Option 1",     
                         Toast.LENGTH_LONG).show();
          return true;
      case R.id.CtxLblOpc2:
        Toast.makeText(getApplicationContext(), 
                         "Option 2", 
                         Toast.LENGTH_LONG).show();
          return true;
      default:
          return super.onContextItemSelected(item);
   }
}

但我看不到上下文菜单,请帮忙,谢谢

【问题讨论】:

    标签: android-layout listview contextmenu


    【解决方案1】:

    我猜你应该尝试添加(在布局中)

    android:descendantFocusability="blocksDescendants"
    

    在您的布局中。 你可以给看看here 我建议您使用带有 ActionMode.Callback 的上下文菜单。如果你想要一个例子看here

    注意:链接来自我的博客

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-24
      • 1970-01-01
      • 2021-11-18
      • 1970-01-01
      • 2012-07-24
      • 2016-05-15
      相关资源
      最近更新 更多