使用onConfigurationChanged在一个片段 [英] Using onConfigurationChanged in a fragment

查看:161
本文介绍了使用onConfigurationChanged在一个片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个code在片段

I have this code in a fragment

public class TestOne extends Fragment {

    View view = null;

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);

      LayoutInflater inflater2 = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater2.inflate(R.layout.testone, null); 

        Toast.makeText(getActivity(), "Rotate fragment", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Toast.makeText(getActivity(), "onCreate Fragment", Toast.LENGTH_SHORT).show();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.testone, null); 

        Toast.makeText(getActivity(), "onCreateView fragment", Toast.LENGTH_SHORT).show();

        return view; 
    }

}

我想要做的是,当我旋转手机,我不希望这些方法再次执行。但我想再次调用XML布局,装载布局,土地文件夹中的XML。

What I'm trying to do is that, when I rotate the phone, I don't want the methods to be executed again. But I want to call again the xml layout, to load the layout-land folder's xml.

这code没有给出任何错误,只是不工作,不明白其中的道理。

This code does not give any error, just does not work and do not understand the reason ..

我在做它用onConfiguratonChanged真正感兴趣

I'm really interested in doing it using onConfiguratonChanged

我AP preciate任何帮助。

I appreciate any help.

感谢和问候

推荐答案

在onCreateView创建的FrameLayout - 这是容器您片段视图。然后创建您的R.layout.testone并将其添加到框架布局。

In onCreateView create FrameLayout - this is container for you fragment view. Then create your R.layout.testone and add it to frame layout.

在onConfigurationChanged明确的FrameLayout,再次创建R.layout.testone并将其添加到框架布局。

In onConfigurationChanged clear FrameLayout, create R.layout.testone again and add it to frame layout.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
{
    frameLayout = new FrameLayout(getActivity());

    LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = inflater.inflate(R.layout.testone, null);

    frameLayout .addView(view);

    return frameLayout; 
}

@Override
public void onConfigurationChanged(Configuration newConfig) 
{
    super.onConfigurationChanged(newConfig);

    frameLayout. removeAllViews();

    LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = inflater.inflate(R.layout.testone, null);

    frameLayout .addView(view);
}

现在所有的工作意愿,只要你想!

Now all will works as you want!

这篇关于使用onConfigurationChanged在一个片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆