【问题标题】:When rotate the landscape to portrait in fragment UI is not refreshing在片段 UI 中将横向旋转为纵向时不刷新
【发布时间】:2015-12-10 16:55:36
【问题描述】:

当我将设备横向旋转为纵向或纵向旋转为横向时,设备不会刷新 UI。我正在使用片段“onCreateView”和“onConfigurationChanged”来绘制布局,remote_gesture 和 remote_navigation 有纵向和横向两种布局。最后将android:configChanges="orientation|screenSize|keyboardHidden"添加到AndroidManifest.xml

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment


    //View rootView = (View)inflater.inflate(R.layout.remote_navigation, container, false);

    View rootView = (View)inflater.inflate(R.layout.fragment_remote, container, false);
    viewSwitcher =(ViewSwitcher)rootView.findViewById(R.id.remote_viewswitcher);
    remoteGesture = (View)inflater.inflate(R.layout.remote_gesture, null);
    remoteNav = (View)inflater.inflate(R.layout.remote_navigation, null);
    viewSwitcher.addView(remoteNav);
    viewSwitcher.addView(remoteGesture);
    setupButtons(rootView);
    return rootView;
}

@Override
public void onConfigurationChanged(Configuration newConfig) {


    super.onConfigurationChanged(newConfig);

    Log.w(TAG, "rrr... onConfigurationChanged...");
    ViewGroup container = (ViewGroup) getView();
    //container.removeAllViewsInLayout();
    LayoutInflater inflater = (LayoutInflater) getActivity().getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rootView = (View)inflater.inflate(R.layout.fragment_remote, container, false);
    viewSwitcher = (ViewSwitcher) rootView.findViewById(R.id.remote_viewswitcher);
    remoteGesture = (View) inflater.inflate(R.layout.remote_gesture, null);
    remoteNav = (View)inflater.inflate(R.layout.remote_navigation, null);
    viewSwitcher.addView(remoteNav);
    viewSwitcher.addView(remoteGesture);
}

我该如何解决这个问题?

【问题讨论】:

    标签: android android-fragments


    【解决方案1】:

    onConfigurationChanged() 上,删除视图在添加新视图之前。这就像强制刷新。示例代码:

    viewSwitcher.removeAllViews()
    

    方法的定义@ViewGroup removeAllViews()

    另一个类似的示例代码,通用版本:

    public void onConfigurationChanged(Configuration newConfig) {
       super.onConfigurationChanged(newConfig);
    
       // Refresh views by removing first
       ViewGroup viewGroup = (ViewGroup) getView(); 
       viewGroup.removeAllViewsInLayout();
       ...
    }
    

    【讨论】:

    • 但这不是重绘布局,只是删除它。
    • @user3480706,如果您只想删除,请不要调用 addView()。但是在您的帖子中,它说您要刷新 UI。所以我说要删除现有视图,然后添加它们。
    • 我只是取消注释行 container.removeAllViewsInLayout();并运行代码但它不刷新 UI 可以给我步骤吗?
    • @user3480706,我从未使用过 ViewSwitcher。在您的情况下,我认为您需要调用 viewSwitcher.removeAllViews() ,因为 viewSwitcher 是添加视图的 View 对象。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多