Listview 自动滚动到顶部

Listview is automatically scrolling to the top

我的项目中有一个列表视图,它包含图像和信息。
所以假设它包含 50 个人信息。 现在,如果我想删除最后两项,删除最后一项后,listlview 会自动聚焦到列表顶部。
所以现在用户向下滚动所有项目以到达第 49 项并将其删除。
那么有什么方法可以将列表视图位置固定在我删除项目的那个位置。

//删除信息代码

public void deleteFeed(int rowID) {
        // fetch the image File name
        Cursor cursor = dbAdapter.getTimelineFeedByID(mSysPrefs.getBabyID(), rowID);
        if(cursor != null && cursor.moveToFirst()) {
            // fetch from DB Cursor
            String imageFileName = cursor.getString(cursor.getColumnIndex(DatabaseAppHelper.KEY_IMAGENAME));
            File file = new File(GlobalConstants.FILE_PATH, imageFileName + GlobalConstants.IMAGE_EXTENSION);
            if(file.exists()) {
                file.delete();
            }
            //listview.smoothScrollToPosition(0, listview.getHeight());
            listview.smoothScrollToPosition(0, listview.getBottom());
        }
        // now delete the record from the DB    
        if(dbAdapter.deleteTimelineFeed(rowID) != -1) {
            onResume();
        } else {
            Toast.makeText(this, "Please try again..", Toast.LENGTH_LONG).show();
        }
    }

//创建列表视图的代码

@Override
protected void onResume() {
    super.onResume();
     turnGPSOn(); // method to turn on the GPS if its in off state.
     getMyCurrentLocation();  
    dbAdapter.openDBConnection();
    new Handler().post(new Runnable() {
        @Override
        public void run() {
            timelineCursorAdapter = new TimelineCursorAdapter(TimelineViewActivity.this,
                    dbAdapter.getAllTimelineFeedsForBaby(mSysPrefs.getBabyID()), 0);
            listview.setAdapter(timelineCursorAdapter);
            //timelineCursorAdapter.notifyDataSetChanged();
            listview.invalidateViews();

        }
    });
}

试试下面的代码

在删除列表项之前使用以下代码获得可见位置

       mIndex = listView.getFirstVisiblePosition();
        View v = listView.getChildAt(0);
        mTop = (v == null) ? 0 : v.getTop();

删除项目后将该位置设置为列表视图

    listView.setSelectionFromTop(mIndex, mTop);

从您删除的项目中获取位置,然后将位置传递给 setselection 方法,如下所示:listview.setSelection(pos);

getCount() returns 适配器持有的当前项目位置。以下方法可以通过在 onResume()

中调用它来提供帮助

mListView.smoothScrollToPosition(mAdapter.getCount());