Android:强制屏幕刷新方向更改

时间:2013-09-16 13:24:55

标签: android android-listview android-fragments

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

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

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

1 个答案:

答案 0 :(得分: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
    }
}
相关问题