如何从菜单(onOptionsItemSelected)缩小/隐藏(不可见)ViewStub?

发布于 12-13 20:58 字数 1812 浏览 0 评论 0原文

我不断收到 NullPointerException 试图从 UI 中缩小或隐藏 ViewStub。我只是想确定我做得对。

我通过执行以下操作在 GalleryView 的 onItemLongClick 方法中膨胀我的 ViewStub:

@Override
            public boolean onItemLongClick(AdapterView<?> arg0, View viu, int arg2,
                    long arg3) {
                Toast.makeText(GalleryView.this, "New item added to Favorites", Toast.LENGTH_SHORT).show();


                favsCount++;

               //checking to see if ViewStub is already inflated or not
                if(!stubvis){
                stub = (ViewStub) findViewById(R.id.stub1);
                stub.inflate();
                stubvis = true;
                trayUP = true;
                }

                return true;
            }

            });

然后在 onPrepareOptionsMenu() 中,我根据 ViewStub 的可见性添加菜单项。如果膨胀且可见,我创建一个菜单项来隐藏它,否则,创建一个菜单项来显示/使其可见。

@Override

public boolean onPrepareOptionsMenu(Menu menu) {

menu.clear();


if(trayUP) {

menu.add(0, HIDETRAY, 0, "Hide Favorites Tray");

} else {

menu.add(0, SHOWTRAY, 0, "Show Favorites Tray");

}

return super.onPrepareOptionsMenu(menu);

}

接下来,在 onOptionsItemSelected() 中,我根据菜单项选择编写两种情况。情况 1 当托盘不可见时,所以我让它可见。情况 2 当它可见时,所以我通过执行以下操作来隐藏它:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

        case HIDETRAY:

            Log.v(TAG, "Hiding Favs Tray");
            findViewById(R.id.stub1).setVisibility(View.GONE);
            trayUP = false;

        case SHOWTRAY:

            Log.v(TAG, "Showing Favs Tray");
            findViewById(R.id.stub1).setVisibility(View.VISIBLE);
            trayUP = true;

        }

    return true;

}

我知道我在某个地方犯了一个愚蠢的错误。我的大脑现在已经太饱和了,无法正常思考。需要帮助:(

谢谢,

阿布

I keep getting NullPointerException trying to deflate or make invisible the ViewStub from my UI. I just wanted to be sure I am doing it right.

I am inflating my ViewStub in onItemLongClick method of GalleryView by doing the following:

@Override
            public boolean onItemLongClick(AdapterView<?> arg0, View viu, int arg2,
                    long arg3) {
                Toast.makeText(GalleryView.this, "New item added to Favorites", Toast.LENGTH_SHORT).show();


                favsCount++;

               //checking to see if ViewStub is already inflated or not
                if(!stubvis){
                stub = (ViewStub) findViewById(R.id.stub1);
                stub.inflate();
                stubvis = true;
                trayUP = true;
                }

                return true;
            }

            });

and then in onPrepareOptionsMenu() I am adding the menu item based on the visibility of ViewStub. If inflated and visible, I create a menu item to hide it, otherwise, a menu item to show/make visible.

@Override

public boolean onPrepareOptionsMenu(Menu menu) {

menu.clear();


if(trayUP) {

menu.add(0, HIDETRAY, 0, "Hide Favorites Tray");

} else {

menu.add(0, SHOWTRAY, 0, "Show Favorites Tray");

}

return super.onPrepareOptionsMenu(menu);

}

Next, in onOptionsItemSelected(), I am writing the two cases based on the menu item selection. Case 1 when the tray is not visible, so I make it visible. Case 2 when it is visible, so I hide it by doing the following:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

        case HIDETRAY:

            Log.v(TAG, "Hiding Favs Tray");
            findViewById(R.id.stub1).setVisibility(View.GONE);
            trayUP = false;

        case SHOWTRAY:

            Log.v(TAG, "Showing Favs Tray");
            findViewById(R.id.stub1).setVisibility(View.VISIBLE);
            trayUP = true;

        }

    return true;

}

I know I am making a silly mistake somewhere. And my mind is too saturated to think straight at the moment. Need help :(

Thanks,

Ab

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小鸟爱天空丶 2024-12-20 20:58:59

一旦视图存根被膨胀,它将不再存在于视图层次结构中,它将被膨胀的内容所取代。因此,您只能膨胀视图存根,无法“缩小”它们或更改它们的可见性。

您可以为 ViewStub 定义一个膨胀的 ID,您可以使用它来引用膨胀的内容,然后使用 .setVisibility(GONE) 或 .setVisibility(VISIBLE) 来打开和关闭该视图。

布局示例

    <ViewStub
        android:id="@+id/stub_vTray"
        android:inflatedId="@+id/vTray"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout="@layout/custom_tray_stub" />

Java 示例

private Boolean mblnTrayInflated = false;
private Boolean mblnTrayVisible = false;

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case HIDETRAY:

        if (mblnTrayInflated && mblnTrayVisible)
        {
            findViewById(R.id.vTray).setVisibility(View.GONE);
            mblnTrayVisible = false;
        }

        case SHOWTRAY:

        if (!mblnTrayInflated)
        {
            ((ViewStub)findViewById(R.id.stub_vTray)).inflate();
            mblnTrayInflated = true;
            mblnTrayVisible = true;
        }
        else if (!mblnTrayVisible)
        {
            findViewById(R.id.vTray).setVisibility(View.VISIBLE);
            mblnTrayVisible = true;
        }

    }
    return true;
}

Once a view stub has been inflated it will no longer exist in the view hierarchy, it will have been replaced with the inflated contents. So you can only inflate view stubs, there is no way to "deflate" them or change their visibility.

You could define an inflated ID for the ViewStub which you can use to refer to the inflated contents, then use .setVisibility(GONE) or .setVisibility(VISIBLE) to switch that view on and off afterwards.

Example Layout

    <ViewStub
        android:id="@+id/stub_vTray"
        android:inflatedId="@+id/vTray"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout="@layout/custom_tray_stub" />

Example Java

private Boolean mblnTrayInflated = false;
private Boolean mblnTrayVisible = false;

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case HIDETRAY:

        if (mblnTrayInflated && mblnTrayVisible)
        {
            findViewById(R.id.vTray).setVisibility(View.GONE);
            mblnTrayVisible = false;
        }

        case SHOWTRAY:

        if (!mblnTrayInflated)
        {
            ((ViewStub)findViewById(R.id.stub_vTray)).inflate();
            mblnTrayInflated = true;
            mblnTrayVisible = true;
        }
        else if (!mblnTrayVisible)
        {
            findViewById(R.id.vTray).setVisibility(View.VISIBLE);
            mblnTrayVisible = true;
        }

    }
    return true;
}
七堇年 2024-12-20 20:58:59

Abhishek,看起来您正在尝试实现某种抽屉功能。如果您简单地使用 SlidingDrawer 类代替。

Abhishek, it looks like you're trying to implement some sort of drawer functionality. You might be able to get around all of this mess of setting views visibilities if you simply use the SlidingDrawer class instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文