ContextMenu不起作用

时间:2013-12-10 21:50:00

标签: android contextmenu android-linearlayout

我正在尝试为ListView执行上下文菜单,但它无效。当我点击项目时,没有任何反应。我已经注册了我的列表视图但仍无法正常工作:(

有什么想法吗?

fragment.xml之

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textViewLblSearchScreen"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="top|left"
    android:layout_marginLeft="25dp"
    android:layout_marginTop="25dp"
    android:text="@string/lblSearchScreen" />

<Button
    android:id="@+id/buttonSearchUsers"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="top|right"
    android:layout_marginRight="25dp"
    android:background="@drawable/bt_gradient"
    android:text="@string/btnSearchUsers" />

<ListView
    android:id="@android:id/list"
    android:layout_gravity="bottom"
    android:layout_width="match_parent"
    android:layout_height="820dp" >
</ListView>

</FrameLayout>     

item_row.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="40dp"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/imageProfileThumbnail"
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_gravity="left"
    android:layout_weight="1"
    android:contentDescription="@string/select_photo" />

<LinearLayout
    android:layout_width="250dp"
    android:layout_height="30dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textViewUsername"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:textSize="12sp">
    </TextView>

    <TextView
        android:id="@+id/textViewGender"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:textSize="12sp">
    </TextView>
</LinearLayout>

</LinearLayout>

Fragment.java

public class SearchFragment extends ListFragment implements LoaderManager.LoaderCallbacks<List<ResultSearchUser>> {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View layout = super.onCreateView(inflater, container,
            savedInstanceState);
    ListView lv = (ListView) layout.findViewById(android.R.id.list);

    ViewGroup parent = (ViewGroup) lv.getParent();
    int lvIndex = parent.indexOfChild(lv);      
    parent.removeViewAt(lvIndex);       
    FrameLayout mLinearLayout = (FrameLayout) inflater.inflate(
            R.layout.search_fragment, container, false);
    registerForContextMenu(lv);
    parent.addView(mLinearLayout, lvIndex, lv.getLayoutParams());       
    final User user = new UserService(getActivity()).getUser();


    return layout;
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle(R.string.ctxMenuResultSearch);
    menu.add(Menu.NONE, v.getId(), 1, R.string.ctxMenuAddFriend);
    menu.add(Menu.NONE, v.getId(), 0, R.string.ctxMenuSendMessage);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    // TODO Auto-generated method stub

    switch (item.getItemId()) {
    case 0:
        System.out.println("oi");
        return true;
    default:
        return super.onContextItemSelected(item);
    }

}

}

提前致谢。

1 个答案:

答案 0 :(得分:0)

没有任何事情发生,但你并没有要求发生任何事情。

您应该查看开发者指南,了解如何创建上下文菜单http://developer.android.com/guide/topics/ui/menus.html#context-menu

你遗失了,至少,像是

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

等等

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    switch (item.getItemId()) {
        case R.id.edit:
            editNote(info.id);
            return true;
        case R.id.delete:
            deleteNote(info.id);
            return true;
        default:
            return super.onContextItemSelected(item);
    }
}

或其他一些内容,如果您希望操作栏以任何方式响应。请参阅official guide