2013-09-16 33 views
0

我有一個應用程序利用操作欄與選項卡導航,它顯示通過自定義遊標適配器的列表視圖,然後顯示單擊listview項時的詳細數據。我已經實現了在橫向模式下顯示雙窗格和在縱向模式下顯示父/子視圖的邏輯。Android:強制屏幕刷新方向更改

我的問題是,當我改變方向時,我的片段保持不變,直到我選擇一個項目或更改爲不同的選項卡。

IE:當我從風景改變爲肖像時,雙窗格以縱向模式顯示。

我的問題是我如何強制片段重新加載?我嘗試過使用onStop()和onResume(),但我不確定從那裏調用什麼方法?

任何幫助或建議將不勝感激。

這裏是我的ListFragment:

public class ItemListFragment extends ListFragment { 

// initialize variables 
private boolean mDualPane; 
private int mCurCheckPosition = 0; 
private DataAdapter db; 
private Cursor cursor; 
private MyListAdapter items; 

// methods 
@Override 
public View onCreateView(LayoutInflater inflater, 
     ViewGroup container, Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.fragment_layout, container, false); 
} 

@Override 
public void onActivityCreated(Bundle savedState) { 
    super.onActivityCreated(savedState); 

    // create a database connection 
    db = new DataAdapter(getActivity()); 
    db.open(); 
    cursor = db.getAllRecords(); 

    // get the filter EditText view 
    filterText = (EditText) getActivity().findViewById(R.id.search_box); 
    filterText.addTextChangedListener(filterTextWatcher); 

    // set the parameters for the ListAdapter 
    String[] from = new String[] { DataAdapter.KEY_TITLE }; 
    int[] to = new int[] {R.id.item_title }; 

    // Now create an array adapter and set it to display using our row 
    items = new MyListAdapter(getActivity(), R.layout.item_cell, cursor, from, to); 

    // get the listview 
    ListView listview = getListView(); 
    listview.setAdapter(items); 

    // check if we need dual pane 
    mDualPane = isLandscape(); 

    if (savedState != null) { 
     // Restore last state for checked position. 
     mCurCheckPosition = savedState.getInt("curChoice", 0); 
    } 

    if (mDualPane) { 
     // getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
     items.setSelectedPosition(mCurCheckPosition, cursor); 
    }  
} 

\佈局\ Fragment_layout.xml:

<?xml version="1.0" encoding="utf-8"?> 

<ListView 
    android:id="@android:id/list" 
    android:layout_width="match_parent" 
    android:layout_height="0dip" 
    android:layout_weight="1" /> 

\佈局,土地\ fragment_layout.xml:

<?xml version="1.0" encoding="utf-8"?> 

<ListView 
    android:id="@android:id/list" 
    android:layout_width="match_parent" 
    android:layout_height="0dip" 
    android:layout_weight="1" /> 

<FrameLayout 
    android:id="@+id/details" 
    android:layout_width="0px" 
    android:layout_height="match_parent" 
    android:layout_weight="1" /> 

回答

0

首先你需要在你的清單一些配置的;

<activity 
     android:name="yourActivity" 
     android:configChanges="orientation|screenSize"  
     android:screenOrientation="sensor" > 
    </activity> 

實際上,此配置會自動處理橫向和縱向模式。但是,如果您想要對片段進行特定更改,則可以在活動中通過onConfigurationChanged方法觸發片段的方法。

public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 

    // Checks the orientation of the screen 
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { 
     //TODO with Landscape 
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { 
     //TODO with Portrait 
    } 
} 
+0

感謝您的回覆。我將額外的屬性添加到我的清單中,但我仍然得到相同的行爲。我應該在onConfigurationChanged方法中強制重新加載片段? – wyoskibum