給ListView添加了一個headerView後,運行後界面上不會出現headerView佈局。測試發現,當給ListView設置adapter後,headerView就能夠顯示了。 java
例如: app
listView = (ListView) findViewById(R.id.list); headerView = LayoutInflater.from(this).inflate(R.layout.header, listView, false); listView.addHeaderView(headerView, null, false);
設置後在onItemClickListener或者onItemLongClickListener等等涉及到position的狀況下,position每每不是你指望的值,好比多是從1開始的 ide
adapter.getItem(int position)
position多是從1開始的 佈局
listView.getAdapter().getItem(int position)
position是指望的值。 測試
關於這個問題,能夠看看源碼的描述: this
/** * Returns the adapter currently in use in this ListView. The returned adapter * might not be the same adapter passed to {@link #setAdapter(ListAdapter)} but * might be a {@link WrapperListAdapter}. * * @return The adapter currently used to display data in this ListView. * * @see #setAdapter(ListAdapter) */ @Override public ListAdapter getAdapter() { return mAdapter; }