ListFragment:重新加载片段后未调用onContextItemSeleected

时间:2014-07-29 22:19:42

标签: android android-fragments

我有一个包含上下文菜单的列表片段。

这就是我为上下文菜单注册listview的方式:

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    inflater.inflate(R.layout.manage_notifications, container);
    Log.d(TAG, "Resumed: " + isResumed());
    if(savedInstanceState == null){
        lv = (ListView) container.findViewById(R.id.notificationsList);
        registerForContextMenu(lv);
        //lv.setLongClickable(true);
        lv.setOnItemClickListener(this);
        config = (AppConfiguration)getActivity().getApplicationContext();
        helper = config.getDbHelper();
        Notification[] notifications = helper.getNotifications();
        Log.d(TAG, "List size: " + notifications.length);
        if(notifications.length>0){
            adapter = new NotificationAdapter(getActivity(),R.layout.notification_item, notifications);
            lv.setAdapter(adapter);
        } else {
            lv.setEmptyView(new View(getActivity()));
        }
    }
    return super.onCreateView(inflater, container, savedInstanceState);
}

然后onCreateContextMenu方法是:

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) { 
    Log.i(TAG, "Called... create context");
    menu.clear();
    MenuInflater inflater = getActivity().getMenuInflater();
    inflater.inflate(R.menu.list_item_menu, menu);
}

onContextItemSelected方法:

@Override
    public boolean onContextItemSelected(MenuItem item) {
        Log.i(TAG, "Item selected called");
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        switch(item.getItemId()){
        case R.id.remove_element:
            Log.i(TAG, "Posistion: " + info.position);
            Notification notification = adapter.getItem(info.position);
            Log.i(TAG, "Notification: " + notification.getId() + " " + notification.getMessage());
            helper.deleteNotification(notification.getId());
            adapter.remove(info.position);
            ListView lv = (ListView)getActivity().findViewById(R.id.notificationsList);
            lv.invalidate();
            return true;
        }
        return super.onContextItemSelected(item);
    }

现在的问题是: 当我启动应用程序,并且我第一次导航到包含listView的片段时,一切都工作正常,事件是上下文菜单,但是如果我离开那个片段(即在另一个片段中)然后返回到它(通过从选项列表中再次选择它)上下文菜单停止工作,并且不调用onContextItemSelected方法。

问题出在哪里?

这是我在片段之间切换的方式:

switch(position){
case NEW_REMINDER:
    Log.i(TAG, "New");
    transaction.replace(container, new NewReminderFragment());
    transaction.commit();
    break;
case MANAGE_REMINDER:
    Log.i(TAG, "Manage");
    transaction.replace(container, new ManageNotifications());
    transaction.commit();
    break;
}

带有listview的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@+id/notificationsList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>

</RelativeLayout>

0 个答案:

没有答案