我有一个Android应用程序(在平板电脑上)使用主 - 详细信息流来显示左侧的预测列表和右侧的详细信息。我希望能够在应用程序加载时选择第一个项目。
我在onLoadFinished
方法中设置了用于设置所选索引的代码。我实现了这个,因为有时候用户会选择一个项目,如果他们改变了方向,它就会离开屏幕。我使用以下方法确保一旦加载,再次选择正确的项目:
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mForecastAdapter.swapCursor(data);
// If we have a selected index - select it.
if(mSelectedIndex != ListView.INVALID_POSITION) {
mForecastListView.setSelection(mSelectedIndex);
}
}
我尝试添加一个检查是否正在使用平板电脑的else语句,以选择第一项。代码已执行,但它似乎发生在listview实际加载并且未选择该项目之前。应用程序打开后,我应该在哪里放置代码来选择第一项?
修改 这是我尝试过的方法,没有运气:
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mForecastAdapter.swapCursor(data);
// If we have a selected index - select it.
if(mSelectedIndex != ListView.INVALID_POSITION) {
mForecastListView.setSelection(mSelectedIndex);
} else if(mForecastAdapter.IsTwoPane()){
mForecastListView.setSelection(0);
}
}
答案 0 :(得分:1)
我会这样做,我已经为列表视图做了一般,也应该使用游标。
在适配器中,为选定位置创建一个字段
public int selectedPos = 0;//use setter/getter eventually
现在确保listView的OnItemClickListener / OnItemSelectedListener执行以下操作:
<YOUR_ADAPTER_INSTANCE>.selectedPos = positionSelected;
最后,在适配器的getView()
中,执行以下操作:
if(position == selectedPos) {
//do what you want to show selection
} else {
//do what you want for default state of the list items
}
这将确保在第一次显示列表时,selectedPos将为0,因此将根据上面选择第一个位置。
如果您想通过orientationChange和喜欢的方式保留用户的选择,您只需要确保在“恢复”后将信息传递给适配器。
答案 1 :(得分:1)
如果您因配置更改而尝试恢复列表视图,可能保存状态会有帮助吗?例如:
private Parcelable mForceastListViewSavedState;
private int mForecastListViewPositionSavedState;
private static final String sForecastListViewSavedStateKey = "ForecastListViewSavedState";
private static final String sForecastListViewPositionSavedState = "ForecastListViewPositionSavedState";
@Override
public void onSaveInstanceState(Bundle outState) {
if(mForecastListView != null){
outState.putParcelable(sForecastListViewSavedStateKey, mForecastListView.onSaveInstanceState());
outState.putInt(sForecastListViewPositionSavedState, mForecastListView.getFirstVisiblePosition());
}
super.onSaveInstanceState(outState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
...
//Restore InstanceState
if (savedInstanceState != null){
mForceastListViewSavedState = savedInstanceState.getParcelable(sForecastListViewSavedStateKey);
mForecastListViewPositionSavedState = savedInstanceState.getInt(sForecastListViewPositionSavedState);
}
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mForecastAdapter.swapCursor(data);
//Restore ListView State
if (mForceastListViewSavedState != null){
mForecastListView.onRestoreInstanceState(mForceastListViewSavedState);
mForecastListView.setSelection(mForecastListViewPositionSavedState);
}
}
答案 2 :(得分:1)
我解决了这个问题,虽然这个问题已经过时了。我的解决方案是:
@Override
public void onLoadFinished(Loader<Cursor> cursorLoader,Cursor cursor){
mForecastAdapter.swapCursor(cursor);
if(mPositon!=ListView.INVALID_POSITION){
mListView.smoothScrollToPosition(mPositon);
}else if(mTwoPane){
{
new Handler().post(new Runnable() {
@Override
public void run() {
mListView.performItemClick(mListView, 0, mListView.getAdapter().getItemId(0)); }
});
}
}
}