如何管理方向变化的两个片段?

时间:2017-02-10 10:04:05

标签: android android-fragments

我有一个活动che包含一个片段,我称之为片段A. 当屏幕足够大时,(布局大)A显示另外两个片段,B(产品列表)和C(列表中所选项目的详细信息)。 当屏幕不够大时,A只显示片段B,当我点击某个列表项时,它会打开包含所选产品的片段C.

问题在于,在纵向模式下,片段C是可见的(片段vith细节),如果我改变方向,它返回可见片段B(列表),但我想保留片段C可见,在横向模式。

我怎样才能做到这一点?

以下是一些代码:

大屏幕

    <?xml version="1.0" encoding="utf-8"?>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/book_list_content"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

    <FrameLayout
        android:id="@+id/book_details_content"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="3"/>

</LinearLayout>

普通屏幕

    <?xml version="1.0" encoding="utf-8"?>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/book_list_content"
        android:layout_below="@id/tool_bar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"/>


</LinearLayout>

最后片段A:

public class HomeFragment extends Fragment implements ListFragment.onBookSelectedListener {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedIstance){
        return inflater.inflate(R.layout.drawer_fragments,container,false);
        /*drawer_fragments is the name of the layout that i've posted above*/
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState){
        if(savedInstanceState == null) {
            ListFragment listfrag = new ListFragment();
            FragmentManager fragManager = getChildFragmentManager();
            fragManager.beginTransaction().replace(R.id.book_list_content, listfrag).commit();
            if (view.findViewById(R.id.book_details_content) != null) {
                    /*Here if the screen is large enought to see also the details*/
                fragManager.beginTransaction().replace(R.id.book_details_content, new DetailsFragment()).commit();
            }
        }

    /*Other code...*/  

    }

1 个答案:

答案 0 :(得分:0)

在你的Fragment_One

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        frameLayout = new FrameLayout(getActivity());
        rootView = inflater.inflate(R.layout.fragment_home_page_updated, null);
        frameLayout.addView(rootView);
        initUI();

        return frameLayout;
    }

并使onConfigurationChange()

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        setRetainInstance(true);
        frameLayout.removeAllViews();
        LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rootView = inflater.inflate(R.layout.fragment_home_page_updated, null);
        frameLayout.addView(rootView);
        initUI();
    }