簡體   English   中英

如何管理方向改變的兩個片段?

[英]How to manage two fragments on orientation change?

我有一個活動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...*/  

    }

在您的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();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM