每次滑动后,为什么Android菜单中的项目会被添加?

时间:2016-06-24 09:40:25

标签: android android-fragments menu menuitem

我想从右到左移动工具栏中的一些项目,为此我使用了android.support.v7.widget.ActionMenuView,但我遇到了麻烦。

我的活动包含三个片段。现在例如,如果我在app启动时从工具栏打开菜单,一切都很好。但是如果我从A滑到B,菜单中的项目会加倍。在启动菜单上包含两个项目,但在刷到另一个片段后,项目变为双倍(它变为四个,依此类推)。

我尝试过搜索互联网,但这类问题不会发布在任何地方。

我的布局:

<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
 <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tool_bar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="2dp"
        android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        >

   <cn.nekocode.toolbarindicator.ToolbarIndicator
    android:id="@+id/indicator"
    android:layout_height="match_parent"
    android:layout_width="120dp"
    android:layout_gravity="center" />
<android.support.v7.widget.ActionMenuView
    android:id="@+id/amvMenu"
    android:layout_width="wrap_content"
    android:layout_height="?attr/actionBarSize"/>
 </android.support.v7.widget.Toolbar>

    <android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v4.view.ViewPager>

   </LinearLayout>

MainActivity:

public class MainActivity extends AppCompatActivity{

ViewPager viewPager;
TabsPagerAdapter adapter;
Toolbar toolbar;
private SharedPreferences savedPreferences;
ToolbarIndicator toolbarIndicator;
ActionMenuView amvMenu;

CharSequence Titles[] = {"one", "two", "three"};
int Numboftabs = 3;

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override
protected void onCreate(Bundle savedInstanceState) {

    savedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
//here is the problem
 toolbar = (Toolbar) findViewById(R.id.tool_bar);
    if (toolbar != null) {
        toolbar.setTitle("");
        toolbar.setSubtitle("");
        setSupportActionBar(toolbar);

    }
 adapter = new TabsPagerAdapter(getSupportFragmentManager(), Titles, Numboftabs);
    // Assigning ViewPager View and setting the adapter
    viewPager = (ViewPager) findViewById(R.id.pager);
    viewPager.setAdapter(adapter);
    viewPager.setOffscreenPageLimit(2);
    // Assiging the fadingIndicator View
     toolbarIndicator = (ToolbarIndicator) this.findViewById(R.id.indicator);
    amvMenu = (ActionMenuView) toolbar.findViewById(R.id.amvMenu);
    amvMenu.setOnMenuItemClickListener(new ActionMenuView.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            return onOptionsItemSelected(menuItem);
        }
    });
toolbarIndicator.setViewPager(viewPager);
}


 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, amvMenu.getMenu());
    return true;
}

//handling the tap on the menu's items
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    switch (id) {
        case R.id.action_settings: {//open settings
            startActivity(new Intent(this, SettingsActivity.class));
            return true;
        }
        case R.id.exit:{
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(1);
        }
        default:
            break;
    }

    return super.onOptionsItemSelected(item);
}


}

如果我将此行getMenuInflater().inflate(R.menu.menu_main, amvMenu.getMenu());修改为getMenuInflater().inflate(R.menu.menu_main, menu);  一切都很好,但项目再次出现在右边。

1 个答案:

答案 0 :(得分:1)

根据评论部分:在充气前清除onCreateOptionsMenu()中的菜单。

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    amvMenu.getMenu().clear(); // add this line
    getMenuInflater().inflate(R.menu.menu_main, amvMenu.getMenu());
    return true;
}