在运行时删除工具栏项目

亚历山德罗·尤迪康

我的应用程序中有一个工具栏,我想在应用程序执行期间(换句话说,在运行时)动态修改它的内容

例如,该应用程序能够拍摄和预览照片;预览照片后,用户可以选择一些照片并向服务器执行发送操作。我还想让用户能够在选择其中一些照片后删除照片,为此,我希望操作栏上的“删除”项目(可通过垃圾桶图标识别)仅在一张或多张照片时可见照片被选中。

这是可能的吗?如果是,如何?

工具栏

In other words and, more generically, I want to control items (visibility) in the toolbar, setting the code as they will be "visible" only when some conditions are "true" (in the example above, when photos are selected or, in a different example, when user is logged) and invisible when they are "false" (when user isn't logged).

For now I just need to "remove" (or make invisible) items in the Toolbar, but it will be useful also to know if is possible to add items in the toolbar on run-time.

I'm adding some code which can help to understand the problem.

app_bar.xml file in "/res/layout", which graphically defines the Toolbar

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:elevation="4dp"
    android:theme="@style/ToolbarTheme" />

menu_resources.xml file, which defines the Toolbar items

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<!-- "User favourite function", should appear as action button if possible -->
<item
    android:id="@+id/action_app_icon"
    android:icon="@mipmap/ic_launcher"
    android:title="@string/action_bar_app_icon"
    app:showAsAction="always" />

<!-- Settings, should always be in the overflow -->
<item
    android:id="@+id/action_delete"
    android:icon="@drawable/trash"
    android:title="@string/action_bar_delete"
    app:showAsAction="always"/>

<!-- Settings, should always be in the overflow -->
<item
    android:id="@+id/action_settings"
    android:icon="@drawable/settings"
    android:title="@string/action_bar_settings"
    app:showAsAction="never"/>

<!-- About, should always be in the overflow -->
<item
    android:id="@+id/about"
    android:icon="@android:drawable/ic_dialog_info"
    app:showAsAction="never"
    android:title="@string/action_bar_about"/>

工具栏所在的活动的一部分

public class myClass extends AppCompatActivity implements View.OnClickListener {

// Instantiating a toolbar
private Toolbar toolbar;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_class);

    // Adding toolbar to the activity
    toolbar = (Toolbar) findViewById(R.id.my_toolbar);
    setSupportActionBar(toolbar);

    // Get a support ActionBar corresponding to this toolbar
    ActionBar ab = getSupportActionBar();
    // Enable the Up button
    ab.setDisplayHomeAsUpEnabled(true);
    }

    // The method that creates an options menu
    @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_resource, menu);
        // This make the delete item invisible
        menu.findItem(R.id.action_delete).setVisible(false);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_settings:
                // Perform the settings action
                return true;
            case R.id.about:
                // Perform the about
                return true;
            case R.id.action_delete:
                deletePhotos();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }


    public static void manageSelection(Boolean state, int position){
        if (photoArray.getNumberSelected() == 0) {
            // disable the trash icon and its functionality;
        } else {
            // enable the trash icon with its functionality;
        }
    }

    // This method allows to deleteItems images to the array
    public void deletePhotos() {
        //code for deleting photos
    }
}

谢谢你的时间。

计时码表

请在您的活动中尝试此代码:

public class ActivityClass extends AppCompatActivity {

    MenuItem menuItem; // Make global toolbar's menuItem

    .
    .
    .

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_layout, menu);

        menuItem = menu.findItem(R.id.toolbar_action_button) // Your toolbar´s button ID and save it in your global menuItem

        return super.onCreateOptionsMenu(menu);
    }

    public void showMenuItem(){
        menuItem.setVisible(true); // Call this method in runtime when you need show it
    }

    public void hideMenuItem(){
        menuItem.setVisible(false); // Call this method in runtime when you need hide it
    }

[编辑]

多亏了Alessandro Iudicone评论,我们还有另一种方法来获取工具栏菜单,而无需全局 MenuItem 而只有全局 Toolbar 实例:

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

    public void showMenuItem(){
        toolbar.getMenu().findItem(R.id.toolbar_actio‌​n_button).setVisible‌​(true);
    }

    public void hideMenuItem(){
        toolbar.getMenu().findItem(R.id.toolbar_actio‌​n_button).setVisible‌​(false);
    }

希望能帮助到你 :)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章