当前位置: 首页 > 知识库问答 >
问题:

在方向改变时保持片段的状态,但替换其布局

堵泽宇
2023-03-14

我试过configChanges,但显然它没有改变我的布局。我试着在onConfigurationChanged的FrameLayout容器中填充肖像和景观布局并替换它们,但似乎也不起作用。

如何处理需要加载不同布局同时保持片段状态不变的情况?这有可能吗?对于这种事情,正确的做法是什么?

共有1个答案

吴镜
2023-03-14

我有这样的情况,通过setLayout方法在改变屏幕方向上加载不同的布局;

public class SampleFragment extends Fragment{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mRoot = inflater.inflate(R.layout.fragment_layout, null);
    return mRoot;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    this.init();
}

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

private void init() {
    this.setLayout();
}

private void setLayout() {
    View view = null;
    int orientation = mResources.getConfiguration().orientation;
    if (orientation == Configuration.ORIENTATION_LANDSCAPE)
        view = mInflater.inflate(R.layout.segment_dettaglio_evento_land, null);
    else if (orientation == Configuration.ORIENTATION_PORTRAIT)
        view = mInflater.inflate(R.layout.segment_dettaglio_evento, null);

    if (orientation == Configuration.ORIENTATION_LANDSCAPE || orientation == Configuration.ORIENTATION_PORTRAIT) {
        ViewGroup viewGroup = (ViewGroup) mRoot.findViewById(R.id.dettaglio_evento_root);
        viewGroup.removeAllViews();
        viewGroup.addView(view, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        this.initComponent();
    }
}

private void initComponent() {
 if (mResult == null)
        retrieveData();
    else
        populateWithDetail();
}

}

fragment_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/dettaglio_evento_root"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@color/dettaglio_evento_background"
  android:orientation="vertical" >
</LinearLayout>

我还有一个变量mResult,它包含从网络请求中保留的数据,所以如果它为null,我会发出请求,否则填充视图。

 类似资料:
  • activity_main_two.xml https://github.com/rjrogers/twopaneuitest/blob/master/app/src/main/res/layout/activit_main_two.xml fragment_steps.xml https://github.com/rjrogers/twopaneuitest/blob/master/app/sr

  • 我正在通过片段“A”调用从服务器获取数据。当我用“B”替换“A”时,在从“B”返回到“A”之后,每次都会调用片段“A”,因此每次都会生成HTTPGET。如何避免这种情况,并在活动中重用类似REORDER_to_FRONT的片段? 我正在使用此代码替换为新片段 当我反压时,

  • 我一直在尝试在我的应用程序中动态替换一个片段,但这样做时,我一直会遇到RuntimeException,因为它找不到应该添加片段的容器ID。我尝试过在类似线程上找到的每一个解决方案,从干净的构建到使用ChildFragmentManager,但仍然有相同的错误。最奇怪的是,当我在onCreate中没有指定任何容器时,一切都很顺利,但一旦指定了容器,应用程序就会崩溃。以下是代码和堆栈跟踪: 堆栈跟踪

  • 视图(在本例中,ViewPager)有自己的保存和恢复状态的实现,我依靠它在设备旋转后将ViewPager保持在同一页面上。 当ViewPager在活动中时,它工作得非常好。我滚动到一个页面,旋转设备,它仍然在那个页面上。在SaveInstanceState中不需要任何代码。 我已经把那个活动转化成了一个片段。现在,ViewPager无法保持其状态!如果我滚动到一个页面并旋转设备,它会返回到第1页

  • 我正在尝试创建一个计算器,在肖像模式下有一个简单的版本,在景观中有一个科学版本旁边有一个过去计算的列表。我遇到了将肖像模式的textview中的当前值传递给横向模式的问题。 我的主要activity: 我尝试在我的ScientificFragment类中使用onSaveInstanceState包来承载它 但我最后有两个单独的保存包,一个用于肖像,一个用于风景。

  • null 我想保留的最后一个查看页面索引,当活动因任何原因重新创建时,例如方向改变。 位于附加到活动的(名为)中。我使用的是compat库,因此该片段是的子类。 我认为可以通过重写方法并在中添加适当的逻辑来完成,如文档中所述: 为了正确地处理重启,重要的是您的活动通过正常的活动生命周期恢复其以前的状态,在正常的活动生命周期中,Android在破坏您的活动之前调用onSaveInstanceStat