0
我有一個活動che包含一個片段,我稱之爲片段A. 當屏幕足夠大時,(layout-large)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>
而在最後的片段答:
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...*/
}
它的行爲以同樣的方式,也許我已經把裏面的東西initUI特別?初始化initUI()中的 – Ollaw
,初始化所有TextView,Button和全部。 – Shekhar