我什么时候必须膨胀自定义视图的布局?

发布于 2024-12-23 02:12:20 字数 240 浏览 6 评论 0原文

我制作了一个自定义视图,用于视图分页器的多个片段中。

视图始终存储为 Fragment 的字段。我在构造函数中膨胀了布局,并且翻阅页面一切正常。因为总是创建下一个片段,所以视图显示没有任何问题。但是,如果我返回,则会重新创建片段的已销毁视图,并且它会给我一个错误,因为我仍然使用相同的自定义视图,该视图没有膨胀,因为没有调用构造函数。

那么,什么时候我必须膨胀自定义视图的布局?我不认为代码可以解决任何问题,但如果需要的话我可以提供。

I made a custom view which is used in several Fragments of an viewpager.

The view is always stored as a field of the Fragment. I inflate the layout in the constructor and everything works fine flipping through the pages. Because always the next Fragment is created the view shows up without any problem. But if I go back a destroyed View of a Fragment is recreated and it gives me an error, because I still use the same custom view, which isn't inflated, because the constructor isn't called.

So, when do I have to inflate my layout of the custom view? I don't think code would clear anything up, but I can deliver if needed.

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

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

发布评论

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

评论(2

她比我温柔 2024-12-30 02:12:20

在此函数中:

@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.game_menu, menu);
    return true;
}

参考是此处

In this function:

@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.game_menu, menu);
    return true;
}

The reference is HERE

守护在此方 2024-12-30 02:12:20

我有一个包含视图的类DynamicRowView。您可以创建一个类并在主文件中调用该类:

public class DynamicRowView
{
    public View getView(int layout_id,Activity context)
    {
        View view = null;
        view = context.getLayoutInflater().inflate(layout_id, null);
        return view;
    }
}

并将其写入您想要View的主类中:

dynamic_row_view = new DynamicRowView();

View view = dynamic_row_view.getView(R.layout.row_menu_list, MenuListingPage.this);

I have a class DynamicRowView which contains view. You can create a class and call the this class in your main file:

public class DynamicRowView
{
    public View getView(int layout_id,Activity context)
    {
        View view = null;
        view = context.getLayoutInflater().inflate(layout_id, null);
        return view;
    }
}

and write this in your main class where you want the View:

dynamic_row_view = new DynamicRowView();

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