下面是一个ListView一个code段。我增加了一个emptyView和headerView。加入headerView导致在onItemClick的位置被增加一。
Below is a code snippet with a ListView. I added an emptyView and a headerView. Adding the headerView causes the position in the onItemClick to be increased by one.
所以没有headerView第一列表元素将具有位置0,与headerView第一列表元素的位置将是1!
So without the headerView the first list element would have position 0, with the headerView the position of the first list element would be 1!
这会导致错误在我的适配器,例如当调用getItem()时,用一些其他的方法,请参阅下文。 奇怪的事情:在适配器的getView()方法的第一个列表元素与要求,即使headerView加入!!位置0
This causes errors in my adapter, e.g. when calling getItem() and using some other methods, see below. Strange thing: In the getView() method of the adapter the first list element is requested with position 0 even if the headerView is added!!
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ListView list = (ListView) viewSwitcher.findViewById(R.id.list);
View emptyView = viewSwitcher.findViewById(R.id.empty);
list.setEmptyView(emptyView);
View sectionHeading = inflater.inflate(R.layout.heading, list, false);
TextView sectionHeadingTextView = (TextView) sectionHeading.findViewById(R.id.headingText);
sectionHeadingTextView.setText(headerText);
list.addHeaderView(sectionHeading);
list.setAdapter(listAdapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
listAdapter.getItem(position);
//Do something with it
}
});
}
有些适配器方法:
Some adapter methods:
@Override
public int getViewTypeCount() {
return TYPE_COUNT;
}
@Override
public int getItemViewType(int position) {
return (position < items.size()) ? ITEM_NORMAL : ITEM_ADVANCED;
}
@Override
public Product getItem(int position) {
return items.get(position);
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
@Override
public boolean isEnabled(int position) {
return (position < items.size());
}
这增加了headerView时是正常的行为?以及如何克服这些问题在我的适配器?
Is this the normal behaviour when adding a headerView?? And how to overcome the issues in my adapter?
推荐答案
如果您添加到的ListView
即成为列表中的第一项的标题。你可以通过覆盖 getCount将()
返回一个项目少,但要确保您处理的getItem(),
getItemId()
等为标题将是位置0的项目。
If you add a header to a ListView
that becomes the first item in the list. You could compensate for this by overriding the getCount()
to return one item less, but ensure you handle the getItem()
, getItemId()
and so on as the header would be the item in position 0.
相关推荐
最新文章