【问题标题】:Android - Cannot change layout using v4 FragmentsAndroid - 无法使用 v4 片段更改布局
【发布时间】:2013-01-28 08:41:14
【问题描述】:

我的用户应该能够在我的应用中更改(使用首选项)界面的外观(在视觉上动态启用和禁用某些布局)。

我正在使用 v4 ViewPager(确切地说是 PagerTabStrip)并尝试在 onResume() 方法中更改它,就在用户关闭“设置菜单”以返回应用程序之后。

我可以通过这种方式更改变量,但似乎无法更改任何布局方面的内容(我曾经使用 TabHosts,这非常有效)。如果我在 onCreateView() 方法中调用此方法,一切正常,但我不希望我的用户在任何更改可见之前强制关闭应用程序。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = null;
    vContainer = container; // global variable
    switch (mCurrentPage) {
    case 1:
        v = inflater.inflate(R.layout.lifepoints_layout,
                container, false);
        // irrelevant code
        InputStyleType = (LinearLayout) v
                .findViewById(R.id.llInputEditText);
        InputStyleButtons = (TableLayout) v
                .findViewById(R.id.tlInputButtons);

        InitializePreferences(); // <- this method
        break;
    case 2:
        // irrelevant code
        break;
    default:
        break;
    }
    return v;
}

这很完美,因为它应该。但是,当通过 onResume() 调用代码时,程序崩溃了!

@Override
public void onResume() {
    super.onResume();
    if (isPaused)
        InitializePreferences();
    isPaused = false;
} 

这是方法本身:

public void InitializePreferences() {
    SharedPreferences getPreferences = PreferenceManager
            .getDefaultSharedPreferences(getActivity().getBaseContext());
    prefInput = getPreferences.getBoolean("pref_input", true);
    prefFb = getPreferences.getBoolean("pref_fb", false);

    if (prefInput) {
        InputStyleType.setVisibility(View.GONE);
        InputStyleButtons.setVisibility(View.VISIBLE);
    } else {
        InputStyleButtons.setVisibility(View.GONE);
        InputStyleType.setVisibility(View.VISIBLE);
    }
}

我尝试了很多,但似乎没有任何效果。除非直接从 onCreateView 调用,否则我似乎无法更改任何布局,这似乎不对。

编辑 1

我已经完全分析了我的方法,它被正确调用并且我已经用一些变量进行了测试。一切正常,直到我尝试更改用户可见的内容(祝酒词工作得很好)。

编辑 3

已删除编辑 2,这是我的代码目前所在的位置。它仍然无法正常工作,实际上有点混乱。我确实认为答案并不遥远。

public void InitializePreferences() {
    SharedPreferences getPreferences = PreferenceManager
            .getDefaultSharedPreferences(getActivity().getBaseContext());
    prefInput = getPreferences.getBoolean("pref_input", true);
    prefFb = getPreferences.getBoolean("pref_fb", false);

    if (isPaused) {
        getActivity().getWindow().getDecorView().invalidate();

        LayoutInflater inflater = (LayoutInflater) getActivity()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.lifepoints_layout, vContainer,
                false);

        InputStyleType = (LinearLayout) v.findViewById(R.id.llInputEditText);
        InputStyleButtons = (TableLayout) v
                .findViewById(R.id.tlInputButtons);

        InputStyleType.invalidate();
        InputStyleButtons.invalidate();
    }

    if (prefInput) {
        InputStyleType.setVisibility(View.GONE);
        InputStyleButtons.setVisibility(View.VISIBLE);
    } else {
        InputStyleButtons.setVisibility(View.GONE);
        InputStyleType.setVisibility(View.VISIBLE);
    }
}

【问题讨论】:

  • 请附上错误的日志猫

标签: android layout android-fragments visibility oncreate


【解决方案1】:

我认为您需要使您的观点无效。尝试将 InputStyleType.invalidate()InputStyleButtons.invalidate() 添加到 InitializePreferences 方法。您可能还需要使包含 InputStyleType 和 InputStyleButtons 的视图父级无效。

编辑:在 onResume() 回调中试试这个:

LayoutInflater inflater = getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.lifepoints_layout,
            container_2, false);

为了完成这项工作,请创建一个全局 ViewGroup 变量并将其值设置为 onCreateView 回调中的 container 的值。

container_2 = container;

【讨论】:

  • 我已经添加了代码,但它似乎不起作用。由于我不熟悉这段特定的代码,我自己可能做错了什么。
  • 嗯,我大约 99% 确定您的问题是因为视图没有被更新。尝试在 onResume() 方法中重新膨胀视图(如在 onCreateView() 中)。看看这是否能为您解决问题。
  • 你能举个例子吗?因为我还在挣扎。也许用上面的代码?无论如何,谢谢帮助
  • 不客气。好的。我举了一个例子来说明我在说什么。
  • 你还有同样的问题吗?另外,您是否尝试过像我的示例中那样重新膨胀布局?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-13
  • 2016-10-01
  • 1970-01-01
  • 2018-05-28
相关资源
最近更新 更多