简体   繁体   English

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

[英]Android: forcing screen refresh on orientation change

I have an app utilizing Action Bar with Tab navigation which displays a listview via custom cursor adapter and then displays detail data when a listview item is clicked. 我有一个使用带选项卡导航的操作栏的应用程序,该应用程序通过自定义光标适配器显示一个列表视图,然后在单击一个列表视图项时显示详细数据。 I have implemented the logic to display dual pane when in landscape mode and parent/child views while in portrait mode. 我实现了在横向模式下显示双窗格并在纵向模式下显示父/子视图的逻辑。

My issue is that when I change orientation, my fragments stay the same until I select an item or change to a different tab. 我的问题是,当我改变方向时,片段会保持不变,直到我选择一个项目或更改为另一个选项卡为止。

IE: when I change from landscape to portrait, the dual pane displays in portrait mode. IE:当我从横向更改为纵向时,双窗格将以纵向模式显示。

My question is how do I force the fragment to reload? 我的问题是如何强制片段重新加载? I have experimented with onStop() and onResume(), but I'm not sure what method to call from there? 我已经尝试过onStop()和onResume(),但是不确定从那里调用什么方法?

Any help or suggestions would be greatly appreciated. 任何帮助或建议,将不胜感激。

Here is my ListFragment: 这是我的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: \\ 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" />

\\layout-land\\fragment_layout.xml: \\ layout-land \\ 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" />

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. 但是,如果您想对片段进行特定的更改,则可以从活动中的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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM