返回ListView时,我维护/保存/恢复listview的滚动位置。这是我返回ListView时listview的维护/保存/恢复滚动位置的代码行。这是当我点击listView项目并返回到listview时,当返回页面时保持滚动位置。但是当我从不同的页面添加一些新的texttual项目到列表视图的开头那时我想从列表视图项目的开头显示listview。如何解决这个问题。
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putString("message", "This is my message to be reloaded");
super.onSaveInstanceState(outState);
int index = listView.getFirstVisiblePosition();
// Log.e(""," 111 index="+index);
View view = listView.getChildAt(0);
int top = (view == null) ? 0 : view.getTop();
// Log.e("", "222 top=" + top);
final SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("key1", index);
editor.putInt("key2", top);
editor.commit();
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if( savedInstanceState != null )
{
Toast.makeText(this, savedInstanceState .getString("message"), Toast.LENGTH_LONG).show();
}
setContentView(R.layout.all_post);
}