标签:
转载地址:http://blog.iamzsx.me/show.html?id=147001
我们在使用ListView的时候,一般都会为ListView添加一个响应事件android.widget.AdapterView.OnItemClickListener。本文主要在于对OnItemClickListener的position和id参数做详细的解释,我相信有些人在这上面走了些弯路。
position | The position of the view in the adapter. |
---|---|
id | The row id of the item that was clicked. |
437 public void setAdapter(ListAdapter adapter) { 438 if (mAdapter != null && mDataSetObserver != null) { 439 mAdapter.unregisterDataSetObserver(mDataSetObserver); 440 } 442 resetList(); 443 mRecycler.clear(); 445 if (mHeaderViewInfos.size() > 0|| mFooterViewInfos.size() > 0) { 446 mAdapter = new HeaderViewListAdapter(mHeaderViewInfos, mFooterViewInfos, adapter); 447 } else { 448 mAdapter = adapter; 449 }
2497 private class PerformClick extends WindowRunnnable implements Runnable { 2498 int mClickMotionPosition; 2500 public void run() { 2501 // The data has changed since we posted this action in the event queue, 2502 // bail out before bad things happen 2503 if (mDataChanged) return; 2505 final ListAdapter adapter = mAdapter; 2506 final int motionPosition = mClickMotionPosition; 2507 if (adapter != null && mItemCount > 0 && 2508 motionPosition != INVALID_POSITION && 2509 motionPosition < adapter.getCount() && sameWindow()) { 2510 final View view = getChildAt(motionPosition - mFirstPosition); 2511 // If there is no view, something bad happened (the view scrolled off the 2512 // screen, etc.) and we should cancel the click 2513 if (view != null) { 2514 performItemClick(view, motionPosition, adapter.getItemId(motionPosition)); 2515 } 2516 } 2517 } 2518 }
188 public long getItemId(int position) { 189 int numHeaders = getHeadersCount(); 190 if (mAdapter != null && position >= numHeaders) { 191 int adjPosition = position - numHeaders; 192 int adapterCount = mAdapter.getCount(); 193 if (adjPosition < adapterCount) { 194 return mAdapter.getItemId(adjPosition); 195 } 196 } 197 return -1; 198 }
void onItemClick(AdapterViewparent, View view, int position, long id){ if(id<0) { // 点击的是headerView或者footerView return; } int realPosition=(int)id; T item=getItem(realPosition); // 响应代码 }
标签:
原文地址:http://www.cnblogs.com/zhouxiuquan/p/4538536.html