我创建了一个新的应用程序,依靠默认的"Master Detail Flow"模板,在Android Studio的" new Project…"对话框中选择。然后,我对应用程序进行了调整,以满足我的需求和数据。在手持设备和平板电脑上很有魅力。
我唯一想要实现的是自动选择列表中的第一项,显示详细视图并将列表项设置为选中。
在"ItemListFragment"中,我在覆盖的"onStart()"方法中调用了"onlisttitemclick(…)"方法。它有以下行为:
- 当旋转设备时,列表中的第一个项目被选中(而不是先前选中的一个)
- 列表项没有在列表 中被标记为已选
有人能告诉我如何实现这一目标的正确方向吗?因为我只希望这种行为在"平板模式"我想我必须把它放到ItemListActivity?
感谢编辑:这是我的"ItemListActivity"的"onCreate"方法
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_list);
if (findViewById(R.id.item_detail_container) != null) {
mTwoPane = true;
((ItemListFragment) getFragmentManager()
.findFragmentById(R.id.item_list))
.setActivateOnItemClick(true);
}
}
您可以通过删除ItemListFragment
的onStart(...)
中的onListItemClick(...)
调用,并将以下代码段添加到setActivateOnItemClick(...)
的末尾来解决此问题:
// If on dual-pane view, pre-select the first item from the chapter list
if (activateOnItemClick && mActivatedPosition == ListView.INVALID_POSITION) {
getListView().performItemClick(getListView(), 0, getListView().getItemIdAtPosition(0));
}
同样,如果onListItemClick(...)
中没有这一行代码,在ItemListFragment
中添加这一行代码:
mActivatedPosition = position;
请检查昨天在我的GH仓库上提交的ChapterListFragment
文件上的差异,以获得更多关于我的解决方案的上下文- https://github.com/floydpink/BhagavadGita/compare/047de35c39f01c6341a7f677aa5b7cc47a7144a3...b1b45d15cd68290e2ebe909ca920bd7aefd2805e#diff-1
希望能有所帮助