片段和onConfigurationChanged片段、onConfigurationChanged

2023-09-06 01:18:28 作者:把愚勇熬成温柔

我试图做一些我做的活动,而是一个片段中。 使用活动我要做的就是:

I'm trying to do something I do with activities, but within a fragment. What I do is using activities:

第一旋转设备时停止该活动将重新启动 安卓configChanges =keyboardHidden |方向|屏幕尺寸

First stop the activity restarts when rotating the device android:configChanges="keyboardHidden|orientation|screenSize"

在我的活动添加:

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

 setContentView(R.layout.main);
}

因此​​,获得活动没有重新启动,但重装main.xml中,使用的布局,土地

So get the activity does not restart, but reloading the main.xml, to use the layout-land

现在我有一个活动显示viewpager,其中包含三个片段。 一切工作正常。检测的旋转是在所述片段

Now I have an activity showing viewpager, which contains three fragments. Everything works properly. Detection of the rotation is in the fragments

public class FRG_map_web extends Fragment  {

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

        Log.i("myLogs", "Rotation");

    }

的问题是,所述片段不使用的setContentView(R.layout.main);这是code:

The problem is that the fragment not use setContentView(R.layout.main); this is the code:

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

我试图用:

LayoutInflater inflater = inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

view = inflater.inflate(R.layout.frg.myFragment, null); 

...

LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

view = inflater.inflate(R.layout.frg.myFragment, null); 

...

LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );

view = inflater.inflate(R.layout.frg.myFragment, null); 

...

LayoutInflater li = LayoutInflater.from(context);

和不同的方式,但始终没有成功 我不能夸大正确。

and different ways, but always without success I can not inflate properly.

谁能告诉我,我怎么也得办?

Can anyone tell me how I have to do?

在此先感谢,我AP preciate帮助

Thanks in advance, I appreciate the help

问候

推荐答案

如果我理解正确的话,你不想给片段重新加载每个旋转。相反,你要重新布局的意见,并与已经掌握的信息进行更新。

If I understand correctly, you don't want to the fragment to reload on each rotation. Instead you want to relayout the views and update them with information you already have.

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

    // Get a layout inflater (inflater from getActivity() or getSupportActivity() works as well)
    LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View newView = inflater.inflate(R.layout.frg.myFragment, null);
    // This just inflates the view but doesn't add it to any thing.
    // You need to add it to the root view of the fragment
    ViewGroup rootView = (ViewGroup) getView();
    // Remove all the existing views from the root view.
    // This is also a good place to recycle any resources you won't need anymore
    rootView.removeAllViews();
    rootView.addView(newView);
    // Viola, you have the new view setup
}

根据该文件(getView()), getView()返回从您的onCreateView()返回了同样的观点,但事实并非如此。实际上,它回报你onCreateView(返回视图的父级),这正是你所需要的。getView()将返回NoSaveStateFrameLayout实例,这是专门用于片段作为其根图。

According to the documentation (getView()), getView() returns the same view that you returned from your onCreateView() but it does not. It actually return the parent of the view you returned in onCreateView() which is exactly what you need. getView() will return an instance of NoSaveStateFrameLayout, which is used specifically for Fragments as its root view.

希望这有助于。