单击时菜单选项不响应

时间:2017-09-25 19:57:41

标签: android menu tabs

我的应用程序中有一个菜单,它运行正常,现在我添加TabLayout,菜单选项现在不再响应用户点击。

这是我的activity_main.xml代码:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <include layout="@layout/toolbar"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill">

    </android.support.design.widget.TabLayout>

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/viewPaper"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</android.support.v4.view.ViewPager>

我的活动onCreate方法:

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    preferences = PreferenceManager.getDefaultSharedPreferences(this);
    itemAdapter = new ClientAdapter(this, null);
    tab = (TabLayout)findViewById(R.id.tab);
    paper = (ViewPager)findViewById(R.id.viewPaper);
    paperAdapter = new ViewPaperAdapter(getSupportFragmentManager());

    HomeFragment homeFragment = new HomeFragment();
    DoneFragment doneFragment = new DoneFragment();


    paperAdapter.addFrament(homeFragment, "Home");
    paperAdapter.addFrament(doneFragment, "Done");

    paper.setAdapter(paperAdapter);
    tab.setupWithViewPager(paper);
}

onCreateOptionMenu

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    MenuItem menuItem = menu.findItem(R.id.action_search);
    searchView = (SearchView)MenuItemCompat.getActionView(menuItem);
    searchView.setOnQueryTextListener(this);
    return super.onCreateOptionsMenu(menu);
}

onOptionItemSelected方法:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case R.id.refresh:
            //Check the internet connection
            if(Util.isConnected(this)){
              //  showProgress();
                //Check if there is not pending item
                sincronize(this);
                dbManager.borrar();
               new HomeFragment().getTarjetas();
            }else{
                Toast.makeText(this, "No hay conexion a internet.", Toast.LENGTH_LONG).show();
            }
            break;
    }
    return super.onOptionsItemSelected(item);
}

menu.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="developer.technologies.agilisa.acardmobile.MainActivity">

<item
    android:id="@+id/action_search"
    android:icon="@android:drawable/ic_menu_search"
    android:orderInCategory="100"
    android:title="@string/Search"
    app:showAsAction="always|collapseActionView"
    android:elevation="8dp"
    app:actionViewClass="android.support.v7.widget.SearchView"/>

<item
    android:id="@+id/refresh"
    android:title="@string/refresh"
    android:icon="@drawable/refresh"
    android:orderInCategory="200"
    app:showAsAction="always|collapseActionView"/>

<item
    android:id="@+id/cerrar"
    android:orderInCategory="10"
    app:showAsAction="never"
    android:title="@string/cerrar_session"/>

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

尝试此更改:

将您的根布局更改为LinearLayoutCoordinator Layout - 因为framelayout有时会消耗其下的项目的点击事件。

1.in onCreateOptionsMenu()

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    //Your code
    return true;
}

2.并在onOptionsItemSelected()添加return true

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.refresh:
        // your code
        return true;    //add this
    default:
        return super.onOptionsItemSelected(item);
    }
}