onOptionsItemSelected() 方法在 android 中不起作用

onOptionsItemSelected() method don`t work in android

我想在单击菜单项时打开片段,但它不起作用而且我不知道 why.I 尝试调试,但无济于事。如果有人可以提供帮助,我将非常高兴。提前致谢。

menu_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.lineup.android.webbrowser.activities.MainActivity">
<item
    android:id="@+id/clear_spinner"
    android:icon="@drawable/ic_clear"
    android:orderInCategory="50"
    android:title="Clear"
    app:showAsAction="collapseActionView|ifRoom" />
<item
    android:id="@+id/action_menu"
    android:icon="@drawable/ic_menu"
    android:orderInCategory="200"
    android:title="@string/menu"
    app:showAsAction="always">
    <menu>
        <item
            android:id="@+id/downloads"
            android:title="@string/downloads"
            android:icon="@drawable/ic_download"/>
        <item
            android:id="@+id/draw"
            android:title="@string/draw"
            android:icon="@drawable/ic_paint"/>
        <item
            android:id="@+id/history"
            android:title="@string/history"
            android:icon="@drawable/ic_action_clock"/>
        <item
            android:id="@+id/settings"
            android:title="@string/settings"
            android:icon="@drawable/ic_settings"/>
    </menu>
</item>
<item
    android:id="@+id/action_search"
    android:icon="@drawable/ic_search"
    android:orderInCategory="100"
    android:title="Search"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="collapseActionView|ifRoom" />

MainActivity.class

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_main, menu);
    return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    switch (id) {
        case R.id.downloads:
            DownloadsFragment dlfrag = new DownloadsFragment();
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, dlfrag)
                    .commit();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

DownloadsFragment.class

public class DownloadsFragment extends Fragment {

private RecyclerView.Adapter adapter;
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private ArrayList<String> listOfFiles=new ArrayList();

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_downloads, container, false);

    Toolbar toolbar = (Toolbar)view.findViewById(R.id.downloads_toolbar);
    toolbar.setTitle("Загрузки");
    ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
    ((AppCompatActivity)getActivity()). getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    recyclerView = (RecyclerView) view.findViewById(R.id.rv_downloads);
    layoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(layoutManager);

    adapter=new DownloadsAdapter(listOfFiles);
    recyclerView.setAdapter(adapter);

    return super.onCreateView(inflater, container, savedInstanceState);
}

}

onCreate方法中添加setHasOptionsMenu(true);

return super.onCreateView(inflater, container, savedInstanceState);

你需要return你的view在这里