每次在溢出选项菜单中打开时复制菜单项

时间:2017-07-02 05:56:24

标签: android menu android-actionbar menuitem

我正在尝试使用xml代码在操作栏中添加两个菜单项:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/search"
android:title="@string/search"
android:icon="@drawable/ic_action_search"
app:showAsAction="ifRoom"
/>
<item
android:id="@+id/share"
android:title="@string/share"
android:icon="@drawable/ic_action_share"
app:showAsAction="ifRoom"
/>

我的java代码是:

    package com.erprakash.contacts;
    import android.content.ContentUris;
    import android.content.Context;
    import android.content.Intent;
    import android.database.Cursor;
    //import android.net.Uri;
    import android.provider.ContactsContract;
    //import android.support.v7.app.ActionBar;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.support.v7.widget.Toolbar;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Toast;

    import java.util.ArrayList;
    //import java.util.Comparator;

   public class MainActivity extends AppCompatActivity {

   private RecyclerView recyclerView;
   private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;

ArrayList<Contact> arrayList = new ArrayList<>();
int img_id = R.drawable.image1;
String name ;
String number ;
private int id;
private Contact contact;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   //Tool bar setting
    //toolbar end
    // Reading Contacts -----Working
    Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null,
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE NOCASE ASC");
    if (phones != null) {
        while (phones.moveToNext())
        {
            name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            id = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Photo.PHOTO_FILE_ID));
            if(id == 0){
                contact = new Contact(number, name, img_id);
            }else {
                 contact = new Contact(number, name, id);
            }
            arrayList.add(contact);
        }
        phones.close();
    }



    /**
    name = getResources().getStringArray(R.array.person_name);
    number = getResources().getIntArray(R.array.mobile_number);
    int c = 0;
    for(String Name: name)
    {
        Contact contact = new Contact(number[c],Name,img_id[c]);
        c++;
        arrayList.add(contact);
    }
     **/
    recyclerView = (RecyclerView)findViewById(R.id.rvMainContennt);
    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setHasFixedSize(true);
    adapter = new ContactAdapter(arrayList);
    recyclerView.setAdapter(adapter);


}
//-------------Toolbar------------


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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int res_id = item.getItemId();
    if(res_id == R.id.search)
    {
        Toast.makeText(this.getApplicationContext(),"You have seleted search option",Toast.LENGTH_SHORT).show();
    }
    return true;
}
@Override
public boolean onPrepareOptionsMenu(final Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return super.onCreateOptionsMenu(menu);
}
}

在预览中,设计显示正确: enter image description here

但在运行状态:enter image description here

点击选项菜单:enter image description here

每当我打开选项菜单时,内容都会重复并继续,我已经搜索了解决方案,但我没有在任何地方获取它。帮助我......提前致谢

1 个答案:

答案 0 :(得分:1)

删除onPrepareOptionsMenu()

@Override
public boolean onPrepareOptionsMenu(final Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return super.onCreateOptionsMenu(menu);
}

更新onCreateOptionsMenu(),如下所示:

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

如果您希望始终在Toolbar上显示菜单项,请按以下方式更新menu_main.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/search"
        android:title="@string/search"
        android:icon="@drawable/ic_action_search"
        app:showAsAction="always" />

    <item
        android:id="@+id/share"
        android:title="@string/share"
        android:icon="@drawable/ic_action_share"
        app:showAsAction="always" />
</menu>