0

我有一个使用带有选项卡导航的操作栏的应用程序,它通过自定义光标适配器显示列表视图,然后在单击列表视图项时显示详细数据。我已经实现了在横向模式下显示双窗格和在纵向模式下显示父/子视图的逻辑。

我的问题是,当我改变方向时,我的片段保持不变,直到我选择一个项目或更改为不同的选项卡。

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);
    }       
}

\layout\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" />

4

1 回答 1

0

First of all you need some configurations on your manifest;

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

actually this congfiguration handles landscape and portrait mode automatically. But if you want to specific changes in your fragments, you can trigger fragment's methods from onConfigurationChanged method in your activity.

    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
    }
}
于 2013-09-16T13:39:20.933 回答