[英]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.