希望这不是一个愚蠢的问题,在我的鼻子底下有一个答案,因为我花了很多时间尝试不同的东西来修复它,而且它终于到了很晚才能放弃这个问题,终于睡了一觉。
我正在构建一个示例应用来解决用户界面中的问题,以便稍后将其添加到真实的应用中。该应用程序将是带有listfragments的选项卡,在列表选择中,它将打开一个带有文本或图片的新片段。
一切都在画像中非常有效,但是当我尝试从任何标签或片段进行横向渲染时,我一直在崩溃。我正在责备标签,当它只是一个列表时,方向交换效果很好,然后我添加了标签,这个错误就开始出现了。
我显示片段的方式是通过替换xml中的片段,正如我所说它在标签出现之前工作正常。方向开关提示需要一个横向xml,我想问题是当方向改变时,选项卡没有正确地使用新布局重新加载。我仍然是超级新手,所以我知道使用onSaveInstanceState,但我不确定如何正确使用它,或者解决方案是否不同。
这是代码。
纵向XML
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_layout_support"
android:layout_width="match_parent"
android:layout_height="match_parent" >
***id.titles includes the listview, which needs to be replaced in portrait***
<fragment
android:id="@+id/titles"
android:layout_width="match_parent"
android:layout_height="match_parent"
class=".TitlesFragment" />
</FrameLayout>
格局XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:baselineAligned="false"
android:orientation="horizontal"
tools:context=".TitlesFragment" >
***listview on the left***
<fragment
android:id="@+id/titles"
android:name=".TitlesFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
***Details frame for fragments to inflate into on the right***
<FrameLayout
android:id="@+id/details"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" />
</LinearLayout>
ListFragment类(两个列表相似)
public class TitlesFragment extends SherlockListFragment implements ActionBar.TabListener {
boolean mDualPane;
int mCurCheckPosition = 0;
private Fragment mFragment;
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
setListAdapter (new ArrayAdapter<String> (getActivity(),
android.R.layout.simple_list_item_1, android.R.id.text1, ColorList.COLORS));
/* Checking if landscape or not. id.details is unique to landscape */
View detailsFrame = getActivity().findViewById(R.id.details);
mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;
if (savedInstanceState != null){
mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
}
if (mDualPane){
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
showDetails(mCurCheckPosition);
}
}
@Override
public void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
outState.putInt("curChoice", mCurCheckPosition);
/****** This SaveInstanceState did well at changing the orientation before the tabs were added.
* Is there a way to refresh the tabs on the new layout when the orientation changes? */
}
@Override
public void onListItemClick(ListView l, View v, int position, long id){
showDetails(position);
}
/* If Landscape, replace details. If not, replace titles, which includes the listview */
void showDetails(int position){
SherlockFragment newContent = new SherlockFragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
if(mDualPane){
switch(position){
case 0:
newContent = new Blue();
break;
case 1:
newContent = new Red();
break;
case 2:
newContent = new Green();
break;
case 3:
newContent = new Yellow();
break;
case 4:
newContent = new Purple();
break;
} ft.replace(R.id.details, newContent);
} else {
switch(position){
case 0:
newContent = new Blue();
break;
case 1:
newContent = new Red();
break;
case 2:
newContent = new Green();
break;
case 3:
newContent = new Yellow();
break;
case 4:
newContent = new Purple();
break;
}ft.replace(R.id.titles, newContent);
}
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
return;
}
/* Tab functionality */
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mFragment = new TitlesFragment();
ft.replace(R.id.titles, mFragment);
//ft.attach(mFragment);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
//ft.remove(mFragment);
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
感谢大家的帮助!我发布此消息后,我为不得不离开而道歉,但我会在睡了几个小时后回来。
答案 0 :(得分:0)
过去几天我遇到了太多麻烦,所以我放弃了标签界面并试图抽屉布局。