2013-10-08 29 views
0

後不滾動在我的應用程序,我有我需要刷新一個ListView兩個方案(customersList):的ListView performItemClick onActivityResult

1)當客戶搜索,我要處理的建議項目點擊一個搜索查看

2)當我想顯示在另一活動中創建新的客戶

我負責刷新ListView的一個方法:

private void showCustomer(Integer customerId) { 

    ListView customersList = (ListView) findViewById(id.list); 

    if(customersList != null) { 
     Integer listId = getItemPositionByAdapterId(customersList.getAdapter(), customerId); 
     customersList.performItemClick(
     customersList.getAdapter().getView(listId, null, null), 
     listId, 
     customersList.getAdapter().getItemId(listId) 
    ); 
     customersList.requestFocusFromTouch(); 
     customersList.setSelection(listId); 
    } 
} 

private int getItemPositionByAdapterId(ListAdapter adapter, final long id) 
{ 
    for (int i = 0; i < adapter.getCount(); i++) 
    { 
     if (adapter.getItemId(i) == id) 
      return i; 
    } 
    return -1; 
} 

的showCustomer()方法被調用在兩個地方:

/** 
* Scenario 1: Handle suggestions item click 
*/ 
@Override 
protected void onNewIntent(Intent intent) { 
    if (Intent.ACTION_VIEW.equals(intent.getAction())) 
    Uri data = intent.getData(); 
    String customerIdString = data.getLastPathSegment(); 
    Integer customerId = Integer.parseInt(customerIdString); 

    if (customerId != null) { 
     showCustomer(customerId); 
    } 
    } 
    super.onNewIntent(intent); 
} 

/** 
* Scenario 2: Handle new customer creation 
*/ 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    // Check which request we're responding to 
    switch (requestCode) { 
    case RESULT_CUSTOMER_ADD: 
     // Make sure the request was successful 
     if (resultCode == RESULT_OK) { 
     Integer customerId = data.getIntExtra(MyContract.CustomersEntry._ID, 0); 
     // This one doesn't work as expected! 
     showCustomer(customerId); 
     } 
    break; 
    } 
} 

當從onNewIntent()(建議項目點擊)呼叫,一切工作正常 - 該項目被選中,列表滾動到該項目。

從onActivityResult()調用時,該項目被選中,但列表不會滾動到合適的元素。

我出來的想法。爲什麼它在這兩種情況下都不以相同的方式工作?任何幫助將不勝感激。

回答

0

你可以嘗試: smoothCrollToPostion(listId)在showCustomer方法,而不是requestFocusFromTouch()?我現在無法嘗試,所以我會等待您的回覆。

+0

這有幫助!我將使用smoothScrollToPositionFromTop(),因爲它的持續時間作爲參數。謝謝。 – schabluk

+0

@schabluk我很高興我幫助^ _ ^ – budaancamanyak

相關問題