【发布时间】:2011-11-03 14:10:25
【问题描述】:
我正在尝试基于 Android 文档末尾提供的关于 Fragments 的example code 构建一个拆分窗格,基本上是一个包含两个片段的 LinearLayout(所有详细信息都可以在上面的链接中找到):
-
TitlesFragment显示标题列表 -
DetailsFragment显示当前选中标题的详细信息
我的问题是发生了以下意外行为:
- 我以横向方向启动应用程序:左侧显示标题列表,右侧显示当前所选标题的详细信息(开头的第一个)。李>
- 我从标题列表中选择item X:由于 ListView 设置为
CHOICE_MODE_SINGLE并且其项目实现了Checkable接口,因此标题变得突出显示。 - 我切换到 纵向 方向(通过旋转设备或模拟器):正如预期的那样,仅显示标题列表并且在此配置中未选择任何项目,因为重新创建了 ListView 并且通过默认设置为
CHOICE_MODE_NONE。 - 我从标题列表中选择item Y:正如预期的那样,一个活动只显示了与所做选择相对应的
DetailsFragment。 - 我切换回横向方向(这是发生意外行为的地方):右侧的
DetailsFragment正确显示了item Y的详细信息(已选中纵向),但TitlesFragment仍将item X显示为选中状态(即在第一次屏幕旋转之前选中的那个)。
当然,我希望在纵向方向上所做的最后一个选择也正确显示在 TitlesFragment 中,否则我最终会出现不一致的突出显示,在显示项目 Y 的详细信息时显示项目 X 已被选中。
我发布了更改ListView模式的相关代码(在'onActivityCreated'内)并设置了所选项目(在'showDetails'方法内):
public static class TitlesFragment extends ListFragment {
boolean mDualPane;
int mCurCheckPosition = 0;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Populate list with our static array of titles.
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_activated_1, Shakespeare.TITLES));
// Check to see if we have a frame in which to embed the details
// fragment directly in the containing UI.
View detailsFrame = getActivity().findViewById(R.id.details);
mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;
if (savedInstanceState != null) {
// Restore last state for checked position.
mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
}
if (mDualPane) {
// In dual-pane mode, the list view highlights the selected item.
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
// Make sure our UI is in the correct state.
showDetails(mCurCheckPosition);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("curChoice", mCurCheckPosition);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
showDetails(position);
}
/**
* Helper function to show the details of a selected item, either by
* displaying a fragment in-place in the current UI, or starting a
* whole new activity in which it is displayed.
*/
void showDetails(int index) {
mCurCheckPosition = index;
if (mDualPane) {
// We can display everything in-place with fragments, so update
// the list to highlight the selected item and show the data.
getListView().setItemChecked(index, true);
// Check what fragment is currently shown, replace if needed.
DetailsFragment details = (DetailsFragment)
getFragmentManager().findFragmentById(R.id.details);
if (details == null || details.getShownIndex() != index) {
// Make new fragment to show this selection.
details = DetailsFragment.newInstance(index);
// Execute a transaction, replacing any existing fragment
// with this one inside the frame.
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.details, details);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
} else {
// Otherwise we need to launch a new activity to display
// the dialog fragment with selected text.
Intent intent = new Intent();
intent.setClass(getActivity(), DetailsActivity.class);
intent.putExtra("index", index);
startActivity(intent);
}
}
有人知道如何解决此类问题吗? 提前致谢。
【问题讨论】:
-
我知道这有点晚了,但我的行为完全相同。我正在真实设备中测试代码,即安装了 API 16 的平板电脑。我放了踪迹,可以看到在
showDetails(index)中,索引是正确的,但setItemChecked(index, true)没有突出显示正确的标题。
标签: android