2个片段并排的问题

时间:2014-12-17 00:02:17

标签: android listview android-fragments

以下是我的简要介绍。 我有两个片段显示在水平方向(listview和detailview)时和单个片段在垂直方向(listview)时。 现在,当我的应用开始我希望时,如果有 savedInstanceState则显示列表视图,并在 A {时显示详情视图{1}}。您可以在Android默认电子邮件应用中看到此操作,无论在什么方向,它都会保留在电子邮件中。

发生了什么事情,它会在旋转到垂直时显示savedInstanceState。 这是我第一次发帖,我希望能得到一些帮助,我是新手。

listview

1 个答案:

答案 0 :(得分:0)

    boolean detailPage = false;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    setContentView(R.layout.ticket_view);

    if(savedInstanceState == null) {
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ListFragmentP listFragment = new ListFragmentP();
        ft.add(R.id.displayList, listFragment, "List_Fragment");
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.commit();
    }


    if(findViewById(R.id.displayDetail) != null){
        Log.d("tap","displayDetail found");
        detailPage = true;
        getFragmentManager().popBackStack();
        DetailFragmentP detailFragment = (DetailFragmentP) getFragmentManager().findFragmentById(R.id.displayDetail);

        if(detailFragment == null){
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            detailFragment = new DetailFragmentP();
            ft.replace(R.id.displayDetail, detailFragment, "Detail_Fragment1");
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            ft.commit();
        }
    }else{

        if(savedInstanceState != null) {
            SQLiteDatabase db = openOrCreateDatabase("acsi2000.db",android.content.Context.MODE_PRIVATE ,null);
            Cursor page_cursor = db.rawQuery("SELECT * FROM tickets WHERE Order_No='" + DetailFragmentP.Order_No + "'",null);
            DetailFragmentP detailFragment = new DetailFragmentP();
            detailFragment.setSMSOContent(page_cursor);
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.replace(R.id.displayList, detailFragment, "Detail_Fragment2");
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            ft.addToBackStack(null);
            ft.commit();


        }

    }
}
@Override
public void onSelected(String SMSO) {
    SQLiteDatabase db = openOrCreateDatabase("acsi2000.db",android.content.Context.MODE_PRIVATE ,null);
    Cursor page_cursor = db.rawQuery("SELECT * FROM tickets WHERE Order_No='" + SMSO + "'",null);
    if(detailPage){
        DetailFragmentP detailFragment = (DetailFragmentP) getFragmentManager().findFragmentById(R.id.displayDetail);
        detailFragment.updateSMSOContent(page_cursor);
    }else{
        DetailFragmentP detailFragment = new DetailFragmentP();
        detailFragment.setSMSOContent(page_cursor);
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.displayList, detailFragment, "Detail_Fragment2");
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.addToBackStack(null);
        ft.commit();
    }
}

看起来我需要在R.id.displayDetail检查中添加一个else语句,然后检查已保存的实例并执行与我选择的相同类型的代码。

感谢您的帮助,我希望这有助于其他人。