我有Custom adapter
扩展BaseAdapter
,一切正常,但getView
方法中项目的返回位置看起来不对。在列表的first scroll
之后它给出了最后显示的项目的索引。
首先从上到下滚动:0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 10 - 11 - 12
首先从下到上滚动:12 - 11 - 10 - 9 - 8 - 7 - 6
第二个从上到下滚动:6 - 7 - 8 - 9 - 10 - 11 - 12
并开始非常随机。
这是我的适配器代码:
public abstract class FourComponentsAdapter extends BaseAdapter {
private UIViewImpl context;
private ArrayList<FourComponentsListItem> listItems;
private int layoutId;
private int itemPosition;
public FourComponentsAdapter (Context context, ArrayList<FourComponentsListItem> listItems, int layouId){
this.context = context;
this.listItems = listItems;
this.layoutId = layouId;
}
@Override
public int getCount () {
return listItems.size ();
}
@Override
public Object getItem (int position) {
return listItems.get (position);
}
@Override
public long getItemId (int position) {
return position;
}
@Override
public View getView (int position, View convertView, ViewGroup parent) {
View rowView = convertView;
ViewHolder viewHolder;
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater ();
rowView = inflater.inflate (layoutId, null);
viewHolder = new ViewHolder ();
UIImage thumbnail = findThumbnail (rowView, viewHolder);
UILabel title = findTitle (rowView, viewHolder);
UILabel subTitle = findSubTitle (rowView, viewHolder);
UIIcon actions = findIconAction (rowView, viewHolder);
if (thumbnail != null) {
viewHolder.iconThumbnail = thumbnail;
}
if (title != null) {
viewHolder.title = title;
}
if (subTitle != null) {
viewHolder.subTitle = subTitle;
}
if (actions != null) {
viewHolder.iconAction = actions;
}
rowView.setTag (viewHolder);
} else {
viewHolder = (ViewHolder)rowView.getTag ();
}
itemPosition = position;
if (viewHolder.iconThumbnail != null) {
// Image or text
viewHolder.iconThumbnail.setOnClickListener (new OnClickListener () {
@Override
public void onClick (View view) {
onThumbnailClick (view, itemPosition);
}
});
}
if (viewHolder.title != null) {
viewHolder.title.setText (listItems.get (position).getTitle ());
viewHolder.title.setOnClickListener (new OnClickListener () {
@Override
public void onClick (View view) {
onTitleClick (view, itemPosition);
}
});
}
if (viewHolder.subTitle != null) {
viewHolder.subTitle.setText (listItems.get (position).getSubTitle ());
}
if (viewHolder.iconAction != null) {
viewHolder.iconAction.setText (listItems.get (position).getIconAction ());
viewHolder.iconAction.setTag (String.valueOf (position));
viewHolder.iconAction.setOnClickListener (new OnClickListener () {
@Override
public void onClick (View view) {
onActionClick (view, itemPosition);
}
});
}
System.out.println ("POSITION ::::: " + itemPosition);
return rowView;
}
protected abstract UIImage findThumbnail (View rowView, ViewHolder viewHolder);
protected abstract UILabel findTitle (View rowView, ViewHolder viewHolder);
protected abstract UILabel findSubTitle (View rowView, ViewHolder viewHolder);
protected abstract UIIcon findIconAction (View rowView, ViewHolder viewHolder);
protected abstract void onThumbnailClick (View view, int position);
protected abstract void onTitleClick (View view, int position);
protected abstract void onActionClick (View view, int position);
public static class ViewHolder {
public UIImage iconThumbnail;
public UILabel title;
public UILabel subTitle;
public UIIcon iconAction;
}
}
我的活动代码:
private ArrayList<FourComponentsListItem> listItems = new ArrayList<FourComponentsListItem> ();
....
UIListView listView = (UIListView) view.findViewById (R.id.list));
listView.setAdapter (
new FourComponentsAdapter (UserActivityView.this, listItems, listId) {
@Override
protected UIImage findThumbnail (View rowView, ViewHolder viewHolder) {
return (UIImage)rowView.findViewById (R.id.thumbnail);
}
@Override
protected UILabel findTitle (View rowView, ViewHolder viewHolder) {
return (UILabel)rowView.findViewById (R.id.name);
}
@Override
protected UILabel findSubTitle (View rowView, ViewHolder viewHolder) {
return (UILabel)rowView.findViewById (R.id.subName);
}
@Override
protected UIIcon findIconAction (View rowView, ViewHolder viewHolder) {
return (UIIcon)rowView.findViewById (R.id.actions);
}
@Override
protected void onThumbnailClick (View view, int position) {
}
@Override
protected void onTitleClick (View view, int position) {
}
@Override
protected void onActionClick (View view, int position) {
// Code here
}
);
我错过了什么吗?谢谢。
答案 0 :(得分:0)
不要将变量itemPosition
用作类成员。
要在onClickListener中使用position
,请将其声明为final。
此外,如果您不使用getItem
和getItemId
,则应分别返回null
和0
。
PS:信任ListView,它知道它需要哪个视图:)