【发布时间】:2015-02-11 10:30:47
【问题描述】:
我的项目中有一个列表视图,它包含图像和信息。
所以假设它包含 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();
}
});
}
【问题讨论】:
-
试试
listView.setSelectionAfterHeaderView();或list.smoothScrollToPosition(0); -
您可以发布您用来执行此操作的代码吗?这样帮助你会更容易