0

希望这不是一个愚蠢的问题,答案就在我的眼皮底下,因为我一直在花费很长时间尝试不同的方法来解决它,而现在已经太晚了,我要放弃这个问题并最终抓住一些睡觉。

我正在构建一个示例应用程序来解决 UI 中的问题,以便稍后将其添加到实际应用程序中。该应用程序将是带有列表片段的选项卡,并且在列表选择中它将打开一个带有文本或图片的新片段。

一切都在纵向上工作得很好,但是当我尝试从任何选项卡或片段进入横向时,我总是遇到崩溃。我责怪标签,当它只是一个列表时,方向交换效果很好,然后我添加了标签,这个错误开始出现。

我显示片段的方式是替换 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

}
}

谢谢大家的帮助!我很抱歉在我发布这篇文章后不得不离开,但我会在睡几个小时后回来。

4

1 回答 1

0

过去几天我遇到了太多麻烦,所以我放弃了选项卡界面并尝试了抽屉布局。

于 2013-09-17T20:15:37.483 回答