ListView performItemClick在onActivityResult之后不滚动

时间:2013-10-08 14:34:51

标签: android listview click

在我的应用程序中,我有两个场景需要刷新ListView(customersList):

1)在搜索客户时,我必须在SearchView

中处理建议项目

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()调用时,项目被选中,但列表不会滚动到适当的元素。

我没有想法。在两种情况下,为什么它不以相同的方式工作?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

你可以尝试:     smoothColrollToPostion(listId)在showCustomer方法而不是requestFocusFromTouch()?我现在无法尝试,所以我会等你的回复。