列表视图在编辑条目后滚动到顶部

时间:2012-04-26 23:14:48

标签: java android

我正在尝试创建一个列表视图,当您单击某个条目时,您可以对其进行修改,并在点击确认后仍然会显示您刚修改的条目,而不是滚动到顶部。

进行了建议更改,在离开编辑活动后,它仍然滚动到顶部并且找不到正确的滚动位置。我错过了什么吗?

static int firstPosition = 0;

protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Cursor c = mNotesCursor;
    ListView mListView = getListView();
    firstPosition = mListView.getFirstVisiblePosition();
    c.moveToPosition(position);
    Intent i = new Intent(this, QuoteEdit.class);
    i.putExtra(QuotesDBAdapter.KEY_ROWID, id);
    i.putExtra(QuotesDBAdapter.KEY_QUOTES, c.getString(
    c.getColumnIndexOrThrow(QuotesDBAdapter.KEY_QUOTES)));
    startActivityForResult(i, ACTIVITY_EDIT);
}
    public void onResume(int requestCode, int resultCode, Intent intent) {
ListView mListView = getListView();

if (mListView != null && firstPosition >= 0){
   mListView.scrollTo(0,firstPosition);
//  mListView.setSelection(firstPosition);

}

}
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    try {
        super.onActivityResult(requestCode, resultCode, intent);
        Bundle extras = intent.getExtras();
        switch(requestCode) {
            case ACTIVITY_CREATE:
                String title = extras.getString(QuotesDBAdapter.KEY_QUOTES);
                mDbHelper.createQuote(title);
                break;
            case ACTIVITY_EDIT: 
                ListView mListView = getListView();
                Long rowId = extras.getLong(QuotesDBAdapter.KEY_ROWID);
                if (rowId != null) {
                    String editTitle = extras.getString(QuotesDBAdapter.KEY_QUOTES);
                    mDbHelper.updateQuote(rowId, editTitle);
                    mListView.setSelection(firstPosition);
                }
                fillData();
                break;
        }
    }
    catch (Exception ex){
        Context context = getApplicationContext();
        CharSequence text = ex.toString();
        int duration = Toast.LENGTH_LONG;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
    }
}

1 个答案:

答案 0 :(得分:1)

您的编辑器是另一个Activity,您使用startActivityForResult()来呼叫。这一行实际上并没有在这里停止 - 它将继续处理方法的其余部分,包括mListView.setSelection(firstPosition);语句。换句话说,在用户编辑值之前调用setSelection()

关闭编辑器Activity后,它会将结果返回到onActivityResult()。在此方法中,您需要告诉它再次将ListView滚动到正确的位置。也许这样的事情......

static int firstPosition = 0; // this is a global variable - set it in onListItemClick()

public void onActivityResult (int requestCode, int resultCode, Intent data){
    mListView.setSelection(firstPosition);
    }

并更改您的onListItemClick()方法以使用全局变量,就像这样...

protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Cursor c = mNotesCursor;
    ListView mListView = getListView();
    firstPosition = mListView.getFirstVisiblePosition(); // changed this line
    c.moveToPosition(position);
    Intent i = new Intent(this, QuoteEdit.class);
    i.putExtra(QuotesDBAdapter.KEY_ROWID, id);
    i.putExtra(QuotesDBAdapter.KEY_QUOTES, c.getString(
    c.getColumnIndexOrThrow(QuotesDBAdapter.KEY_QUOTES)));
    startActivityForResult(i, ACTIVITY_EDIT);
    mListView.setSelection(firstPosition);
}

现在,setSelection()方法不会更改滚动位置,但您可以尝试这个...

onListItemClick()中,更改为以下内容...

    firstPosition = mListView.getScrollY();

onActivityResult()中,请更改为以下内容...

    mlistView.scrollTo(0,firstPosition);

可能还会将以下代码添加到onResume()方法的末尾(在您的主Activity中),以便在您返回主Activity时运行它。

ListView mListView = getElementById("myListView");
if (mlistView != null && firstPosition >= 0){
    mlistView.scrollTo(0,firstPosition);
}