2015-02-11 150 views
2

我在我的項目中有一個列表視圖,它包含圖像和信息。
SO假設它包含50人的信息。 現在,如果我想刪除最後兩個項目,刪除最後一個項目後,listlview自動聚焦到列表的頂部。
所以,現在用戶已經向下滾動所有項目以到達第49項並刪除它。
那麼是否有任何方法來解決從我刪除該項目的位置的列表視圖位置。對於刪除信息Listview自動滾動到頂部

//代碼

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(); 

     } 
    }); 
} 
+0

嘗試'listView.setSelectionAfterHeaderView();'或'list.smoothScrollToPosition(0);' – 2015-02-11 10:32:13

+0

你可以發佈你正在使用的代碼嗎?它會更容易幫助你, – 2015-02-11 10:33:56

回答

1

獲取位置從刪除的項目,然後通過這樣的位置,爲setSelection方法:listview.setSelection(pos);

+0

Partially Solved the problem.one more problem i have also in it.AnywayThanks many – 2015-02-11 11:56:06

0

與嘗試下面的代碼

刪除列表中的項目之前,使用下面的代碼

獲得醒目的位置
 mIndex = listView.getFirstVisiblePosition(); 
     View v = listView.getChildAt(0); 
     mTop = (v == null) ? 0 : v.getTop(); 

刪除項目後設置的位置列表視圖

listView.setSelectionFromTop(mIndex, mTop);