【问题标题】:Android: forcing screen refresh on orientation changeAndroid:在方向更改时强制屏幕刷新
【发布时间】:2013-09-16 21:39:20
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: android android-listview android-fragments


    【解决方案1】:

    首先,您需要在清单上进行一些配置;

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

    【讨论】:

    • 感谢您的回复。我在清单中添加了其他属性,但我仍然得到相同的行为。我应该在 onConfigurationChanged 方法中添加什么来强制片段重新加载?
    猜你喜欢
    • 2013-09-16
    • 1970-01-01
    • 1970-01-01
    • 2014-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多